Skip to content

Commit 6a65742

Browse files
committed
[DependencyInjection] Always preload services classes tagged with container.preload
1 parent 75e71e3 commit 6a65742

File tree

11 files changed

+260
-107
lines changed

11 files changed

+260
-107
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
584584
$lineage = [];
585585
foreach ($this->inlinedDefinitions as $def) {
586586
if (!$def->isDeprecated()) {
587-
foreach ($this->getClasses($def, $cId) as $class) {
587+
foreach ($this->getClasses($def) as $class) {
588588
$this->collectLineage($class, $lineage);
589589
}
590590
}
@@ -596,7 +596,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
596596
&& $this->container->has($id)
597597
&& $this->isTrivialInstance($def = $this->container->findDefinition($id))
598598
) {
599-
foreach ($this->getClasses($def, $cId) as $class) {
599+
foreach ($this->getClasses($def) as $class) {
600600
$this->collectLineage($class, $lineage);
601601
}
602602
}
@@ -851,9 +851,9 @@ protected function {$methodName}($lazyInitialization)
851851
if ($definition->isDeprecated()) {
852852
$deprecation = $definition->getDeprecation($id);
853853
$code .= sprintf(" trigger_deprecation(%s, %s, %s);\n\n", $this->export($deprecation['package']), $this->export($deprecation['version']), $this->export($deprecation['message']));
854-
} elseif (!$definition->hasTag($this->preloadTags[1])) {
854+
} elseif ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) {
855855
foreach ($this->inlinedDefinitions as $def) {
856-
foreach ($this->getClasses($def, $id) as $class) {
856+
foreach ($this->getClasses($def) as $class) {
857857
$this->preload[$class] = $class;
858858
}
859859
}
@@ -1017,10 +1017,10 @@ private function addServices(array &$services = null): string
10171017
foreach ($definitions as $id => $definition) {
10181018
if (!$definition->isSynthetic()) {
10191019
$services[$id] = $this->addService($id, $definition);
1020-
} elseif (!$definition->hasTag($this->preloadTags[1])) {
1020+
} elseif ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) {
10211021
$services[$id] = null;
10221022

1023-
foreach ($this->getClasses($definition, $id) as $class) {
1023+
foreach ($this->getClasses($definition) as $class) {
10241024
$this->preload[$class] = $class;
10251025
}
10261026
}
@@ -1046,7 +1046,7 @@ private function generateServiceFiles(array $services): iterable
10461046
ksort($definitions);
10471047
foreach ($definitions as $id => $definition) {
10481048
if ((list($file, $code) = $services[$id]) && null !== $file && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) {
1049-
yield $file => [$code, !$definition->hasTag($this->preloadTags[1]) && !$definition->isDeprecated() && !$definition->hasErrors()];
1049+
yield $file => [$code, ($definition->hasTag($this->preloadTags[0]) || !$definition->hasTag($this->preloadTags[1])) && !$definition->isDeprecated() && !$definition->hasErrors()];
10501050
}
10511051
}
10521052
}
@@ -1399,7 +1399,7 @@ private function addInlineRequires(): string
13991399
$inlinedDefinitions = $this->getDefinitionsFromArguments([$definition]);
14001400

14011401
foreach ($inlinedDefinitions as $def) {
1402-
foreach ($this->getClasses($def, $id) as $class) {
1402+
foreach ($this->getClasses($def) as $class) {
14031403
$this->collectLineage($class, $lineage);
14041404
}
14051405
}
@@ -2130,17 +2130,15 @@ private function getAutoloadFile(): ?string
21302130
return null;
21312131
}
21322132

2133-
private function getClasses(Definition $definition, string $id): array
2133+
private function getClasses(Definition $definition): array
21342134
{
21352135
$classes = [];
21362136

21372137
while ($definition instanceof Definition) {
21382138
foreach ($definition->getTag($this->preloadTags[0]) as $tag) {
2139-
if (!isset($tag['class'])) {
2140-
throw new InvalidArgumentException(sprintf('Missing attribute "class" on tag "%s" for service "%s".', $this->preloadTags[0], $id));
2139+
if (isset($tag['class'])) {
2140+
$classes[] = trim($tag['class'], '\\');
21412141
}
2142-
2143-
$classes[] = trim($tag['class'], '\\');
21442142
}
21452143

21462144
$classes[] = trim($definition->getClass(), '\\');

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ public function testDumpAsFiles()
236236
->addError('No-no-no-no');
237237
$container->compile();
238238
$dumper = new PhpDumper($container);
239-
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]), true);
239+
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false, 'build_time' => 1588866825]), true);
240240
if ('\\' === \DIRECTORY_SEPARATOR) {
241241
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo.php', '/Fixtures/includes/foo.php', $dump);
242242
}
243-
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
243+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
244244
}
245245

246246
public function testDumpAsFilesWithFactoriesInlined()

src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@
137137
->tag('container.preload', ['class' => 'Some\Sidekick2'])
138138
->public();
139139

140+
$s->set('no_preload', 'TestNoPreload')
141+
->tag('container.no_preload')
142+
->public();
143+
144+
$s->set('preload_no_preload', 'TestPreloadNoPreload')
145+
->tag('container.preload')
146+
->tag('container.no_preload')
147+
->public();
148+
140149
$s->alias('alias_for_foo', 'foo')->private()->public();
141150
$s->alias('alias_for_alias', ref('alias_for_foo'));
142151
};

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,13 @@
193193
->addTag('container.preload', ['class' => 'Some\Sidekick1'])
194194
->addTag('container.preload', ['class' => 'Some\Sidekick2']);
195195

196+
$container->register('no_preload', 'TestNoPreload')
197+
->addTag('container.no_preload')
198+
->setPublic(true);
199+
200+
$container->register('preload_no_preload', 'TestPreloadNoPreload')
201+
->addTag('container.preload')
202+
->addTag('container.no_preload')
203+
->setPublic(true);
204+
196205
return $container;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ digraph sc {
3737
node_runtime_error [label="runtime_error\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
3838
node_errored_definition [label="errored_definition\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
3939
node_preload_sidekick [label="preload_sidekick\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
40+
node_no_preload [label="no_preload\nTestNoPreload\n", shape=record, fillcolor="#eeeeee", style="filled"];
41+
node_preload_no_preload [label="preload_no_preload\nTestPreloadNoPreload\n", shape=record, fillcolor="#eeeeee", style="filled"];
4042
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
4143
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
4244
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];

0 commit comments

Comments
 (0)