Skip to content

Commit

Permalink
[FrameworkBundle] cache:clear command fills *.php.meta files with wro…
Browse files Browse the repository at this point in the history
…ng data
  • Loading branch information
Strate authored and fabpot committed Nov 21, 2014
1 parent 580de75 commit 76273bf
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Expand Up @@ -112,6 +112,9 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
$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) {
Expand Down Expand Up @@ -147,6 +150,9 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
file_put_contents(str_replace($search, $replace, $file), $content);
unlink($file);
}

// remove temp kernel file after cache warmed up
@unlink($tempKernelFile);
}

/**
Expand Down Expand Up @@ -186,13 +192,30 @@ public function getRootDir()
{
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());
Expand Down
@@ -0,0 +1,78 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture\TestAppKernel;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class CacheClearCommandTest extends TestCase
{
/** @var TestAppKernel */
private $kernel;
/** @var Filesystem */
private $fs;
private $rootDir;

protected function setUp()
{
$this->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');
}
}
@@ -0,0 +1,27 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class TestAppKernel extends Kernel
{
public function registerBundles()
{
return array(
new FrameworkBundle(),
);
}

public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.DIRECTORY_SEPARATOR.'config.yml');
}
}
@@ -0,0 +1,2 @@
framework:
secret: test

0 comments on commit 76273bf

Please sign in to comment.