From f4e4a2aa1b1a4279057ff43c7c9a59f8124746fa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 14 Mar 2011 18:37:25 +0100 Subject: [PATCH] refactored ConfigCache and optimized container:debug task --- .../ContainerBuilderDebugDumpPass.php | 21 ++++++------ .../Translation/Translator.php | 2 +- src/Symfony/Component/Config/ConfigCache.php | 32 +++++++------------ src/Symfony/Component/HttpKernel/Kernel.php | 15 +++++---- src/Symfony/Component/Routing/Router.php | 4 +-- 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php index d9239b2269b8..5dadcc079097 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php @@ -14,40 +14,37 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Config\ConfigCache; /** * Dumps the ContainerBuilder to a cache file so that it can be used by * debugging tools such as the container:debug console command. * * @author Ryan Weaver + * @author Fabien Potencier */ class ContainerBuilderDebugDumpPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { - $file = self::getBuilderCacheFilename($container); + $cache = new ConfigCache(self::getBuilderCacheFilename($container), false); - if (false !== @file_put_contents($file, serialize($container))) { - chmod($file, 0666); - } else { - throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file)); - } + $cache->write(serialize($container)); } /** * Calculates the cache filename to be used to cache the ContainerBuilder * * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * * @return string */ - public static function getBuilderCacheFilename(ContainerInterface $container) + static public function getBuilderCacheFilename(ContainerInterface $container) { - $cacheDir = $container->getParameter('kernel.cache_dir'); $name = $container->getParameter('kernel.name'); $env = ucfirst($container->getParameter('kernel.environment')); - $debug = ($container->getParameter('kernel.debug')) ? 'Debug' : ''; + $debug = $container->getParameter('kernel.debug') ? 'Debug' : ''; - return $cacheDir.'/'.$name.$env.$debug.'ProjectContainerBuilder.cache'; + return $container->getParameter('kernel.cache_dir').'/'.$name.$env.$debug.'ProjectContainerBuilder.cache'; } -} \ No newline at end of file +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index bf557dc0d3ed..92d5a817fc84 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -89,7 +89,7 @@ protected function loadCatalogue($locale) return parent::loadCatalogue($locale); } - $cache = new ConfigCache($this->options['cache_dir'], 'catalogue.'.$locale, $this->options['debug']); + $cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']); if (!$cache->isFresh()) { $this->initialize(); diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index e6fae6890a77..78806da03868 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -22,19 +22,16 @@ class ConfigCache { protected $debug; - protected $cacheDir; protected $file; /** * Constructor. * - * @param string $cacheDir The cache directory - * @param string $file The cache file name (without the .php extension) + * @param string $file The absolute cache path * @param Boolean $debug Whether debugging is enabled or not */ - public function __construct($cacheDir, $file, $debug) + public function __construct($file, $debug) { - $this->cacheDir = $cacheDir; $this->file = $file; $this->debug = (Boolean) $debug; } @@ -46,7 +43,7 @@ public function __construct($cacheDir, $file, $debug) */ public function __toString() { - return $this->getCacheFile(); + return $this->file; } /** @@ -58,8 +55,7 @@ public function __toString() */ public function isFresh() { - $file = $this->getCacheFile(); - if (!file_exists($file)) { + if (!file_exists($this->file)) { return false; } @@ -67,12 +63,12 @@ public function isFresh() return true; } - $metadata = $this->getCacheFile('meta'); + $metadata = $this->file.'.meta'; if (!file_exists($metadata)) { return false; } - $time = filemtime($file); + $time = filemtime($this->file); $meta = unserialize(file_get_contents($metadata)); foreach ($meta as $resource) { if (!$resource->isFresh($time)) { @@ -93,8 +89,7 @@ public function isFresh() */ public function write($content, array $metadata = null) { - $file = $this->getCacheFile(); - $dir = dirname($file); + $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)); @@ -103,24 +98,19 @@ public function write($content, array $metadata = null) throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir)); } - $tmpFile = tempnam(dirname($file), basename($file)); - if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) { - chmod($file, 0666); + $tmpFile = tempnam(dirname($this->file), basename($this->file)); + if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) { + chmod($this->file, 0666); } else { throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file)); } if (null !== $metadata && true === $this->debug) { - $file = $this->getCacheFile('meta'); + $file = $this->file.'.meta'; $tmpFile = tempnam(dirname($file), basename($file)); if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) { chmod($file, 0666); } } } - - protected function getCacheFile($extension = 'php') - { - return $this->cacheDir.'/'.$this->file.'.'.$extension; - } } diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index f72faa811e88..dfd32c1ca4dd 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -400,7 +400,7 @@ protected function initializeBundles() protected function initializeContainer() { $class = $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; - $cache = new ConfigCache($this->getCacheDir(), $class, $this->debug); + $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug); $fresh = false; if (!$cache->isFresh()) { $container = $this->buildContainer(); @@ -471,13 +471,7 @@ protected function buildContainer() if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) { $container->merge($cont); } - $container->compile(); - - return $container; - } - protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class) - { foreach (array('cache', 'logs') as $name) { $dir = $container->getParameter(sprintf('kernel.%s_dir', $name)); if (!is_dir($dir)) { @@ -489,6 +483,13 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container } } + $container->compile(); + + return $container; + } + + protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class) + { // cache the container $dumper = new PhpDumper($container); $content = $dumper->dump(array('class' => $class)); diff --git a/src/Symfony/Component/Routing/Router.php b/src/Symfony/Component/Routing/Router.php index e2d7cc856b45..8fdcfc0b9d08 100644 --- a/src/Symfony/Component/Routing/Router.php +++ b/src/Symfony/Component/Routing/Router.php @@ -154,7 +154,7 @@ public function getMatcher() } $class = $this->options['matcher_cache_class']; - $cache = new ConfigCache($this->options['cache_dir'], $class, $this->options['debug']); + $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']); if (!$cache->isFresh($class)) { $dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection()); @@ -187,7 +187,7 @@ public function getGenerator() } $class = $this->options['generator_cache_class']; - $cache = new ConfigCache($this->options['cache_dir'], $class, $this->options['debug']); + $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']); if (!$cache->isFresh($class)) { $dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());