diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 0d2f8968d8..1fea1b91a4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -112,6 +112,9 @@ EOF $tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir); $tempKernel->boot(); + $tempKernelReflection = new \ReflectionObject($tempKernel); + $tempKernelFile = $tempKernelReflection->getFileName(); + // warmup temporary dir $warmer = $tempKernel->getContainer()->get('cache_warmer'); if ($enableOptionalWarmers) { @@ -147,6 +150,9 @@ EOF file_put_contents(str_replace($search, $replace, $file), $content); unlink($file); } + + // remove temp kernel file after cache warmed up + @unlink($tempKernelFile); } /** @@ -186,13 +192,30 @@ namespace $namespace { return '$rootDir'; } + + protected function buildContainer() + { + \$container = parent::buildContainer(); + + // filter container's resources, removing reference to temp kernel file + \$resources = \$container->getResources(); + \$filteredResources = array(); + foreach (\$resources as \$resource) { + if ((string) \$resource !== __FILE__) { + \$filteredResources[] = \$resource; + } + } + + \$container->setResources(\$filteredResources); + + return \$container; + } } } EOF; $this->getContainer()->get('filesystem')->mkdir($warmupDir); file_put_contents($file = $warmupDir.'/kernel.tmp', $code); require_once $file; - @unlink($file); $class = "$namespace\\$class"; return new $class($parent->getEnvironment(), $parent->isDebug()); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php new file mode 100644 index 0000000000..78ab561df1 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php @@ -0,0 +1,78 @@ +fs = new Filesystem(); + $this->kernel = new TestAppKernel('test', true); + $this->rootDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.uniqid('sf2_cache_'); + $this->kernel->setRootDir($this->rootDir); + $this->fs->mkdir($this->rootDir); + } + + protected function tearDown() + { + $this->fs->remove($this->rootDir); + } + + public function testCacheIsFreshAfterCacheClearedWithWarmup() + { + $input = new ArrayInput(array('cache:clear')); + $application = new Application($this->kernel); + $application->setCatchExceptions(false); + + $application->doRun($input, new NullOutput()); + + // Ensure that all *.meta files are fresh + $finder = new Finder(); + $metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta'); + // simply check that cache is warmed up + $this->assertGreaterThanOrEqual(1, count($metaFiles)); + foreach ($metaFiles as $file) { + $configCache = new ConfigCache(substr($file, 0, -5), true); + $this->assertTrue( + $configCache->isFresh(), + sprintf( + 'Meta file "%s" is not fresh', + (string) $file + ) + ); + } + + // check that app kernel file present in meta file of container's cache + $containerRef = new \ReflectionObject($this->kernel->getContainer()); + $containerFile = $containerRef->getFileName(); + $containerMetaFile = $containerFile.'.meta'; + $kernelRef = new \ReflectionObject($this->kernel); + $kernelFile = $kernelRef->getFileName(); + /** @var ResourceInterface[] $meta */ + $meta = unserialize(file_get_contents($containerMetaFile)); + $found = false; + foreach ($meta as $resource) { + if ((string) $resource === $kernelFile) { + $found = true; + break; + } + } + $this->assertTrue($found, 'Kernel file should present as resource'); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php new file mode 100644 index 0000000000..da835dfd4d --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php @@ -0,0 +1,27 @@ +rootDir = $rootDir; + } + + public function registerContainerConfiguration(LoaderInterface $loader) + { + $loader->load(__DIR__.DIRECTORY_SEPARATOR.'config.yml'); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/config.yml new file mode 100644 index 0000000000..68f8d04061 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/config.yml @@ -0,0 +1,2 @@ +framework: + secret: test