8000 bug #23873 [HttpKernel] Remove old container files (nicolas-grekas) · symfony/symfony@ff62871 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff62871

Browse files
bug #23873 [HttpKernel] Remove old container files (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Remove old container files | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23872 | License | MIT | Doc PR | - ping @mvrhov Commits ------- 2e4e6ad [HttpKernel] Remove old container files
2 parents aef502b + 2e4e6ad commit ff62871

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,11 @@ protected function initializeContainer()
584584
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
585585
}
586586
}
587+
588+
if ($oldContainer = file_exists($cache->getPath()) ? @include $cache->getPath() : false) {
589+
$oldContainer = new \ReflectionClass($oldContainer);
590+
}
591+
587592
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
588593

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

595-
if (!$fresh && $this->container->has('cache_warmer')) {
600+
if ($fresh) {
601+
return;
602+
}
603+
604+
if ($oldContainer && get_class($this->container) !== $oldContainer->name) {
605+
(new Filesystem())->remove(dirname($oldContainer->getFileName()));
606+
}
607+
608+
if ($this->container->has('cache_warmer')) {
596609
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
597610
}
598611
}
@@ -773,6 +786,9 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
773786
@chmod($dir.$file, 0666 & ~umask());
774787
}
775788

789+
// track changes made to the container directory
790+
$container->fileExists(dirname($dir.$file));
791+
776792
$cache->write($rootCode, $container->getResources());
777793
}
778794

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,38 @@ public function testSymfonyEnvironmentVariables()
771771

772772
public function testProjectDirExtension()
773773
{
774-
$kernel = new CustomProjectDirKernel('test', true);
774+
$kernel = new CustomProjectDirKernel();
775775
$kernel->boot();
776776

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

781+
public function testKernelReset()
782+
{
783+
(new Filesystem())->remove(__DIR__.'/Fixtures/cache');
784+
785+
$kernel = new CustomProjectDirKernel();
786+
$kernel->boot();
787+
788+
$containerClass = get_class($kernel->getContainer());
789+
$containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
790+
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
791+
792+
$kernel = new CustomProjectDirKernel();
793+
$kernel->boot();
794+
795+
$this->assertSame($containerClass, get_class($kernel->getContainer()));
796+
$this->assertFileExists($containerFile);
797+
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
798+
799+
$kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass'); });
800+
$kernel->boot();
801+
802+
$this->assertTrue(get_class($kernel->getContainer()) !== $containerClass);
803+
$this->assertFileNotExists($containerFile);
804+
}
805+
781806
/**
782807
* Returns a mock for the BundleInterface.
783808
*
@@ -878,12 +903,14 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
878903
class CustomProjectDirKernel extends Kernel
879904
{
880905
private $baseDir;
906+
private $buildContainer;
881907

882-
public function __construct()
908+
public function __construct(\Closure $buildContainer = null)
883909
{
884-
parent::__construct('test', false);
910+
parent::__construct('custom', true);
885911

886912
$this->baseDir = 'foo';
913+
$this->buildContainer = $buildContainer;
887914
}
888915

889916
public function registerBundles()
@@ -904,4 +931,11 @@ public function getRootDir()
904931
{
905932
return __DIR__.'/Fixtures';
906933
}
934+
935+
protected function build(ContainerBuilder $container)
936+
{
937+
if ($build = $this->buildContainer) {
938+
$build($container);
939+
}
940+
}
907941
}

0 commit comments

Comments
 (0)
0