Skip to content

Commit 5b5926e

Browse files
minor #49471 [DependencyInjection] Only include $containerRef in compiled container when needed (ruudk)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Only include `$containerRef` in compiled container when needed | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? |no | Deprecations? |no | Tickets | | License | MIT | Doc PR | When passing an array to the `ServiceClosureArgument`, the compiled container can be optimized. Only when the `$containerRef` is used, it should be included. Commits ------- 0f1b865341 Only include $containerRef in compiled container when needed
2 parents 901c457 + b713ab4 commit 5b5926e

File tree

4 files changed

+10
-21
lines changed

4 files changed

+10
-21
lines changed

Dumper/PhpDumper.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,6 @@ private function dumpValue(mixed $value, bool $interpolate = true): string
18171817
$returnedType = sprintf(': %s\%s', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $value->getInvalidBehavior() ? '' : '?', str_replace(['|', '&'], ['|\\', '&\\'], $value->getType()));
18181818
}
18191819

1820-
$code = sprintf('return %s;', $code);
1821-
18221820
$attribute = '';
18231821
if ($value instanceof Reference) {
18241822
$attribute = 'name: '.$this->dumpValue((string) $value, $interpolate);
@@ -1829,9 +1827,14 @@ private function dumpValue(mixed $value, bool $interpolate = true): string
18291827

18301828
$attribute = sprintf('#[\Closure(%s)] ', $attribute);
18311829
}
1832-
$this->addContainerRef = true;
18331830

1834-
return sprintf("%sfunction () use (\$containerRef)%s {\n \$container = \$containerRef->get();\n\n %s\n }", $attribute, $returnedType, $code);
1831+
if (str_contains($code, '$container')) {
1832+
$this->addContainerRef = true;
1833+
1834+
return sprintf("%sfunction () use (\$containerRef)%s {\n \$container = \$containerRef->get();\n\n return %s;\n }", $attribute, $returnedType, $code);
1835+
}
1836+
1837+
return sprintf('%sfn ()%s => %s', $attribute, $returnedType, $code);
18351838
}
18361839

18371840
if ($value instanceof IteratorArgument) {

Tests/Fixtures/php/services_closure_argument_compiled.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ protected static function getServiceClosureService($container)
7373
*/
7474
protected static function getServiceClosureInvalidService($container)
7575
{
76-
$containerRef = $container->ref;
77-
78-
return $container->services['service_closure_invalid'] = new \Bar(function () use ($containerRef) {
79-
$container = $containerRef->get();
80-
81-
return NULL;
82-
});
76+
return $container->services['service_closure_invalid'] = new \Bar(fn () => NULL);
8377
}
8478
}

Tests/Fixtures/php/services_locator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ protected static function getFooServiceService($container)
8282
$container = $containerRef->get();
8383

8484
return ($container->privates['baz_service'] ??= new \stdClass());
85-
}, 'nil' => function () use ($containerRef) {
86-
$container = $containerRef->get();
87-
88-
return NULL;
89-
}]);
85+
}, 'nil' => fn () => NULL]);
9086
}
9187

9288
/**

Tests/Fixtures/php/services_uninitialized_ref.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ protected static function getBarService($container)
6666
$container = $containerRef->get();
6767

6868
return ($container->services['foo1'] ?? null);
69-
}, #[\Closure(name: 'foo2')] function () use ($containerRef) {
70-
$container = $containerRef->get();
71-
72-
return null;
73-
}, #[\Closure(name: 'foo3', class: 'stdClass')] function () use ($containerRef) {
69+
}, #[\Closure(name: 'foo2')] fn () => null, #[\Closure(name: 'foo3', class: 'stdClass')] function () use ($containerRef) {
7470
$container = $containerRef->get();
7571

7672
return ($container->privates['foo3'] ?? null);

0 commit comments

Comments
 (0)