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

Skip to content

Commit 4ebfa6b

Browse files
minor #46459 [DependencyInjection] Optimize dumped container (nicolas-grekas)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Optimize dumped container | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- 9e32833 [DependencyInjection] Optimize dumped container
2 parents 8f7ed6b + 9e32833 commit 4ebfa6b

13 files changed

+22
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ protected function {$methodName}($lazyInitialization)
884884

885885
if ($isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition)) {
886886
if (!$definition->isShared()) {
887-
$code .= sprintf(' %s = %1$s ?? ', $factory);
887+
$code .= sprintf(' %s ??= ', $factory);
888888

889889
if ($asFile) {
890890
$code .= "function () {\n";
@@ -1953,7 +1953,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
19531953
}
19541954
$code = $this->addNewInstance($definition, '', $id);
19551955
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
1956-
$code = sprintf('$this->%s[%s] = %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
1956+
return sprintf('$this->%s[%s] ??= %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
19571957
}
19581958
$code = "($code)";
19591959
} else {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ class getTaggedIteratorService extends ProjectServiceContainer
674674
{
675675
return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($container) {
676676
yield 0 => ($container->services['foo'] ?? $container->load('getFooService'));
677-
yield 1 => ($container->privates['tagged_iterator_foo'] ?? ($container->privates['tagged_iterator_foo'] = new \Bar()));
677+
yield 1 => $container->privates['tagged_iterator_foo'] ??= new \Bar();
678678
}, 2));
679679
}
680680
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected function getTaggedIteratorService()
401401
{
402402
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
403403
yield 0 => ($this->services['foo'] ?? $this->getFooService());
404-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
404+
yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar();
405405
}, 2));
406406
}
407407

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class ProjectServiceContainer extends Container
446446
{
447447
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
448448
yield 0 => ($this->services['foo'] ?? $this->getFooService());
449-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
449+
yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar();
450450
}, 2));
451451
}
452452

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function getBar3Service()
122122
{
123123
$this->services['bar3'] = $instance = new \BarCircular();
124124

125-
$a = ($this->services['foobar3'] ?? ($this->services['foobar3'] = new \FoobarCircular()));
125+
$a = $this->services['foobar3'] ??= new \FoobarCircular();
126126

127127
$instance->addFoobar($a, $a);
128128

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function getFooService()
5656
protected function getServiceClosureService()
5757
{
5858
return $this->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] function () {
59-
return ($this->services['foo'] ?? ($this->services['foo'] = new \Foo()));
59+
return $this->services['foo'] ??= new \Foo();
6060
});
6161
}
6262

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getRemovedIds(): array
5454
*/
5555
protected function getBarService()
5656
{
57-
return $this->services['bar'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? ($this->privates['bar_%env(BAR)%'] = new \stdClass())));
57+
return $this->services['bar'] = new \stdClass($this->privates['bar_%env(BAR)%'] ??= new \stdClass());
5858
}
5959

6060
/**
@@ -64,7 +64,7 @@ protected function getBarService()
6464
*/
6565
protected function getFooService()
6666
{
67-
return $this->services['foo'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? ($this->privates['bar_%env(BAR)%'] = new \stdClass())), ['baz_'.$this->getEnv('string:BAR') => new \stdClass()]);
67+
return $this->services['foo'] = new \stdClass($this->privates['bar_%env(BAR)%'] ??= new \stdClass(), ['baz_'.$this->getEnv('string:BAR') => new \stdClass()]);
6868
}
6969

7070
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected function getTaggedIteratorService()
401401
{
402402
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
403403
yield 0 => ($this->services['foo'] ?? $this->getFooService());
404-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
404+
yield 1 => $this->privates['tagged_iterator_foo'] ??= new \Bar();
405405
}, 2));
406406
}
407407

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getRemovedIds(): array
6060
*/
6161
protected function getBarServiceService()
6262
{
63-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
63+
return $this->services['bar_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass());
6464
}
6565

6666
/**
@@ -73,7 +73,7 @@ protected function getFooServiceService()
7373
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => #[\Closure(name: 'bar_service', class: 'stdClass')] function () {
7474
return ($this->services['bar_service'] ?? $this->getBarServiceService());
7575
}, 'baz' => #[\Closure(name: 'baz_service', class: 'stdClass')] function (): \stdClass {
76-
return ($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass()));
76+
return $this->privates['baz_service'] ??= new \stdClass();
7777
}, 'nil' => function () {
7878
return NULL;
7979
}]);
@@ -117,7 +117,7 @@ protected function getTranslator_Loader3Service()
117117
protected function getTranslator1Service()
118118
{
119119
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 () {
120-
return ($this->services['translator.loader_1'] ?? ($this->services['translator.loader_1'] = new \stdClass()));
120+
return $this->services['translator.loader_1'] ??= new \stdClass();
121121
}]));
122122
}
123123

@@ -129,10 +129,10 @@ protected function getTranslator1Service()
129129
protected function getTranslator2Service()
130130
{
131131
$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 () {
132-
return ($this->services['translator.loader_2'] ?? ($this->services['translator.loader_2'] = new \stdClass()));
132+
return $this->services['translator.loader_2'] ??= new \stdClass();
133133
}]));
134134

135-
$instance->addResource('db', ($this->services['translator.loader_2'] ?? ($this->services['translator.loader_2'] = new \stdClass())), 'nl');
135+
$instance->addResource('db', $this->services['translator.loader_2'] ??= new \stdClass(), 'nl');
136136

137137
return $instance;
138138
}
@@ -145,10 +145,10 @@ protected function getTranslator2Service()
145145
protected function getTranslator3Service()
146146
{
147147
$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 () {
148-
return ($this->services['translator.loader_3'] ?? ($this->services['translator.loader_3'] = new \stdClass()));
148+
return $this->services['translator.loader_3'] ??= new \stdClass();
149149
}]));
150150

151-
$a = ($this->services['translator.loader_3'] ?? ($this->services['translator.loader_3'] = new \stdClass()));
151+
$a = $this->services['translator.loader_3'] ??= new \stdClass();
152152

153153
$instance->addResource('db', $a, 'nl');
154154
$instance->addResource('db', $a, 'en');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function getBarService()
6565
*/
6666
protected function getFooService($lazyLoad = true)
6767
{
68-
$this->factories['service_container']['foo'] = $this->factories['service_container']['foo'] ?? $this->getFooService(...);
68+
$this->factories['service_container']['foo'] ??= $this->getFooService(...);
6969

7070
// lazy factory for stdClass
7171

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class getNonSharedFooService extends ProjectServiceContainer
1919
*/
2020
public static function do($container, $lazyLoad = true)
2121
{
22-
$container->factories['non_shared_foo'] = $container->factories['non_shared_foo'] ?? function () use ($container) {
22+
$container->factories['non_shared_foo'] ??= function () use ($container) {
2323
return self::do($container);
2424
};
2525

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getRemovedIds(): array
5151
*/
5252
protected function getBarServiceService()
5353
{
54-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
54+
return $this->services['bar_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass());
5555
}
5656

5757
/**
@@ -61,6 +61,6 @@ protected function getBarServiceService()
6161
*/
6262
protected function getFooServiceService()
6363
{
64-
return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
64+
return $this->services['foo_service'] = new \stdClass($this->privates['baz_service'] ??= new \stdClass());
6565
}
6666
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function getBazService()
9191
{
9292
$this->services['baz'] = $instance = new \stdClass();
9393

94-
$instance->foo3 = ($this->privates['foo3'] ?? ($this->privates['foo3'] = new \stdClass()));
94+
$instance->foo3 = $this->privates['foo3'] ??= new \stdClass();
9595

9696
return $instance;
9797
}

0 commit comments

Comments
 (0)