From 30a1e32c91e6237ed518f5b5512cceae87ba5a40 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 09:24:34 +0200 Subject: [PATCH 1/9] Introduce the CacheFileUtils helper --- .../Compiler/CompilerDebugDumpPass.php | 5 +- .../ContainerBuilderDebugDumpPass.php | 5 +- src/Symfony/Component/Config/ConfigCache.php | 32 +++----- .../Component/Config/Util/CacheFileUtils.php | 77 +++++++++++++++++++ src/Symfony/Component/HttpKernel/Kernel.php | 9 +-- 5 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 src/Symfony/Component/Config/Util/CacheFileUtils.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php index ad93d7b419bba..e6e55f80f842c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Config\Util\CacheFileUtils; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -20,8 +20,7 @@ class CompilerDebugDumpPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { - $cache = new ConfigCache($this->getCompilerLogFilename($container), false); - $cache->write(implode("\n", $container->getCompiler()->getLog())); + CacheFileUtils::dumpInFile($this->getCompilerLogFilename($container), implode("\n", $container->getCompiler()->getLog())); } public static function getCompilerLogFilename(ContainerInterface $container) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php index 1457d7cf2354d..1b7a337a3587e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php @@ -14,7 +14,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Dumper\XmlDumper; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Config\Util\CacheFileUtils; /** * Dumps the ContainerBuilder to a cache file so that it can be used by @@ -28,7 +28,6 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $dumper = new XmlDumper($container); - $cache = new ConfigCache($container->getParameter('debug.container.dump'), false); - $cache->write($dumper->dump()); + CacheFileUtils::dumpInFile($container->getParameter('debug.container.dump'), $dumper->dump()); } } diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index e3ce58adf245c..ced40e03b2f0d 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Config; use Symfony\Component\Config\Resource\ResourceInterface; +use Symfony\Component\Config\Util\CacheFileUtils; /** * ConfigCache manages PHP cache files. @@ -48,6 +49,15 @@ public function __toString() return $this->file; } + /** + * Gets the meta file path. + * + * @return string The meta file path + */ + protected function getMetaFile() { + return $this->file . '.meta'; + } + /** * Checks if the cache is still fresh. * @@ -92,28 +102,10 @@ public function isFresh() */ public function write($content, array $metadata = null) { - $dir = dirname($this->file); - if (!is_dir($dir)) { - if (false === @mkdir($dir, 0777, true)) { - throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir)); - } - } elseif (!is_writable($dir)) { - throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir)); - } - - $tmpFile = tempnam($dir, basename($this->file)); - if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) { - @chmod($this->file, 0666 & ~umask()); - } else { - throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file)); - } + CacheFileUtils::dumpInFile($this->file, $content); if (null !== $metadata && true === $this->debug) { - $file = $this->file.'.meta'; - $tmpFile = tempnam($dir, basename($file)); - if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) { - @chmod($file, 0666 & ~umask()); - } + CacheFileUtils::dumpInFile($this->getMetaFile(), serialize($metadata)); } } } diff --git a/src/Symfony/Component/Config/Util/CacheFileUtils.php b/src/Symfony/Component/Config/Util/CacheFileUtils.php new file mode 100644 index 0000000000000..ca9e705cc51b0 --- /dev/null +++ b/src/Symfony/Component/Config/Util/CacheFileUtils.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Util; + +/** + * CacheFileUtils contains utility method to atomically (?) write files. + * + * This class contains static methods only and is not meant to be instantiated. + * + * @author Matthias Pigulla + */ +class CacheFileUtils +{ + + /** + * Make sure the given directory can be used as a cache directory, creating it if necessary. + * @param string $dir The directory path + * @param string $name An optional name to be used in the exception message in case of failure + * @throws \RuntimeException if the directory cannot be created or written to. + */ + public static function createCacheDir($dir, $name = null) + { + if ($name) { + $name = " ($name)"; + } + + if (!is_dir($dir)) { + if (false === @mkdir($dir, 0777, true)) { + throw new \RuntimeException(sprintf('Unable to create the %s directory%s\n', $dir, $name)); + } + } elseif (!is_writable($dir)) { + throw new \RuntimeException(sprintf('Unable to write in the %s directory%s\n', $dir, $name)); + } + } + + /** + * Tries to create a writeable directory that can contain a given file. + * + * @param string $filename The file the containing directory has to be created for + * @return string The directory now available for caching + * @throws \RuntimeException If the directory cannot be created or written to + */ + public static function createDirectoryForFile($filename) + { + $dir = dirname($filename); + self::createCacheDir($dir); + return $dir; + } + + /** + * Dumps content into a file, trying to make it atomically. The directory for the file must exist. + * @param string $filename The file to be written to. + * @param string $content The data to write into the file. + * @throws \RuntimeException If the file cannot be written to. + */ + public static function dumpInFile($filename, $content) + { + $dir = self::createDirectoryForFile($filename); + + $tmpFile = tempnam($dir, basename($filename)); + if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $filename)) { + @chmod($filename, 0666 & ~umask()); + } else { + throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $filename)); + } + + } +} diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 17d56823c0166..d948b829105e9 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -30,6 +30,7 @@ use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\Config\Loader\DelegatingLoader; use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Config\Util\CacheFileUtils; use Symfony\Component\ClassLoader\ClassCollectionLoader; /** @@ -613,13 +614,7 @@ protected function getEnvParameters() protected function buildContainer() { foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) { - if (!is_dir($dir)) { - if (false === @mkdir($dir, 0777, true)) { - throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir)); - } - } elseif (!is_writable($dir)) { - throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir)); - } + CacheFileUtils::createCacheDir($dir, $name); } $container = $this->getContainerBuilder(); From 669898bf57b75a9d1f06a161045d4dcaca13f10d Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 11:17:25 +0200 Subject: [PATCH 2/9] Revert newly introduced dependency --- src/Symfony/Component/HttpKernel/Kernel.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index d948b829105e9..17d56823c0166 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -30,7 +30,6 @@ use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\Config\Loader\DelegatingLoader; use Symfony\Component\Config\ConfigCache; -use Symfony\Component\Config\Util\CacheFileUtils; use Symfony\Component\ClassLoader\ClassCollectionLoader; /** @@ -614,7 +613,13 @@ protected function getEnvParameters() protected function buildContainer() { foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) { - CacheFileUtils::createCacheDir($dir, $name); + if (!is_dir($dir)) { + if (false === @mkdir($dir, 0777, true)) { + throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir)); + } + } elseif (!is_writable($dir)) { + throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir)); + } } $container = $this->getContainerBuilder(); From ed861bac63fe1121cc09e6ed73dfeab4e75568d5 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 12:25:52 +0200 Subject: [PATCH 3/9] Remove unused methods. The only client of these methods was removed in 669898bf57b75a9d1f06a161045d4dcaca13f10d. --- .../Component/Config/Util/CacheFileUtils.php | 46 ++++--------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Config/Util/CacheFileUtils.php b/src/Symfony/Component/Config/Util/CacheFileUtils.php index ca9e705cc51b0..ac5e53e895237 100644 --- a/src/Symfony/Component/Config/Util/CacheFileUtils.php +++ b/src/Symfony/Component/Config/Util/CacheFileUtils.php @@ -20,58 +20,30 @@ */ class CacheFileUtils { - /** - * Make sure the given directory can be used as a cache directory, creating it if necessary. - * @param string $dir The directory path - * @param string $name An optional name to be used in the exception message in case of failure - * @throws \RuntimeException if the directory cannot be created or written to. + * Dumps content into a file, trying to make it atomically. The directory for the file must exist. + * @param string $filename The file to be written to. + * @param string $content The data to write into the file. + * @throws \RuntimeException If the file cannot be written to. */ - public static function createCacheDir($dir, $name = null) + public static function dumpInFile($filename, $content) { - if ($name) { - $name = " ($name)"; - } + $dir = dirname($filename); if (!is_dir($dir)) { if (false === @mkdir($dir, 0777, true)) { - throw new \RuntimeException(sprintf('Unable to create the %s directory%s\n', $dir, $name)); + throw new \RuntimeException(sprintf('Unable to create the %s directory\n', $dir)); } } elseif (!is_writable($dir)) { - throw new \RuntimeException(sprintf('Unable to write in the %s directory%s\n', $dir, $name)); + throw new \RuntimeException(sprintf('Unable to write in the %s directory\n', $dir)); } - } - - /** - * Tries to create a writeable directory that can contain a given file. - * - * @param string $filename The file the containing directory has to be created for - * @return string The directory now available for caching - * @throws \RuntimeException If the directory cannot be created or written to - */ - public static function createDirectoryForFile($filename) - { - $dir = dirname($filename); - self::createCacheDir($dir); - return $dir; - } - - /** - * Dumps content into a file, trying to make it atomically. The directory for the file must exist. - * @param string $filename The file to be written to. - * @param string $content The data to write into the file. - * @throws \RuntimeException If the file cannot be written to. - */ - public static function dumpInFile($filename, $content) - { - $dir = self::createDirectoryForFile($filename); $tmpFile = tempnam($dir, basename($filename)); + if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $filename)) { @chmod($filename, 0666 & ~umask()); } else { throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $filename)); } - } } From bbb884bee6170995562dacbd716b345512c1ac4b Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 13:38:32 +0200 Subject: [PATCH 4/9] Move the remaining method from CacheFileUtils to the Filesystem class --- .../Compiler/CompilerDebugDumpPass.php | 5 +- .../ContainerBuilderDebugDumpPass.php | 5 +- src/Symfony/Component/Config/ConfigCache.php | 7 +-- .../Component/Config/Util/CacheFileUtils.php | 49 ------------------- .../Component/Filesystem/Filesystem.php | 27 ++++++++++ 5 files changed, 37 insertions(+), 56 deletions(-) delete mode 100644 src/Symfony/Component/Config/Util/CacheFileUtils.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php index e6e55f80f842c..b4b50de7058fe 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php @@ -12,15 +12,16 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\Config\Util\CacheFileUtils; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\Filesystem\Filesystem; class CompilerDebugDumpPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { - CacheFileUtils::dumpInFile($this->getCompilerLogFilename($container), implode("\n", $container->getCompiler()->getLog())); + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->getCompilerLogFilename($container), implode("\n", $container->getCompiler()->getLog())); } public static function getCompilerLogFilename(ContainerInterface $container) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php index 1b7a337a3587e..3cbf65af05bdd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php @@ -14,7 +14,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Dumper\XmlDumper; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\Config\Util\CacheFileUtils; +use Symfony\Component\Filesystem\Filesystem; /** * Dumps the ContainerBuilder to a cache file so that it can be used by @@ -28,6 +28,7 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $dumper = new XmlDumper($container); - CacheFileUtils::dumpInFile($container->getParameter('debug.container.dump'), $dumper->dump()); + $fs = new Filesystem(); + $fs->dumpFile($container->getParameter('debug.container.dump'), $dumper->dump()); } } diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index ced40e03b2f0d..dfdd5cc3760f2 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Config; use Symfony\Component\Config\Resource\ResourceInterface; -use Symfony\Component\Config\Util\CacheFileUtils; +use Symfony\Component\Filesystem\Filesystem; /** * ConfigCache manages PHP cache files. @@ -102,10 +102,11 @@ public function isFresh() */ public function write($content, array $metadata = null) { - CacheFileUtils::dumpInFile($this->file, $content); + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->file, $content); if (null !== $metadata && true === $this->debug) { - CacheFileUtils::dumpInFile($this->getMetaFile(), serialize($metadata)); + $filesystem->dumpFile($this->getMetaFile(), serialize($metadata)); } } } diff --git a/src/Symfony/Component/Config/Util/CacheFileUtils.php b/src/Symfony/Component/Config/Util/CacheFileUtils.php deleted file mode 100644 index ac5e53e895237..0000000000000 --- a/src/Symfony/Component/Config/Util/CacheFileUtils.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Config\Util; - -/** - * CacheFileUtils contains utility method to atomically (?) write files. - * - * This class contains static methods only and is not meant to be instantiated. - * - * @author Matthias Pigulla - */ -class CacheFileUtils -{ - /** - * Dumps content into a file, trying to make it atomically. The directory for the file must exist. - * @param string $filename The file to be written to. - * @param string $content The data to write into the file. - * @throws \RuntimeException If the file cannot be written to. - */ - public static function dumpInFile($filename, $content) - { - $dir = dirname($filename); - - if (!is_dir($dir)) { - if (false === @mkdir($dir, 0777, true)) { - throw new \RuntimeException(sprintf('Unable to create the %s directory\n', $dir)); - } - } elseif (!is_writable($dir)) { - throw new \RuntimeException(sprintf('Unable to write in the %s directory\n', $dir)); - } - - $tmpFile = tempnam($dir, basename($filename)); - - if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $filename)) { - @chmod($filename, 0666 & ~umask()); - } else { - throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $filename)); - } - } -} diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index af26ef4c6f79d..50c3fdff3075d 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -430,4 +430,31 @@ private function toIterator($files) return $files; } + + /** + * Atomically dumps content into a file. + * + * @param string $filename The file to be written to. + * @param string $content The data to write into the file. + * @throws IOException If the file cannot be written to. + */ + public function dumpFile($filename, $content) + { + $dir = dirname($filename); + + if (!is_dir($dir)) { + $this->mkdir($dir); + } elseif (!is_writable($dir)) { + throw new IOException(sprintf('Unable to write in the %s directory\n', $dir)); + } + + $tmpFile = tempnam($dir, basename($filename)); + + if (false !== @file_put_contents($tmpFile, $content)) { + throw new IOException(sprintf('Failed to write file "%s".', $filename)); + } + + $this->rename($tmpFile, $filename); + $this->chmod($filename, 0666, umask()); + } } From 249abb38516dacec3cc3015f3465161888b9ae7d Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 14:06:39 +0200 Subject: [PATCH 5/9] Bug introduced when turning the condition around --- src/Symfony/Component/Filesystem/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 50c3fdff3075d..822efb8adb0b3 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -450,7 +450,7 @@ public function dumpFile($filename, $content) $tmpFile = tempnam($dir, basename($filename)); - if (false !== @file_put_contents($tmpFile, $content)) { + if (false === @file_put_contents($tmpFile, $content)) { throw new IOException(sprintf('Failed to write file "%s".', $filename)); } From 327befe5927f4a89b8b8a50eb6c41a9f2bb33c20 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 14:16:30 +0200 Subject: [PATCH 6/9] With dumpFile() in the Filesystem class, the mode must be a param. Clients of ConfigCache are/were using 0666 & ~umask(). --- .../Compiler/CompilerDebugDumpPass.php | 6 +++++- .../Compiler/ContainerBuilderDebugDumpPass.php | 8 ++++++-- src/Symfony/Component/Config/ConfigCache.php | 5 +++-- src/Symfony/Component/Filesystem/Filesystem.php | 11 ++++++----- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php index b4b50de7058fe..20591c89cac32 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php @@ -21,7 +21,11 @@ class CompilerDebugDumpPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $filesystem = new Filesystem(); - $filesystem->dumpFile($this->getCompilerLogFilename($container), implode("\n", $container->getCompiler()->getLog())); + $filesystem->dumpFile( + $this->getCompilerLogFilename($container), + implode("\n", $container->getCompiler()->getLog()), + 0666 & ~umask() + ); } public static function getCompilerLogFilename(ContainerInterface $container) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php index 3cbf65af05bdd..a9916cdbdc1c8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php @@ -28,7 +28,11 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $dumper = new XmlDumper($container); - $fs = new Filesystem(); - $fs->dumpFile($container->getParameter('debug.container.dump'), $dumper->dump()); + $filesystem = new Filesystem(); + $filesystem->dumpFile( + $container->getParameter('debug.container.dump'), + $dumper->dump(), + 0666 & ~umask() + ); } } diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index dfdd5cc3760f2..a1865eda67c2b 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -102,11 +102,12 @@ public function isFresh() */ public function write($content, array $metadata = null) { + $mode = 0666 & ~umask(); $filesystem = new Filesystem(); - $filesystem->dumpFile($this->file, $content); + $filesystem->dumpFile($this->file, $content, $mode); if (null !== $metadata && true === $this->debug) { - $filesystem->dumpFile($this->getMetaFile(), serialize($metadata)); + $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode); } } } diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 822efb8adb0b3..809b32cf5e1ae 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -434,11 +434,12 @@ private function toIterator($files) /** * Atomically dumps content into a file. * - * @param string $filename The file to be written to. - * @param string $content The data to write into the file. - * @throws IOException If the file cannot be written to. + * @param string $filename The file to be written to. + * @param string $content The data to write into the file. + * @param integer $mode The file mode (octal). + * @throws IOException If the file cannot be written to. */ - public function dumpFile($filename, $content) + public function dumpFile($filename, $content, $mode = 0666) { $dir = dirname($filename); @@ -455,6 +456,6 @@ public function dumpFile($filename, $content) } $this->rename($tmpFile, $filename); - $this->chmod($filename, 0666, umask()); + $this->chmod($filename, $mode); } } From a4904867f48c765f216a6fec189f959ec9b50380 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 15:39:52 +0200 Subject: [PATCH 7/9] Added entry for new dumpFile() method --- src/Symfony/Component/Filesystem/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Symfony/Component/Filesystem/CHANGELOG.md b/src/Symfony/Component/Filesystem/CHANGELOG.md index d20ba07cdc411..e6aee66a572a3 100644 --- a/src/Symfony/Component/Filesystem/CHANGELOG.md +++ b/src/Symfony/Component/Filesystem/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +2.3.0 +----- + + * added the dumpFile() method to atomically write files + 2.2.0 ----- From 6da1b0a1c3a5d4806a3558c03ba13bbf70b6ffe6 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 15:40:19 +0200 Subject: [PATCH 8/9] Use method here as well --- src/Symfony/Component/Config/ConfigCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index a1865eda67c2b..f23fa6cc116b5 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -76,7 +76,7 @@ public function isFresh() return true; } - $metadata = $this->file.'.meta'; + $metadata = $this->getMetaFile(); if (!is_file($metadata)) { return false; } From ad47bc47380188041b7889f40e380ad3766a0110 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 21 Apr 2013 15:53:56 +0200 Subject: [PATCH 9/9] Make method private + fix CS #fail --- src/Symfony/Component/Config/ConfigCache.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index f23fa6cc116b5..cbb198427260f 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -49,15 +49,6 @@ public function __toString() return $this->file; } - /** - * Gets the meta file path. - * - * @return string The meta file path - */ - protected function getMetaFile() { - return $this->file . '.meta'; - } - /** * Checks if the cache is still fresh. * @@ -110,4 +101,15 @@ public function write($content, array $metadata = null) $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode); } } + + /** + * Gets the meta file path. + * + * @return string The meta file path + */ + private function getMetaFile() + { + return $this->file.'.meta'; + } + }