diff -r 000000000000 -r 7f95f8617b0b vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php Sat Sep 24 15:40:41 2011 +0200 @@ -0,0 +1,170 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +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. + * + * @author Francis Besset + * @author Fabien Potencier + */ +class CacheClearCommand extends ContainerAwareCommand +{ + protected $name; + + /** + * @see Command + */ + protected function configure() + { + $this + ->setName('cache:clear') + ->setDefinition(array( + new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'), + )) + ->setDescription('Clear the cache') + ->setHelp(<<cache:clear command clears the application cache for a given environment +and debug mode: + +php app/console cache:clear --env=dev +php app/console cache:clear --env=prod --no-debug +EOF + ) + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir'); + $oldCacheDir = $realCacheDir.'_old'; + + if (!is_writable($realCacheDir)) { + throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir)); + } + + $kernel = $this->getContainer()->get('kernel'); + $output->writeln(sprintf('Clearing the cache for the %s environment with debug %s', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); + + if ($input->getOption('no-warmup')) { + rename($realCacheDir, $oldCacheDir); + } else { + $warmupDir = $realCacheDir.'_new'; + + $this->warmup($warmupDir); + + rename($realCacheDir, $oldCacheDir); + rename($warmupDir, $realCacheDir); + } + + $this->getContainer()->get('filesystem')->remove($oldCacheDir); + } + + protected function warmup($warmupDir) + { + $this->getContainer()->get('filesystem')->remove($warmupDir); + + $parent = $this->getContainer()->get('kernel'); + $class = get_class($parent); + $namespace = ''; + if (false !== $pos = strrpos($class, '\\')) { + $namespace = substr($class, 0, $pos); + $class = substr($class, $pos + 1); + } + + $kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir); + $kernel->boot(); + + $warmer = $kernel->getContainer()->get('cache_warmer'); + $warmer->enableOptionalWarmers(); + $warmer->warmUp($warmupDir); + + // fix container files and classes + $regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/'; + $finder = new Finder(); + foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) { + $content = file_get_contents($file); + $content = preg_replace($regex, '', $content); + + // fix absolute paths to the cache directory + $content = preg_replace('/'.preg_quote($warmupDir,'/').'/', preg_replace('/_new$/', '', $warmupDir), $content); + + file_put_contents(preg_replace($regex, '', $file), $content); + unlink($file); + } + + // fix meta references to the Kernel + foreach ($finder->files()->name('*.meta')->in($warmupDir) as $file) { + $content = preg_replace( + '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/', + sprintf('C:%s:"%s"', strlen($class), $class), + file_get_contents($file) + ); + file_put_contents($file, $content); + } + } + + protected function getTempKernelSuffix() + { + if (null === $this->name) { + $this->name = '__'.uniqid().'__'; + } + + return $this->name; + } + + protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir) + { + $suffix = $this->getTempKernelSuffix(); + $rootDir = $parent->getRootDir(); + $code = <<getContainer()->get('filesystem')->mkdir($warmupDir); + file_put_contents($file = $warmupDir.'/kernel.tmp', $code); + require_once $file; + @unlink($file); + $class = "$namespace\\$class$suffix"; + + return new $class($parent->getEnvironment(), $parent->isDebug()); + } +}