8000 [HttpKernel] Remove old container files by nicolas-grekas · Pull Request #23873 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Remove old container files #23873

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 1 commit into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ protected function initializeContainer()
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
}
}

if ($oldContainer = file_exists($cache->getPath()) ? @include $cache->getPath() : false) {
$oldContainer = new \ReflectionClass($oldContainer);
}

$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

$fresh = false;
Expand All @@ -592,7 +597,15 @@ protected function initializeContainer()
$this->container = require $cache->getPath();
$this->container->set('kernel', $this);

if (!$fresh && $this->container->has('cache_warmer')) {
if ($fresh) {
return;
}

if ($oldContainer && get_class($this->container) !== $oldContainer->name) {
(new Filesystem())->remove(dirname($oldContainer->getFileName()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this remove the whole cache dir ? This is not good, as it is not all about the DIC cache.

}

if ($this->container->has('cache_warmer')) {
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
}
}
Expand Down Expand Up @@ -773,6 +786,9 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
@chmod($dir.$file, 0666 & ~umask());
}

// track changes made to the container directory
$container->fileExists(dirname($dir.$file));

$cache->write($rootCode, $container->getResources());
}

Expand Down
40 changes: 37 additions & 3 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,38 @@ public function testSymfonyEnvironmentVariables()

public function testProjectDirExtension()
{
$kernel = new CustomProjectDirKernel('test', true);
$kernel = new CustomProjectDirKernel();
$kernel->boot();

$this->assertSame('foo', $kernel->getProjectDir());
$this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
}

public function testKernelReset()
{
(new Filesystem())->remove(__DIR__.'/Fixtures/cache');

$kernel = new CustomProjectDirKernel();
$kernel->boot();

$containerClass = get_class($kernel->getContainer());
$containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');

$kernel = new CustomProjectDirKernel();
$kernel->boot();

$this->assertSame($containerClass, get_class($kernel->getContainer()));
$this->assertFileExists($containerFile);
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');

$kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass'); });
$kernel->boot();

$this->assertTrue(get_class($kernel->getContainer()) !== $containerClass);
$this->assertFileNotExists($containerFile);
}

/**
* Returns a mock for the BundleInterface.
*
Expand Down Expand Up @@ -878,12 +903,14 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
class CustomProjectDirKernel extends Kernel
{
private $baseDir;
private $buildContainer;

public function __construct()
public function __construct(\Closure $buildContainer = null)
{
parent::__construct('test', false);
parent::__construct('custom', true);

$this->baseDir = 'foo';
$this->buildContainer = $buildContainer;
}

public function registerBundles()
Expand All @@ -904,4 +931,11 @@ public function getRootDir()
{
return __DIR__.'/Fixtures';
}

protected function build(ContainerBuilder $container)
{
if ($build = $this->buildContainer) {
$build($container);
}
}
}
0