- if (file_put_contents($output.'.new', $content) === false) {
- throw new \Exception();
- }
- } catch(\Exception $e) {
- throw new Error(sprintf('Unable to write to: %s', $output.'.new'), $token->getLine(), $stream->getSourceContext(), $e);
- }
-
- //Remove old file
- if (is_file($output)) {
- try {
- if (unlink($output) === false) {
- throw new \Exception();
- }
- } catch (\Exception $e) {
- throw new Error(sprintf('Unable to unlink: %s', $output), $token->getLine(), $stream->getSourceContext(), $e);
- }
- }
-
- //Rename it
- try {
- if (rename($output.'.new', $output) === false) {
- throw new \Exception();
- }
- } catch (\Exception $e) {
- throw new Error(sprintf('Unable to rename: %s to %s', $output.'.new', $output), $token->getLine(), $stream->getSourceContext(), $e);
+ //Write content to file
+ //XXX: this call is (maybe) atomic
+ //XXX: see https://symfony.com/doc/current/components/filesystem.html#dumpfile
+ $filesystem->dumpFile($output, $content);
+ } catch (IOExceptionInterface $e) {
+ //Throw error
+ throw new Error(sprintf('Unable to write to: %s', $output), $token->getLine(), $stream->getSourceContext(), $e);