Skip to content

[FrameworkBundle] fixes cahe:clear command's warmup #7360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 65 additions & 62 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
class CacheClearCommand extends ContainerAwareCommand
{
protected $name;

/**
* @see Command
*/
Expand Down Expand Up @@ -57,31 +55,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old';
$filesystem = $this->getContainer()->get('filesystem');

if (!is_writable($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)) {
$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')) {
$filesystem->rename($realCacheDir, $oldCacheDir);
} else {
$warmupDir = $realCacheDir.'_new';
// the warmup cache dir name must have the same length than the real one
// to avoid the many problems in serialized resources files
$warmupDir = substr($realCacheDir, 0, -1).'_';

if ($filesystem->exists($warmupDir)) {
$filesystem->remove($warmupDir);
}

$this->warmup($warmupDir, !$input->getOption('no-optional-warmers'));
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));

$filesystem->rename($realCacheDir, $oldCacheDir);
$filesystem->rename($warmupDir, $realCacheDir);
Expand All @@ -90,98 +89,102 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filesystem->remove($oldCacheDir);
}

protected function warmup($warmupDir, $enableOptionalWarmers = true)
/**
* @param string $warmupDir
* @param string $realCacheDir
* @param bool $enableOptionalWarmers
*/
protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true)
{
$this->getContainer()->get('filesystem')->remove($warmupDir);

$parent = $this->getContainer()->get('kernel');
$class = get_class($parent);
// create a temporary kernel
$realKernel = $this->getContainer()->get('kernel');
$realKernelClass = get_class($realKernel);
$namespace = '';
if (false !== $pos = strrpos($class, '\\')) {
$namespace = substr($class, 0, $pos);
$class = substr($class, $pos + 1);
if (false !== $pos = strrpos($realKernelClass, '\\')) {
$namespace = substr($realKernelClass, 0, $pos);
$realKernelClass = substr($realKernelClass, $pos + 1);
}
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
$tempKernel->boot();

$kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir);
$kernel->boot();

$warmer = $kernel->getContainer()->get('cache_warmer');

// warmup temporary dir
$warmer = $tempKernel->getContainer()->get('cache_warmer');
if ($enableOptionalWarmers) {
$warmer->enableOptionalWarmers();
}

$warmer->warmUp($warmupDir);

// fix references to the Kernel in .meta files
foreach (Finder::create()->files()->name('*.meta')->in($warmupDir) as $file) {
// fix meta references to the Kernel
$content = preg_replace(
'/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
sprintf('C:%s:"%s"', strlen($class), $class),
file_put_contents($file, preg_replace(
'/(C\:\d+\:)"'.get_class($tempKernel).'"/',
sprintf('$1"%s"', $realKernelClass),
file_get_contents($file)
);

// fix meta references to cache files
$realWarmupDir = substr($warmupDir, 0, -4);
$content = preg_replace_callback(
'/s\:\d+\:"'.preg_quote($warmupDir, '/').'([^"]+)"/',
function (array $matches) use ($realWarmupDir) {
$path = $realWarmupDir.$matches[1];
return sprintf('s:%s:"%s"', strlen($path), $path);
},
$content
);
));
}

// fix references to cached files with the real cache directory name
foreach (Finder::create()->files()->in($warmupDir) as $file) {
$content = str_replace($warmupDir, $realCacheDir, file_get_contents($file));
file_put_contents($file, $content);
}

// fix container files and classes
$regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
foreach (Finder::create()->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);
// fix references to kernel/container related classes
$search = $tempKernel->getName().ucfirst($tempKernel->getEnvironment());
$replace = $realKernel->getName().ucfirst($realKernel->getEnvironment());
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);
}
}

protected function getTempKernelSuffix()
/**
* @deprecated to be removed in 2.3
*/
protected function getTempSuffix()
{
if (null === $this->name) {
$this->name = '__'.uniqid().'__';
}

return $this->name;
return '';
}

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();
// 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
<?php

namespace $namespace
{
class $class$suffix extends $class
class $class extends $parentClass
{
public function getCacheDir()
{
return '$warmupDir';
}

public function getRootDir()
public function getName()
{
return '$rootDir';
return '$name';
}

protected function getContainerClass()
public function getRootDir()
{
return parent::getContainerClass().'$suffix';
return '$rootDir';
}
}
}
Expand All @@ -190,7 +193,7 @@ protected function getContainerClass()
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
require_once $file;
@unlink($file);
$class = "$namespace\\$class$suffix";
$class = "$namespace\\$class";

return new $class($parent->getEnvironment(), $parent->isDebug());
}
Expand Down