vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle\Command;
       
    13 
       
    14 use Symfony\Component\Console\Input\InputInterface;
       
    15 use Symfony\Component\Console\Input\InputOption;
       
    16 use Symfony\Component\Console\Output\OutputInterface;
       
    17 use Symfony\Component\HttpKernel\KernelInterface;
       
    18 use Symfony\Component\Finder\Finder;
       
    19 
       
    20 /**
       
    21  * Clear and Warmup the cache.
       
    22  *
       
    23  * @author Francis Besset <francis.besset@gmail.com>
       
    24  * @author Fabien Potencier <fabien@symfony.com>
       
    25  */
       
    26 class CacheClearCommand extends ContainerAwareCommand
       
    27 {
       
    28     protected $name;
       
    29 
       
    30     /**
       
    31      * @see Command
       
    32      */
       
    33     protected function configure()
       
    34     {
       
    35         $this
       
    36             ->setName('cache:clear')
       
    37             ->setDefinition(array(
       
    38                 new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'),
       
    39             ))
       
    40             ->setDescription('Clear the cache')
       
    41             ->setHelp(<<<EOF
       
    42 The <info>cache:clear</info> command clears the application cache for a given environment
       
    43 and debug mode:
       
    44 
       
    45 <info>php app/console cache:clear --env=dev</info>
       
    46 <info>php app/console cache:clear --env=prod --no-debug</info>
       
    47 EOF
       
    48             )
       
    49         ;
       
    50     }
       
    51 
       
    52     /**
       
    53      * {@inheritdoc}
       
    54      */
       
    55     protected function execute(InputInterface $input, OutputInterface $output)
       
    56     {
       
    57         $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
       
    58         $oldCacheDir  = $realCacheDir.'_old';
       
    59 
       
    60         if (!is_writable($realCacheDir)) {
       
    61             throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
       
    62         }
       
    63 
       
    64         $kernel = $this->getContainer()->get('kernel');
       
    65         $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
       
    66 
       
    67         if ($input->getOption('no-warmup')) {
       
    68             rename($realCacheDir, $oldCacheDir);
       
    69         } else {
       
    70             $warmupDir = $realCacheDir.'_new';
       
    71 
       
    72             $this->warmup($warmupDir);
       
    73 
       
    74             rename($realCacheDir, $oldCacheDir);
       
    75             rename($warmupDir, $realCacheDir);
       
    76         }
       
    77 
       
    78         $this->getContainer()->get('filesystem')->remove($oldCacheDir);
       
    79     }
       
    80 
       
    81     protected function warmup($warmupDir)
       
    82     {
       
    83         $this->getContainer()->get('filesystem')->remove($warmupDir);
       
    84 
       
    85         $parent = $this->getContainer()->get('kernel');
       
    86         $class = get_class($parent);
       
    87         $namespace = '';
       
    88         if (false !== $pos = strrpos($class, '\\')) {
       
    89             $namespace = substr($class, 0, $pos);
       
    90             $class = substr($class, $pos + 1);
       
    91         }
       
    92 
       
    93         $kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir);
       
    94         $kernel->boot();
       
    95 
       
    96         $warmer = $kernel->getContainer()->get('cache_warmer');
       
    97         $warmer->enableOptionalWarmers();
       
    98         $warmer->warmUp($warmupDir);
       
    99 
       
   100         // fix container files and classes
       
   101         $regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
       
   102         $finder = new Finder();
       
   103         foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
       
   104             $content = file_get_contents($file);
       
   105             $content = preg_replace($regex, '', $content);
       
   106 
       
   107             // fix absolute paths to the cache directory
       
   108             $content = preg_replace('/'.preg_quote($warmupDir,'/').'/', preg_replace('/_new$/', '', $warmupDir), $content);
       
   109 
       
   110             file_put_contents(preg_replace($regex, '', $file), $content);
       
   111             unlink($file);
       
   112         }
       
   113 
       
   114         // fix meta references to the Kernel
       
   115         foreach ($finder->files()->name('*.meta')->in($warmupDir) as $file) {
       
   116             $content = preg_replace(
       
   117                 '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
       
   118                 sprintf('C:%s:"%s"', strlen($class), $class),
       
   119                 file_get_contents($file)
       
   120             );
       
   121             file_put_contents($file, $content);
       
   122         }
       
   123     }
       
   124 
       
   125     protected function getTempKernelSuffix()
       
   126     {
       
   127         if (null === $this->name) {
       
   128             $this->name = '__'.uniqid().'__';
       
   129         }
       
   130 
       
   131         return $this->name;
       
   132     }
       
   133 
       
   134     protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir)
       
   135     {
       
   136         $suffix = $this->getTempKernelSuffix();
       
   137         $rootDir = $parent->getRootDir();
       
   138         $code = <<<EOF
       
   139 <?php
       
   140 
       
   141 namespace $namespace
       
   142 {
       
   143     class $class$suffix extends $class
       
   144     {
       
   145         public function getCacheDir()
       
   146         {
       
   147             return '$warmupDir';
       
   148         }
       
   149 
       
   150         public function getRootDir()
       
   151         {
       
   152             return '$rootDir';
       
   153         }
       
   154 
       
   155         protected function getContainerClass()
       
   156         {
       
   157             return parent::getContainerClass().'$suffix';
       
   158         }
       
   159     }
       
   160 }
       
   161 EOF;
       
   162         $this->getContainer()->get('filesystem')->mkdir($warmupDir);
       
   163         file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
       
   164         require_once $file;
       
   165         @unlink($file);
       
   166         $class = "$namespace\\$class$suffix";
       
   167 
       
   168         return new $class($parent->getEnvironment(), $parent->isDebug());
       
   169     }
       
   170 }