This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

144 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <francis.besset@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class CacheClearCommand extends Command
{
/**
* @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(<<<EOF
The <info>cache:clear</info> command clears the application cache for a given environment
and debug mode:
<info>./app/console cache:clear --env=dev</info>
<info>./app/console cache:clear --env=prod --no-debug</info>
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$realCacheDir = $this->container->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old';
if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
}
if ($input->getOption('no-warmup')) {
rename($realCacheDir, $oldCacheDir);
} else {
$warmupDir = $realCacheDir.'_new';
$this->warmup($warmupDir);
rename($realCacheDir, $oldCacheDir);
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);
// fix container files and classes
$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);
2011-03-22 18:46:08 +00:00
$namespace = '';
2011-03-22 18:46:08 +00:00
if (false !== $pos = strrpos($parentClass, '\\')) {
$namespace = substr($parentClass, 0, $pos);
$parentClass = substr($parentClass, $pos + 1);
}
2011-03-22 18:46:08 +00:00
$rand = uniqid();
$class = $parentClass.$rand;
$rootDir = $parent->getRootDir();
$code = <<<EOF
<?php
2011-03-22 18:46:08 +00:00
namespace $namespace
{
2011-03-22 18:46:08 +00:00
class $class extends $parentClass
{
2011-03-22 18:46:08 +00:00
public function getCacheDir()
{
return '$warmupDir';
}
2011-03-22 18:46:08 +00:00
public function getRootDir()
{
return '$rootDir';
}
2011-03-22 18:46:08 +00:00
protected function getContainerClass()
{
return parent::getContainerClass().'__{$rand}__';
}
}
}
EOF;
$this->container->get('filesystem')->mkdir($warmupDir);
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
require_once $file;
@unlink($file);
2011-04-01 09:28:09 +01:00
$class = "$namespace\\$class";
2011-03-22 18:46:08 +00:00
2011-06-08 14:20:37 +01:00
return new $class($parent->getEnvironment(), $parent->isDebug());
}
}