[FrameworkBundle] changed temp kernel name in cache:clear

This commit is contained in:
Jean-François Simon 2013-03-14 11:46:34 +01:00
parent 7d87ecd346
commit cc3a40ed78
1 changed files with 51 additions and 46 deletions

View File

@ -25,8 +25,6 @@ use Symfony\Component\Finder\Finder;
*/ */
class CacheClearCommand extends ContainerAwareCommand class CacheClearCommand extends ContainerAwareCommand
{ {
protected $name;
/** /**
* @see Command * @see Command
*/ */
@ -57,25 +55,24 @@ EOF
{ {
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir'); $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old'; $oldCacheDir = $realCacheDir.'_old';
$filesystem = $this->getContainer()->get('filesystem');
if (!is_writable($realCacheDir)) { if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir)); throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
} }
$filesystem = $this->getContainer()->get('filesystem');
$kernel = $this->getContainer()->get('kernel');
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
if ($filesystem->exists($oldCacheDir)) { if ($filesystem->exists($oldCacheDir)) {
$filesystem->remove($oldCacheDir); $filesystem->remove($oldCacheDir);
} }
$kernel = $this->getContainer()->get('kernel');
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
if ($input->getOption('no-warmup')) { if ($input->getOption('no-warmup')) {
$filesystem->rename($realCacheDir, $oldCacheDir); $filesystem->rename($realCacheDir, $oldCacheDir);
} else { } else {
// the warmup cache dir name must have the have length than the real one // the warmup cache dir name must have the same length than the real one
// to avoid the many problems in serialized resources files // to avoid the many problems in serialized resources files
$warmupDir = substr($realCacheDir, 0, -1).'_'; $warmupDir = substr($realCacheDir, 0, -1).'_';
@ -92,87 +89,95 @@ EOF
$filesystem->remove($oldCacheDir); $filesystem->remove($oldCacheDir);
} }
/**
* @param string $warmupDir
* @param string $realCacheDir
* @param bool $enableOptionalWarmers
*/
protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true) protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true)
{ {
$this->getContainer()->get('filesystem')->remove($warmupDir); $this->getContainer()->get('filesystem')->remove($warmupDir);
$parent = $this->getContainer()->get('kernel'); // create a temporary kernel
$class = get_class($parent); $realKernel = $this->getContainer()->get('kernel');
$realKernelClass = get_class($realKernel);
$namespace = ''; $namespace = '';
if (false !== $pos = strrpos($class, '\\')) { if (false !== $pos = strrpos($realKernelClass, '\\')) {
$namespace = substr($class, 0, $pos); $namespace = substr($realKernelClass, 0, $pos);
$class = substr($class, $pos + 1); $realKernelClass = substr($realKernelClass, $pos + 1);
} }
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
$tempKernel->boot();
$kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir); // warmup temporary dir
$kernel->boot(); $warmer = $tempKernel->getContainer()->get('cache_warmer');
$warmer = $kernel->getContainer()->get('cache_warmer');
if ($enableOptionalWarmers) { if ($enableOptionalWarmers) {
$warmer->enableOptionalWarmers(); $warmer->enableOptionalWarmers();
} }
$warmer->warmUp($warmupDir); $warmer->warmUp($warmupDir);
// fix references to the Kernel in .meta files // fix references to the Kernel in .meta files
foreach (Finder::create()->files()->name('*.meta')->in($warmupDir) as $file) { foreach (Finder::create()->files()->name('*.meta')->in($warmupDir) as $file) {
file_put_contents($file, preg_replace( file_put_contents($file, preg_replace(
'/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/', '/(C\:\d+\:)"'.get_class($tempKernel).'"/',
sprintf('C:%s:"%s"', strlen($class), $class), sprintf('$1"%s"', $realKernelClass),
file_get_contents($file) file_get_contents($file)
)); ));
} }
// fix kernel class names in container-specific cache classes
// and rename those classes by removing temp suffix
foreach (Finder::create()->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
$content = str_replace($this->getTempKernelSuffix(), '', file_get_contents($file));
file_put_contents(str_replace($this->getTempKernelSuffix(), '', $file), $content);
unlink($file);
}
// fix references to cached files with the real cache directory name // fix references to cached files with the real cache directory name
foreach (Finder::create()->files()->in($warmupDir) as $file) { foreach (Finder::create()->files()->in($warmupDir) as $file) {
$content = str_replace($warmupDir, $realCacheDir, file_get_contents($file)); $content = str_replace($warmupDir, $realCacheDir, file_get_contents($file));
file_put_contents($file, $content); file_put_contents($file, $content);
} }
}
protected function getTempKernelSuffix() // fix references to kernel/container related classes
{ $search = $tempKernel->getName().ucfirst($tempKernel->getEnvironment());
if (null === $this->name) { $replace = $realKernel->getName().ucfirst($realKernel->getEnvironment());
$this->name = '__'.uniqid().'__'; foreach (Finder::create()->files()->name($search.'*')->in($warmupDir) as $file) {
$content = str_replace($search, $replace, file_get_contents($file));
file_put_contents(str_replace($search, $replace, $file), $content);
unlink($file);
} }
return $this->name;
} }
protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir) /**
* @param KernelInterface $parent
* @param string $namespace
* @param string $parentClass
* @param string $warmupDir
*
* @return KernelInterface
*/
protected function getTempKernel(KernelInterface $parent, $namespace, $parentClass, $warmupDir)
{ {
$suffix = $this->getTempKernelSuffix();
$rootDir = $parent->getRootDir(); $rootDir = $parent->getRootDir();
// the temp kernel class name must have the same length than the real one
// to avoid the many problems in serialized resources files
$class = substr($parentClass, 0, -1).'_';
// the temp kernel name must be changed too
$name = substr($parent->getName(), 0, -1).'_';
$code = <<<EOF $code = <<<EOF
<?php <?php
namespace $namespace namespace $namespace
{ {
class $class$suffix extends $class class $class extends $parentClass
{ {
public function getCacheDir() public function getCacheDir()
{ {
return '$warmupDir'; return '$warmupDir';
} }
public function getName()
{
return '$name';
}
public function getRootDir() public function getRootDir()
{ {
return '$rootDir'; return '$rootDir';
} }
protected function getContainerClass()
{
return parent::getContainerClass().'$suffix';
}
} }
} }
EOF; EOF;
@ -180,7 +185,7 @@ EOF;
file_put_contents($file = $warmupDir.'/kernel.tmp', $code); file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
require_once $file; require_once $file;
@unlink($file); @unlink($file);
$class = "$namespace\\$class$suffix"; $class = "$namespace\\$class";
return new $class($parent->getEnvironment(), $parent->isDebug()); return new $class($parent->getEnvironment(), $parent->isDebug());
} }