Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7da6759

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 87a6f04 commit 7da6759

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* The `DebugHandlersListener` class has been marked as `final`
8+
* Add support for dumping the container in one file instead of many files
89

910
4.3.0
1011
-----
@@ -36,8 +37,8 @@ CHANGELOG
3637

3738
* deprecated `KernelInterface::getRootDir()` and the `kernel.root_dir` parameter
3839
* deprecated `KernelInterface::getName()` and the `kernel.name` parameter
39-
* deprecated the first and second constructor argument of `ConfigDataCollector`
40-
* deprecated `ConfigDataCollector::getApplicationName()`
40+
* deprecated the first and second constructor argument of `ConfigDataCollector`
41+
* deprecated `ConfigDataCollector::getApplicationName()`
4142
* deprecated `ConfigDataCollector::getApplicationVersion()`
4243

4344
4.1.0

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ protected function getContainerBaseClass()
479479
*/
480480
protected function initializeContainer()
481481
{
482+
$asFiles = $this->getKernelParameters()['container.dump_as_files'] ?? true;
482483
$class = $this->getContainerClass();
483484
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
484485
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
@@ -488,6 +489,13 @@ protected function initializeContainer()
488489
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
489490
$fresh = $oldContainer = false;
490491
try {
492+
if (!$asFiles) {
493+
require_once $cache->getPath();
494+
$this->container = new $class();
495+
$this->container->set('kernel', $this);
496+
497+
return;
498+
}
491499
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
492500
$this->container->set('kernel', $this);
493501
$oldContainer = $this->container;
@@ -557,7 +565,7 @@ protected function initializeContainer()
557565
}
558566
}
559567

560-
if (null === $oldContainer && file_exists($cache->getPath())) {
568+
if ($asFiles && null === $oldContainer && file_exists($cache->getPath())) {
561569
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
562570
try {
563571
$oldContainer = include $cache->getPath();
@@ -569,7 +577,12 @@ protected function initializeContainer()
569577
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
570578

571579
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
572-
$this->container = require $cache->getPath();
580+
if ($asFiles) {
581+
$this->container = require $cache->getPath();
582+
} else {
583+
require $cache->getPath();
584+
$this->container = new $class();
585+
}
573586
$this->container->set('kernel', $this);
574587

575588
if ($oldContainer && \get_class($this->container) !== $oldContainer->name) {
@@ -731,26 +744,32 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
731744
$dumper->setProxyDumper(new ProxyDumper());
732745
}
733746

747+
$asFiles = $container->hasParameter('container.dump_as_files') ? $container->getParameter('container.dump_as_files') : true;
748+
734749
$content = $dumper->dump([
735750
'class' => $class,
736751
'base_class' => $baseClass,
737752
'file' => $cache->getPath(),
738-
'as_files' => true,
753+
'as_files' => $asFiles,
739754
'debug' => $this->debug,
740755
'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
741756
]);
742757

743-
$rootCode = array_pop($content);
744-
$dir = \dirname($cache->getPath()).'/';
745-
$fs = new Filesystem();
758+
if ($asFiles) {
759+
$rootCode = array_pop($content);
760+
$dir = \dirname($cache->getPath()).'/';
761+
$fs = new Filesystem();
746762

747-
foreach ($content as $file => $code) {
748-
$fs->dumpFile($dir.$file, $code);
749-
@chmod($dir.$file, 0666 & ~umask());
750-
}
751-
$legacyFile = \dirname($dir.$file).'.legacy';
752-
if (file_exists($legacyFile)) {
753-
@unlink($legacyFile);
763+
foreach ($content as $file => $code) {
764+
$fs->dumpFile($dir.$file, $code);
765+
@chmod($dir.$file, 0666 & ~umask());
766+
}
767+
$legacyFile = \dirname($dir.$file).'.legacy';
768+
if (file_exists($legacyFile)) {
769+
@unlink($legacyFile);
770+
}
771+
} else {
772+
$rootCode = $content;
754773
}
755774

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

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public function testInitializeContainerClearsOldContainers()
103103
$this->assertFileNotExists($legacyContainerDir.'.legacy');
104104
}
105105

106+
public function testKernelWithOneFileContainer()
107+
{
108+
$kernel = new KernelWithOneFileContainer('dev', true);
109+
$kernel->boot();
110+
111+
$this->assertNotNull($kernel->getContainer());
112+
113+
$kernel->shutdown();
114+
$kernel->boot();
115+
$this->assertNotNull($kernel->getContainer());
116+
}
117+
106118
public function testBootInitializesBundlesAndContainer()
107119
{
108120
$kernel = $this->getKernel(['initializeBundles', 'initializeContainer']);
@@ -769,3 +781,29 @@ public function process(ContainerBuilder $container)
769781
$container->setParameter('test.processed', true);
770782
}
771783
}
784+
785+
class KernelWithOneFileContainer extends Kernel
786+
{
787+
public function registerBundles()
788+
{
789+
return [];
790+
}
791+
792+
public function registerContainerConfiguration(LoaderInterface $loader)
793+
{
794+
}
795+
796+
public function getProjectDir()
797+
{
798+
return __DIR__.'/Fixtures';
799+
}
800+
801+
protected function getKernelParameters()
802+
{
803+
return array_merge(parent::getKernelParameters(), [
804+
'container.build_id' => 'static',
805+
'container.build_hash' => 'static',
806+
'container.dump_as_files' => false,
807+
]);
808+
}
809+
}

0 commit comments

Comments
 (0)