From 7d87ecd3465c38ee8f5d6c370cc4de22d80a6b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 13 Mar 2013 12:02:56 +0100 Subject: [PATCH 1/3] [FrameworkBundle] fixed cahe:clear command's warmup --- .../Command/CacheClearCommand.php | 46 ++++++++----------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 1fcfe589c53b5..c05cdf6b66e55 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -75,13 +75,15 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($input->getOption('no-warmup')) { $filesystem->rename($realCacheDir, $oldCacheDir); } else { - $warmupDir = $realCacheDir.'_new'; + // the warmup cache dir name must have the have 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); @@ -90,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $filesystem->remove($oldCacheDir); } - protected function warmup($warmupDir, $enableOptionalWarmers = true) + protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true) { $this->getContainer()->get('filesystem')->remove($warmupDir); @@ -113,40 +115,28 @@ protected function warmup($warmupDir, $enableOptionalWarmers = true) $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( + file_put_contents($file, preg_replace( '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/', sprintf('C:%s:"%s"', strlen($class), $class), 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 - ); - - file_put_contents($file, $content); + )); } - // fix container files and classes - $regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/'; + // 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 = 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); + $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 + foreach (Finder::create()->files()->in($warmupDir) as $file) { + $content = str_replace($warmupDir, $realCacheDir, file_get_contents($file)); + file_put_contents($file, $content); + } } protected function getTempKernelSuffix() From cc3a40ed7868d537fa2c59a6adf437795467ca5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Thu, 14 Mar 2013 11:46:34 +0100 Subject: [PATCH 2/3] [FrameworkBundle] changed temp kernel name in cache:clear --- .../Command/CacheClearCommand.php | 95 ++++++++++--------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index c05cdf6b66e55..4f78aeb671b74 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -25,8 +25,6 @@ */ class CacheClearCommand extends ContainerAwareCommand { - protected $name; - /** * @see Command */ @@ -57,25 +55,24 @@ 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 %s environment with debug %s', $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 %s environment with debug %s', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); + $this->getContainer()->get('cache_clearer')->clear($realCacheDir); + if ($input->getOption('no-warmup')) { $filesystem->rename($realCacheDir, $oldCacheDir); } 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 $warmupDir = substr($realCacheDir, 0, -1).'_'; @@ -92,86 +89,94 @@ protected function execute(InputInterface $input, OutputInterface $output) $filesystem->remove($oldCacheDir); } + /** + * @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) { file_put_contents($file, preg_replace( - '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/', - sprintf('C:%s:"%s"', strlen($class), $class), + '/(C\:\d+\:)"'.get_class($tempKernel).'"/', + sprintf('$1"%s"', $realKernelClass), 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 foreach (Finder::create()->files()->in($warmupDir) as $file) { $content = str_replace($warmupDir, $realCacheDir, file_get_contents($file)); file_put_contents($file, $content); } - } - protected function getTempKernelSuffix() - { - if (null === $this->name) { - $this->name = '__'.uniqid().'__'; + // 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); } - - 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(); + // 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 = <<getEnvironment(), $parent->isDebug()); } From f2ef6bc66b7758f220e1c397436b3ee203605d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Thu, 14 Mar 2013 12:23:30 +0100 Subject: [PATCH 3/3] [FrameworkBundle] removed BC break --- .../Bundle/FrameworkBundle/Command/CacheClearCommand.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 4f78aeb671b74..81491ba0bef5d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -141,6 +141,14 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr } } + /** + * @deprecated to be removed in 2.3 + */ + protected function getTempSuffix() + { + return ''; + } + /** * @param KernelInterface $parent * @param string $namespace