8000 [DI] Allow dumping the container in one file instead of many files · symfony/symfony@c95a05a · GitHub
[go: up one dir, main page]

Skip to content

Commit c95a05a

Browse files
lyrixxnicolas-grekas
authored andcommitted
[DI] Allow dumping the container in one file instead of many files
1 parent 52e9fb9 commit c95a05a

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* added support for dumping the container in one file instead of many files
78
* deprecated support for short factories and short configurators in Yaml
89
* deprecated `tagged` in favor of `tagged_iterator`
910
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class PhpDumper extends Dumper
7272
private $namespace;
7373
private $asFiles;
7474
private $hotPathTag;
75+
private $inlineFactories;
7576
private $inlineRequires;
7677
private $inlinedRequires = [];
7778
private $circularReferences = [];
@@ -134,6 +135,7 @@ public function dump(array $options = [])
134135
'as_files' => false,
135136
'debug' => true,
136137
'hot_path_tag' => 'container.hot_path',
138+
'inline_factories_parameter' => 'container.dumper.inline_factories',
137139
'inline_class_loader_parameter' => 'container.dumper.inline_class_loader',
138140
'service_locator_tag' => 'container.service_locator',
139141
'build_time' => time(),
@@ -143,6 +145,7 @@ public function dump(array $options = [])
143145
$this->namespace = $options['namespace'];
144146
$this->asFiles = $options['as_files'];
145147
$this->hotPathTag = $options['hot_path_tag'];
148+
$this->inlineFactories = $options['inline_factories_parameter'] && $this->container->hasParameter($options['inline_factories_parameter']) && $this->container->getParameter($options['inline_factories_parameter']);
146149
$this->inlineRequires = $options['inline_class_loader_parameter'] && $this->container->hasParameter($options['inline_class_loader_parameter']) && $this->container->getParameter($options['inline_class_loader_parameter']);
147150
$this->serviceLocatorTag = $options['service_locator_tag'];
148151

@@ -257,14 +260,24 @@ public function dump(array $options = [])
257260
}
258261
$files['removed-ids.php'] = $c .= "];\n";
259262
}
260-
261-
foreach ($this->generateServiceFiles($services) as $file => $c) {
262-
$files[$file] = $fileStart.$c;
263+
if (!$this->inlineFactories) {
264+
foreach ($this->generateServiceFiles($services) as $file => $c) {
265+
$files[$file] = $fileStart.$c;
266+
}
267+
foreach ($this->generateProxyClasses() as $file => $c) {
268+
$files[$file] = "<?php\n".$c;
269+
}
263270
}
264-
foreach ($this->generateProxyClasses() as $file => $c) {
265-
$files[$file] = "<?php\n".$c;
271+
272+
$code .= $this->endClass();
273+
274+
if ($this->inlineFactories) {
275+
foreach ($this->generateProxyClasses() as $c) {
276+
$code .= $c;
277+
}
266278
}
267-
$files[$options['class'].'.php'] = $code.$this->endClass();
279+
280+
$files[$options['class'].'.php'] = $code;
268281
$hash = ucfirst(strtr(ContainerBuilder::hash($files), '._', 'xx'));
269282
$code = [];
270283

@@ -685,7 +698,7 @@ private function addService(string $id, Definition $definition): array
685698
$lazyInitialization = '';
686699
}
687700

688-
$asFile = $this->asFiles && !$this->isHotPath($definition);
701+
$asFile = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition);
689702
$methodName = $this->generateMethodName($id);
690703
if ($asFile) {
691704
$file = $methodName.'.php';
@@ -1140,7 +1153,7 @@ private function addMethodMap(): string
11401153
$definitions = $this->container->getDefinitions();
11411154
ksort($definitions);
11421155
foreach ($definitions as $id => $definition) {
1143-
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || $this->isHotPath($definition))) {
1156+
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || $this->inlineFactories || $this->isHotPath($definition))) {
11441157
$code .= ' '.$this->doExport($id).' => '.$this->doExport($this->generateMethodName($id)).",\n";
11451158
}
11461159
}
@@ -1163,7 +1176,7 @@ private function addFileMap(): string
11631176
$definitions = $this->container->getDefinitions();
11641177
ksort($definitions);
11651178
foreach ($definitions as $id => $definition) {
1166-
if (!$definition->isSynthetic() && $definition->isPublic() && !$this->isHotPath($definition)) {
1179+
if (!$definition->isSynthetic() && $definition->isPublic() && !$this->inlineFactories && !$this->isHotPath($definition)) {
11671180
$code .= sprintf(" %s => '%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
11681181
}
11691182
}
@@ -1578,7 +1591,7 @@ private function dumpValue($value, bool $interpolate = true): string
15781591
continue;
15791592
}
15801593
$definition = $this->container->findDefinition($id = (string) $v);
1581-
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->isHotPath($definition) : reset($e);
1594+
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : reset($e);
15821595
$serviceMap .= sprintf("\n %s => [%s, %s, %s, %s],",
15831596
$this->export($k),
15841597
$this->export($definition->isShared() ? ($definition->isPublic() ? 'services' : 'privates') : false),
@@ -1716,7 +1729,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
17161729
$code = sprintf('$this->%s[%s] = %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
17171730
}
17181731
$code = "($code)";
1719-
} elseif ($this->asFiles && !$this->isHotPath($definition)) {
1732+
} elseif ($this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition)) {
17201733
$code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
17211734
if (!$definition->isShared()) {
17221735
$factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']&q F438 uot;, $this->doExport($id));

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,23 +486,16 @@ protected function initializeContainer()
486486
if ($fresh = $cache->isFresh()) {
487487
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
488488
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
489-
$fresh = $oldContainer = false;
490489
try {
491490
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
492491
$this->container->set('kernel', $this);
493-
$oldContainer = $this->container;
494-
$fresh = true;
495492
}
496493
} catch (\Throwable $e) {
497494
} finally {
498495
error_reporting($errorLevel);
499496
}
500497
}
501498

502-
if ($fresh) {
503-
return;
504-
}
505-
506499
if ($this->debug) {
507500
$collectedLogs = [];
508501
$previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
@@ -557,7 +550,7 @@ protected function initializeContainer()
557550
}
558551
}
559552

560-
if (null === $oldContainer && file_exists($cache->getPath())) {
553+
if (!$fresh && file_exists($cache->getPath())) {
561554
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
562555
try {
563556
$oldContainer = include $cache->getPath();
@@ -566,7 +559,7 @@ protected function initializeContainer()
566559
error_reporting($errorLevel);
567560
}
568561
}
569-
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
562+
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : null;
570563

571564
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
572565
$this->container = require $cache->getPath();

0 commit comments

Comments
 (0)
0