Browse Source

refactored ConfigCache and optimized container:debug task

Fabien Potencier 14 years ago
parent
commit
f4e4a2aa1b

+ 9 - 12
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php

@@ -14,40 +14,37 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
 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 <ryan@thatsquality.com>
+ * @author Fabien Potencier <fabien@symfony.com>
  */
 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';
     }
-}
+}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

@@ -89,7 +89,7 @@ class Translator extends BaseTranslator
             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();
 

+ 11 - 21
src/Symfony/Component/Config/ConfigCache.php

@@ -22,19 +22,16 @@ namespace Symfony\Component\Config;
 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 @@ class ConfigCache
      */
     public function __toString()
     {
-        return $this->getCacheFile();
+        return $this->file;
     }
 
     /**
@@ -58,8 +55,7 @@ class ConfigCache
      */
     public function isFresh()
     {
-        $file = $this->getCacheFile();
-        if (!file_exists($file)) {
+        if (!file_exists($this->file)) {
             return false;
         }
 
@@ -67,12 +63,12 @@ class ConfigCache
             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 @@ class ConfigCache
      */
     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 @@ class ConfigCache
             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;
-    }
 }

+ 8 - 7
src/Symfony/Component/HttpKernel/Kernel.php

@@ -400,7 +400,7 @@ abstract class Kernel implements KernelInterface
     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 @@ abstract class Kernel implements KernelInterface
         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 @@ abstract class Kernel implements KernelInterface
             }
         }
 
+        $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));

+ 2 - 2
src/Symfony/Component/Routing/Router.php

@@ -154,7 +154,7 @@ class Router implements RouterInterface
         }
 
         $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 @@ class Router implements RouterInterface
         }
 
         $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());