From 8e1db2aa00a1cd0f18fe2e2e97d8ad1e7451aba1 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 9 Jun 2022 14:46:32 +0200 Subject: [PATCH] [DependencyInjection] fix dumped container --- .../DependencyInjection/Dumper/PhpDumper.php | 2 +- .../Tests/Fixtures/config/services9.php | 7 + .../Tests/Fixtures/containers/container9.php | 8 + .../Tests/Fixtures/graphviz/services9.dot | 3 + .../Tests/Fixtures/php/services9_as_files.txt | 287 ++++-------------- .../Tests/Fixtures/php/services9_compiled.php | 25 +- .../php/services9_inlined_factories.txt | 27 +- .../php/services_almost_circular_public.php | 2 +- .../services_closure_argument_compiled.php | 2 +- .../Tests/Fixtures/php/services_env_in_id.php | 4 +- .../php/services_errored_definition.php | 25 +- .../Tests/Fixtures/php/services_locator.php | 14 +- .../Fixtures/php/services_private_frozen.php | 4 +- .../php/services_uninitialized_ref.php | 2 +- .../Tests/Fixtures/xml/services9.xml | 7 + .../Tests/Fixtures/yaml/services9.yml | 10 + 16 files changed, 184 insertions(+), 245 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 48427038b1197..3f5fd134ef6f7 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1952,7 +1952,7 @@ private function getServiceCall(string $id, Reference $reference = null): string } $code = $this->addNewInstance($definition, '', $id); if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) { - return sprintf('$this->%s[%s] ??= %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); + return sprintf('($this->%s[%s] ??= %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); } $code = "($code)"; } else { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php index aa132a4fb6150..9a8ccacefe5f3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php @@ -137,6 +137,13 @@ ->tag('container.preload', ['class' => 'Some\Sidekick2']) ->public(); + $s->set('a_factory', 'Bar') + ->private(); + $s->set('a_service', 'Bar') + ->factory([service('a_factory'), 'getBar']); + $s->set('b_service', 'Bar') + ->factory([service('a_factory'), 'getBar']); + $s->alias('alias_for_foo', 'foo')->private()->public(); $s->alias('alias_for_alias', service('alias_for_foo')); }; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php index 47922be9bde58..a178250aa6125 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php @@ -189,4 +189,12 @@ ->addTag('container.preload', ['class' => 'Some\Sidekick1']) ->addTag('container.preload', ['class' => 'Some\Sidekick2']); +$container->register('a_factory', 'Bar'); +$container->register('a_service', 'Bar') + ->setFactory([new Reference('a_factory'), 'getBar']) + ->setPublic(true); +$container->register('b_service', 'Bar') + ->setFactory([new Reference('a_factory'), 'getBar']) + ->setPublic(true); + return $container; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot index 74af3cc094111..90937983717da 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot @@ -37,6 +37,9 @@ digraph sc { node_runtime_error [label="runtime_error\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_errored_definition [label="errored_definition\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_preload_sidekick [label="preload_sidekick\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_a_factory [label="a_factory\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_a_service [label="a_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_b_service [label="b_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"]; node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"]; node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"]; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt index ed8beb93f34f5..71e12e573e620 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt @@ -5,6 +5,7 @@ Array namespace Container%s; return [ + 'a_factory' => true, 'configurator_service' => true, 'configurator_service_simple' => true, 'decorated.pif-pouf' => true, @@ -45,15 +46,7 @@ class getBAR2Service extends ProjectServiceContainer } [Container%s/getBAR22Service.php] => services['a_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); + } +} -use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; -use Symfony\Component\DependencyInjection\Exception\RuntimeException; + [Container%s/getBServiceService.php] => services['b_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); + } +} -/** - * @internal This class has been auto-generated by the Symfony Dependency Injection Component. - */ + [Container%s/getBar23Service.php] => services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($container) { yield 0 => ($container->services['foo'] ?? $container->load('getFooService')); - yield 1 => $container->privates['tagged_iterator_foo'] ??= new \Bar(); + yield 1 => ($container->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } } [Container%s/getThrowingOneService.php] => fileMap = [ 'BAR' => 'getBAR2Service', 'BAR2' => 'getBAR22Service', + 'a_service' => 'getAServiceService', + 'b_service' => 'getBServiceService', 'bar2' => 'getBar23Service', 'baz' => 'getBazService', 'configured_service' => 'getConfiguredServiceService', @@ -920,14 +753,16 @@ require __DIR__.'/Container%s/getConfiguredServiceSimpleService.php'; require __DIR__.'/Container%s/getConfiguredServiceService.php'; require __DIR__.'/Container%s/getBazService.php'; require __DIR__.'/Container%s/getBar23Service.php'; +require __DIR__.'/Container%s/getBServiceService.php'; +require __DIR__.'/Container%s/getAServiceService.php'; require __DIR__.'/Container%s/getBAR22Service.php'; require __DIR__.'/Container%s/getBAR2Service.php'; $classes = []; +$classes[] = 'Bar'; $classes[] = 'Bar\FooClass'; $classes[] = 'Baz'; $classes[] = 'ConfClass'; -$classes[] = 'Bar'; $classes[] = 'BazClass'; $classes[] = 'Foo'; $classes[] = 'LazyContext'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 5aee165752c9a..a872e1d20e219 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -27,6 +27,8 @@ public function __construct() $this->methodMap = [ 'BAR' => 'getBARService', 'BAR2' => 'getBAR2Service', + 'a_service' => 'getAServiceService', + 'b_service' => 'getBServiceService', 'bar' => 'getBar3Service', 'bar2' => 'getBar22Service', 'baz' => 'getBazService', @@ -70,6 +72,7 @@ public function isCompiled(): bool public function getRemovedIds(): array { return [ + 'a_factory' => true, 'configurator_service' => true, 'configurator_service_simple' => true, 'decorated.pif-pouf' => true, @@ -106,6 +109,26 @@ protected function getBAR2Service() return $this->services['BAR2'] = new \stdClass(); } + /** + * Gets the public 'a_service' shared service. + * + * @return \Bar + */ + protected function getAServiceService() + { + return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + + /** + * Gets the public 'b_service' shared service. + * + * @return \Bar + */ + protected function getBServiceService() + { + return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + /** * Gets the public 'bar' shared service. * @@ -401,7 +424,7 @@ protected function getTaggedIteratorService() { return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar(); + yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt index 3dc4cfa266cf1..930ad979f46c5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt @@ -5,6 +5,7 @@ Array namespace Container%s; return [ + 'a_factory' => true, 'configurator_service' => true, 'configurator_service_simple' => true, 'decorated.pif-pouf' => true, @@ -54,6 +55,8 @@ class ProjectServiceContainer extends Container $this->methodMap = [ 'BAR' => 'getBARService', 'BAR2' => 'getBAR2Service', + 'a_service' => 'getAServiceService', + 'b_service' => 'getBServiceService', 'bar' => 'getBar3Service', 'bar2' => 'getBar22Service', 'baz' => 'getBazService', @@ -129,6 +132,26 @@ class ProjectServiceContainer extends Container return $this->services['BAR2'] = new \stdClass(); } + /** + * Gets the public 'a_service' shared service. + * + * @return \Bar + */ + protected function getAServiceService() + { + return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + + /** + * Gets the public 'b_service' shared service. + * + * @return \Bar + */ + protected function getBServiceService() + { + return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + /** * Gets the public 'bar' shared service. * @@ -446,7 +469,7 @@ class ProjectServiceContainer extends Container { return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar(); + yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } @@ -555,10 +578,10 @@ require dirname(__DIR__, %d).'%svendor/autoload.php'; (require __DIR__.'/ProjectServiceContainer.php')->set(\Container%s\ProjectServiceContainer::class, null); $classes = []; +$classes[] = 'Bar'; $classes[] = 'Bar\FooClass'; $classes[] = 'Baz'; $classes[] = 'ConfClass'; -$classes[] = 'Bar'; $classes[] = 'BazClass'; $classes[] = 'Foo'; $classes[] = 'LazyContext'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php index f80c7ecf042da..a81fd8b5ee0ea 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php @@ -122,7 +122,7 @@ protected function getBar3Service() { $this->services['bar3'] = $instance = new \BarCircular(); - $a = $this->services['foobar3'] ??= new \FoobarCircular(); + $a = ($this->services['foobar3'] ??= new \FoobarCircular()); $instance->addFoobar($a, $a); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php index 8809803a3e978..d6e186f8ebb10 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php @@ -56,7 +56,7 @@ protected function getFooService() protected function getServiceClosureService() { return $this->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] function () { - return $this->services['foo'] ??= new \Foo(); + return ($this->services['foo'] ??= new \Foo()); }); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php index ac337463aaca1..91da01032862a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php @@ -54,7 +54,7 @@ public function getRemovedIds(): array */ protected function getBarService() { - return $this->services['bar'] = new \stdClass($this->privates['bar_%env(BAR)%'] ??= new \stdClass()); + return $this->services['bar'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ??= new \stdClass())); } /** @@ -64,7 +64,7 @@ protected function getBarService() */ protected function getFooService() { - return $this->services['foo'] = new \stdClass($this->privates['bar_%env(BAR)%'] ??= new \stdClass(), ['baz_'.$this->getEnv('string:BAR') => new \stdClass()]); + return $this->services['foo'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ??= new \stdClass()), ['baz_'.$this->getEnv('string:BAR') => new \stdClass()]); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php index 39dd94157aa65..5eeafa3cd0778 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php @@ -27,6 +27,8 @@ public function __construct() $this->methodMap = [ 'BAR' => 'getBARService', 'BAR2' => 'getBAR2Service', + 'a_service' => 'getAServiceService', + 'b_service' => 'getBServiceService', 'bar' => 'getBar3Service', 'bar2' => 'getBar22Service', 'baz' => 'getBazService', @@ -70,6 +72,7 @@ public function isCompiled(): bool public function getRemovedIds(): array { return [ + 'a_factory' => true, 'configurator_service' => true, 'configurator_service_simple' => true, 'decorated.pif-pouf' => true, @@ -106,6 +109,26 @@ protected function getBAR2Service() return $this->services['BAR2'] = new \stdClass(); } + /** + * Gets the public 'a_service' shared service. + * + * @return \Bar + */ + protected function getAServiceService() + { + return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + + /** + * Gets the public 'b_service' shared service. + * + * @return \Bar + */ + protected function getBServiceService() + { + return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + } + /** * Gets the public 'bar' shared service. * @@ -401,7 +424,7 @@ protected function getTaggedIteratorService() { return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar(); + yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php index 27232f650a261..bfabae15405b0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php @@ -60,7 +60,7 @@ public function getRemovedIds(): array */ protected function getBarServiceService() { - return $this->services['bar_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass()); + return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); } /** @@ -73,7 +73,7 @@ protected function getFooServiceService() return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => #[\Closure(name: 'bar_service', class: 'stdClass')] function () { return ($this->services['bar_service'] ?? $this->getBarServiceService()); }, 'baz' => #[\Closure(name: 'baz_service', class: 'stdClass')] function (): \stdClass { - return $this->privates['baz_service'] ??= new \stdClass(); + return ($this->privates['baz_service'] ??= new \stdClass()); }, 'nil' => function () { return NULL; }]); @@ -117,7 +117,7 @@ protected function getTranslator_Loader3Service() protected function getTranslator1Service() { return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_1' => #[\Closure(name: 'translator.loader_1', class: 'stdClass')] function () { - return $this->services['translator.loader_1'] ??= new \stdClass(); + return ($this->services['translator.loader_1'] ??= new \stdClass()); }])); } @@ -129,10 +129,10 @@ protected function getTranslator1Service() protected function getTranslator2Service() { $this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_2' => #[\Closure(name: 'translator.loader_2', class: 'stdClass')] function () { - return $this->services['translator.loader_2'] ??= new \stdClass(); + return ($this->services['translator.loader_2'] ??= new \stdClass()); }])); - $instance->addResource('db', $this->services['translator.loader_2'] ??= new \stdClass(), 'nl'); + $instance->addResource('db', ($this->services['translator.loader_2'] ??= new \stdClass()), 'nl'); return $instance; } @@ -145,10 +145,10 @@ protected function getTranslator2Service() protected function getTranslator3Service() { $this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_3' => #[\Closure(name: 'translator.loader_3', class: 'stdClass')] function () { - return $this->services['translator.loader_3'] ??= new \stdClass(); + return ($this->services['translator.loader_3'] ??= new \stdClass()); }])); - $a = $this->services['translator.loader_3'] ??= new \stdClass(); + $a = ($this->services['translator.loader_3'] ??= new \stdClass()); $instance->addResource('db', $a, 'nl'); $instance->addResource('db', $a, 'en'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php index 4014b23da709b..c1fec3424eecd 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php @@ -51,7 +51,7 @@ public function getRemovedIds(): array */ protected function getBarServiceService() { - return $this->services['bar_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass()); + return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); } /** @@ -61,6 +61,6 @@ protected function getBarServiceService() */ protected function getFooServiceService() { - return $this->services['foo_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass()); + return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php index aa75ae83d74c0..8b0b7071259ec 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php @@ -91,7 +91,7 @@ protected function getBazService() { $this->services['baz'] = $instance = new \stdClass(); - $instance->foo3 = $this->privates['foo3'] ??= new \stdClass(); + $instance->foo3 = ($this->privates['foo3'] ??= new \stdClass()); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml index f59838396fb0b..24f025f523f7d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml @@ -154,6 +154,13 @@ + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml index 3f8446f682bdf..8fa97f4f685ab 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml @@ -179,3 +179,13 @@ services: - container.preload: { class: 'Some\Sidekick1' } - container.preload: { class: 'Some\Sidekick2' } public: true + a_factory: + class: Bar + a_service: + class: Bar + factory: ['@a_factory', 'getBar'] + public: true + b_service: + class: Bar + factory: ['@a_factory', 'getBar'] + public: true