浏览代码

[FrameworkBundle] refactored the cache:clear command

* removed the hack on the Kernel
* removed inheritance from the warmup command
* major cleanup
Fabien Potencier 14 年之前
父节点
当前提交
e4a636a885

+ 62 - 4
src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

@@ -14,6 +14,8 @@ namespace Symfony\Bundle\FrameworkBundle\Command;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\HttpKernel\KernelInterface;
+use Symfony\Component\Finder\Finder;
 
 /**
  * Clear and Warmup the cache.
@@ -21,7 +23,7 @@ use Symfony\Component\Console\Output\OutputInterface;
  * @author Francis Besset <francis.besset@gmail.com>
  * @author Fabien Potencier <fabien@symfony.com>
  */
-class CacheClearCommand extends CacheWarmupCommand
+class CacheClearCommand extends Command
 {
     /**
      * @see Command
@@ -58,14 +60,70 @@ EOF
         if ($input->getOption('no-warmup')) {
             rename($realCacheDir, $oldCacheDir);
         } else {
-            $this->setWarmupDir($this->container->getParameter('kernel.environment').'_tmp');
+            $warmupDir = $realCacheDir.'_new';
 
-            parent::execute($input, $output);
+            $this->warmup($warmupDir);
 
             rename($realCacheDir, $oldCacheDir);
-            rename($this->kernelTmp->getCacheDir(), $realCacheDir);
+            rename($warmupDir, $realCacheDir);
         }
 
         $this->container->get('filesystem')->remove($oldCacheDir);
     }
+
+    protected function warmup($warmupDir)
+    {
+        $this->container->get('filesystem')->remove($warmupDir);
+
+        $kernel = $this->getTempKernel($this->container->get('kernel'), $warmupDir);
+        $kernel->boot();
+
+        $warmer = $kernel->getContainer()->get('cache_warmer');
+        $warmer->enableOptionalWarmers();
+        $warmer->warmUp($warmupDir);
+
+        // rename container files
+        $finder = new Finder();
+        foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
+            $content = file_get_contents($file);
+            $content = preg_replace('/__.*__/', '', $content);
+            file_put_contents(preg_replace('/__.*__/', '', $file), $content);
+            unlink($file);
+        }
+    }
+
+    protected function getTempKernel(KernelInterface $parent, $warmupDir)
+    {
+        $parentClass = get_class($parent);
+        $rand = uniqid();
+        $class = $parentClass.$rand;
+        $rootDir = $parent->getRootDir();
+        $code = <<<EOF
+<?php
+
+class $class extends $parentClass
+{
+    public function getCacheDir()
+    {
+        return '$warmupDir';
+    }
+
+    public function getRootDir()
+    {
+        return '$rootDir';
+    }
+
+    protected function getContainerClass()
+    {
+        return parent::getContainerClass().'__{$rand}__';
+    }
+}
+EOF;
+        $this->container->get('filesystem')->mkdirs($warmupDir);
+        file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
+        require_once $file;
+        @unlink($file);
+
+        return new $class($parent->getEnvironment(), $parent->isDebug());
+    }
 }

+ 2 - 42
src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php

@@ -12,22 +12,15 @@
 namespace Symfony\Bundle\FrameworkBundle\Command;
 
 use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\Finder\Finder;
 
 /**
  * Warmup the cache.
  *
  * @author Fabien Potencier <fabien@symfony.com>
- * @author Francis Besset <francis.besset@gmail.com>
  */
 class CacheWarmupCommand extends Command
 {
-    protected $warmupDir;
-    protected $kernelTmp;
-
     /**
      * @see Command
      */
@@ -36,9 +29,6 @@ class CacheWarmupCommand extends Command
         $this
             ->setName('cache:warmup')
             ->setDescription('Warms up an empty cache')
-            ->setDefinition(array(
-                new InputOption('warmup-dir', '', InputOption::VALUE_REQUIRED, 'Warms up the cache in the specified directory')
-            ))
             ->setHelp(<<<EOF
 The <info>cache:warmup</info> command warms up the cache.
 
@@ -55,38 +45,8 @@ EOF
     {
         $output->writeln('Warming up the cache');
 
-        if ($input->getOption('warmup-dir')) {
-            $this->setWarmupDir($input->getOption('warmup-dir'));
-        }
-
-        if (!$this->warmupDir) {
-            $this->warmUp($this->container);
-        } else {
-            $class = get_class($this->container->get('kernel'));
-            $this->kernelTmp = new $class(
-                $this->container->getParameter('kernel.environment'),
-                $this->container->getParameter('kernel.debug'),
-                $this->warmupDir
-            );
-
-            $this->container->get('filesystem')->remove($this->kernelTmp->getCacheDir());
-
-            $this->kernelTmp->boot();
-            unlink($this->kernelTmp->getCacheDir().DIRECTORY_SEPARATOR.$this->kernelTmp->getContainerClass().'.php');
-
-            $this->warmUp($this->kernelTmp->getContainer());
-        }
-    }
-
-    protected function warmUp(ContainerInterface $container)
-    {
-        $warmer = $container->get('cache_warmer');
+        $warmer = $this->container->get('cache_warmer');
         $warmer->enableOptionalWarmers();
-        $warmer->warmUp($container->getParameter('kernel.cache_dir'));
-    }
-
-    protected function setWarmupDir($dir)
-    {
-        $this->warmupDir = $dir;
+        $warmer->warmUp($this->container->getParameter('kernel.cache_dir'));
     }
 }

+ 0 - 9
src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmer.php

@@ -19,15 +19,6 @@ abstract class CacheWarmer implements CacheWarmerInterface
 {
     protected function writeCacheFile($file, $content)
     {
-        $dir = dirname($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(dirname($file), basename($file));
         if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
             chmod($file, 0644);

+ 4 - 19
src/Symfony/Component/HttpKernel/Kernel.php

@@ -44,7 +44,6 @@ abstract class Kernel implements KernelInterface
     protected $rootDir;
     protected $environment;
     protected $debug;
-    protected $cacheDir;
     protected $booted;
     protected $name;
     protected $startTime;
@@ -57,11 +56,10 @@ abstract class Kernel implements KernelInterface
      * @param string  $environment The environment
      * @param Boolean $debug       Whether to enable debugging or not
      */
-    public function __construct($environment, $debug, $cacheDir = null)
+    public function __construct($environment, $debug)
     {
         $this->environment = $environment;
         $this->debug = (Boolean) $debug;
-        $this->cacheDir = $cacheDir;
         $this->booted = false;
         $this->rootDir = $this->getRootDir();
         $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
@@ -337,7 +335,7 @@ abstract class Kernel implements KernelInterface
      */
     public function getCacheDir()
     {
-        return $this->rootDir.'/cache/'.($this->cacheDir ?: $this->environment);
+        return $this->rootDir.'/cache/'.$this->environment;
     }
 
     /**
@@ -414,9 +412,9 @@ abstract class Kernel implements KernelInterface
      *
      * @return string The container class
      */
-    public function getContainerClass()
+    protected function getContainerClass()
     {
-        return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'.($this->cacheDir ? 'Tmp' : '');
+        return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
     }
 
     /**
@@ -445,19 +443,6 @@ abstract class Kernel implements KernelInterface
         if (!$fresh && 'cli' !== php_sapi_name()) {
             $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
         }
-
-        if ($cacheDir = $this->cacheDir) {
-            $realCacheDir   = $this->getCacheDir();
-            $this->cacheDir = null;
-
-            $class = $this->getContainerClass();
-            $cache = new ConfigCache($realCacheDir, $class, $this->debug);
-
-            $container = $this->buildContainer();
-            $this->dumpContainer($cache, $container, $class);
-
-            $this->cacheDir = $cacheDir;
-        }
     }
 
     /**