8000 [Kernel+DIC] Fixed as_files behavior · symfony/symfony@32563e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32563e5

Browse files
committed
[Kernel+DIC] Fixed as_files behavior
Many issue about this: * Kernel does not handle when the dumper returns a string * Kernel does not handle when the compiled Container file does not return a new instance of itself * Kernel try to old load and clean old container, but if the container does not change its name, it tries to load 2 time the same file, and so the same class This patch fixed all theses case. Now we can use with ease a multifiles container or a simple unique file.
1 parent 081c601 commit 32563e5

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,25 @@ protected function getContainerBaseClass()
572572
*/
573573
protected function initializeContainer()
574574
{
575+
$asFiles = true;
575576
$class = $this->getContainerClass();
576577
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
577578
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
578579
$oldContainer = null;
579580
if ($fresh = $cache->isFresh()) {
581+
if (!$asFiles) {
582+
require_once $cache->getPath();
583+
$this->container = new $class();
584+
$this->container->set('kernel', $this);
585+
586+
return;
587+
}
580588
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
581589
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
582590
$fresh = $oldContainer = false;
583591
try {
584592
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
585593
$this->container->set('kernel', $this);
586-
$oldContainer = $this->container;
587594
$fresh = true;
588595
}
589596
} catch (\Throwable $e) {
@@ -634,6 +641,10 @@ protected function initializeContainer()
634641
try {
635642
$container = null;
636643
$container = $this->buildContainer();
644+
$container->getParameterBag()->add([
645+
'container.build_id' => 'static',
646+
'container.as_files' => $asFiles,
647+
]);
637648
$container->compile();
638649
} finally {
639650
if ($this->debug && true !== $previousHandler) {
@@ -657,7 +668,12 @@ protected function initializeContainer()
657668
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
658669

659670
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
660-
$this->container = require $cache->getPath();
671+
if ($asFiles) {
672+
$this->container = require $cache->getPath();
673+
} else {
674+
require $cache->getPath();
675+
$this->container = new $class();
676+
}
661677
$this->container->set('kernel', $this);
662678

663679
if ($oldContainer && \get_class($this->container) !== $oldContainer->name) {
@@ -844,27 +860,33 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
844860
$dumper->setProxyDumper(new ProxyDumper());
845861
}
846862

863+
$asFiles = $container->hasParameter('container.as_files') ? $container->getParameter('container.as_files') : true;
864+
847865
$content = $dumper->dump([
848866
'class' => $class,
849867
'base_class' => $baseClass,
850868
'file' => $cache->getPath(),
851-
'as_files' => true,
869+
'as_files' => $asFiles,
852870
'debug' => $this->debug,
853871
'inline_class_loader_parameter' => \PHP_VERSION_ID >= 70000 && !$this->loadClassCache && !class_exists(ClassCollectionLoader::class, false) ? 'container.dumper.inline_class_loader' : null,
854872
'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
855873
]);
856874

857-
$rootCode = array_pop($content);
858-
$dir = \dirname($cache->getPath()).'/';
859-
$fs = new Filesystem();
875+
if ($asFiles) {
876+
$rootCode = array_pop($content);
877+
$dir = \dirname($cache->getPath()).'/';
878+
$fs = new Filesystem();
860879

861-
foreach ($content as $file => $code) {
862-
$fs->dumpFile($dir.$file, $code);
863-
@chmod($dir.$file, 0666 & ~umask());
864-
}
865-
$legacyFile = \dirname($dir.$file).'.legacy';
866-
if (file_exists($legacyFile)) {
867-
@unlink($legacyFile);
880+
foreach ($content as $file => $code) {
881+< 7765 /span>
$fs->dumpFile($dir.$file, $code);
882+
@chmod($dir.$file, 0666 & ~umask());
883+
}
884+
$legacyFile = \dirname($dir.$file).'.legacy';
885+
if (file_exists($legacyFile)) {
886+
@unlink($legacyFile);
887+
}
888+
} else {
889+
$rootCode = $content;
868890
}
869891

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

0 commit comments

Comments
 (0)
0