Skip to content

Commit ce36821

Browse files
committed
bug symfony#12137 [FrameworkBundle] cache:clear command fills *.php.meta files with wrong data (Strate)
This PR was submitted for the 2.5 branch but it was merged into the 2.3 branch instead (closes symfony#12137). Discussion ---------- [FrameworkBundle] cache:clear command fills *.php.meta files with wrong data | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#12110 | License | MIT Test and fix of ticket symfony#12110 Commits ------- 76273bf [FrameworkBundle] cache:clear command fills *.php.meta files with wrong data
2 parents 580de75 + 76273bf commit ce36821

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
112112
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
113113
$tempKernel->boot();
114114

115+
$tempKernelReflection = new \ReflectionObject($tempKernel);
116+
$tempKernelFile = $tempKernelReflection->getFileName();
117+
115118
// warmup temporary dir
116119
$warmer = $tempKernel->getContainer()->get('cache_warmer');
117120
if ($enableOptionalWarmers) {
@@ -147,6 +150,9 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
147150
file_put_contents(str_replace($search, $replace, $file), $content);
148151
unlink($file);
149152
}
153+
154+
// remove temp kernel file after cache warmed up
155+
@unlink($tempKernelFile);
150156
}
151157

152158
/**
@@ -186,13 +192,30 @@ public function getRootDir()
186192
{
187193
return '$rootDir';
188194
}
195+
196+
protected function buildContainer()
197+
{
198+
\$container = parent::buildContainer();
199+
200+
// filter container's resources, removing reference to temp kernel file
201+
\$resources = \$container->getResources();
202+
\$filteredResources = array();
203+
foreach (\$resources as \$resource) {
204+
if ((string) \$resource !== __FILE__) {
205+
\$filteredResources[] = \$resource;
206+
}
207+
}
208+
209+
\$container->setResources(\$filteredResources);
210+
211+
return \$container;
212+
}
189213
}
190214
}
191215
EOF;
192216
$this->getContainer()->get('filesystem')->mkdir($warmupDir);
193217
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
194218
require_once $file;
195-
@unlink($file);
196219
$class = "$namespace\\$class";
197220

198221
return new $class($parent->getEnvironment(), $parent->isDebug());
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand;
4+
5+
use Symfony\Bundle\FrameworkBundle\Console\Application;
6+
use Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture\TestAppKernel;
7+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
8+
use Symfony\Component\Config\ConfigCache;
9+
use Symfony\Component\Config\Resource\ResourceInterface;
10+
use Symfony\Component\Console\Input\ArrayInput;
11+
use Symfony\Component\Console\Output\NullOutput;
12+
use Symfony\Component\Filesystem\Filesystem;
13+
use Symfony\Component\Finder\Finder;
14+
15+
class CacheClearCommandTest extends TestCase
16+
{
17+
/** @var TestAppKernel */
18+
private $kernel;
19+
/** @var Filesystem */
20+
private $fs;
21+
private $rootDir;
22+
23+
protected function setUp()
24+
{
25+
$this->fs = new Filesystem();
26+
$this->kernel = new TestAppKernel('test', true);
27+
$this->rootDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.uniqid('sf2_cache_');
28+
$this->kernel->setRootDir($this->rootDir);
29+
$this->fs->mkdir($this->rootDir);
30+
}
31+
32+
protected function tearDown()
33+
{
34+
$this->fs->remove($this->rootDir);
35+
}
36+
37+
public function testCacheIsFreshAfterCacheClearedWithWarmup()
38+
{
39+
$input = new ArrayInput(array('cache:clear'));
40+
$application = new Application($this->kernel);
41+
$application->setCatchExceptions(false);
42+
43+
$application->doRun($input, new NullOutput());
44+
45+
// Ensure that all *.meta files are fresh
46+
$finder = new Finder();
47+
$metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
48+
// simply check that cache is warmed up
49+
$this->assertGreaterThanOrEqual(1, count($metaFiles));
50+
foreach ($metaFiles as $file) {
51+
$configCache = new ConfigCache(substr($file, 0, -5), true);
52+
$this->assertTrue(
53+
$configCache->isFresh(),
54+
sprintf(
55+
'Meta file "%s" is not fresh',
56+
(string) $file
57+
)
58+
);
59+
}
60+
61+
// check that app kernel file present in meta file of container's cache
62+
$containerRef = new \ReflectionObject($this->kernel->getContainer());
63+
$containerFile = $containerRef->getFileName();
64+
$containerMetaFile = $containerFile.'.meta';
65+
$kernelRef = new \ReflectionObject($this->kernel);
66+
$kernelFile = $kernelRef->getFileName();
67+
/** @var ResourceInterface[] $meta */
68+
$meta = unserialize(file_get_contents($containerMetaFile));
69+
$found = false;
70+
foreach ($meta as $resource) {
71+
if ((string) $resource === $kernelFile) {
72+
$found = true;
73+
break;
74+
}
75+
}
76+
$this->assertTrue($found, 'Kernel file should present as resource');
77+
}
78+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
6+
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\HttpKernel\Kernel;
8+
9+
class TestAppKernel extends Kernel
10+
{
11+
public function registerBundles()
12+
{
13+
return array(
14+
new FrameworkBundle(),
15+
);
16+
}
17+
18+
public function setRootDir($rootDir)
19+
{
20+
$this->rootDir = $rootDir;
21+
}
22+
23+
public function registerContainerConfiguration(LoaderInterface $loader)
24+
{
25+
$loader->load(__DIR__.DIRECTORY_SEPARATOR.'config.yml');
26+
}
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
secret: test

0 commit comments

Comments
 (0)