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

Skip to content

Commit ff7ccca

Browse files
committed
[DependencyInjection] Improve dumping closure of service closure
1 parent c83aadb commit ff7ccca

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,10 @@ private function addNewInstance(Definition $definition, string $return = '', str
11561156

11571157
if (['Closure', 'fromCallable'] === $callable && [0] === array_keys($definition->getArguments())) {
11581158
$callable = $definition->getArgument(0);
1159+
if ($callable instanceof ServiceClosureArgument) {
1160+
return $return.$this->dumpLiteralClass($this->dumpValue($callable)).$tail;
1161+
}
1162+
11591163
$arguments = ['...'];
11601164

11611165
if ($callable instanceof Reference || $callable instanceof Definition) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,11 @@ public function testClosure()
16011601
->setFactory(['Closure', 'fromCallable'])
16021602
->setArguments([new Reference('bar')]);
16031603
$container->register('bar', 'stdClass');
1604+
$container->register('closure_of_service_closure', 'Closure')
1605+
->setPublic('true')
1606+
->setFactory(['Closure', 'fromCallable'])
1607+
->setArguments([new ServiceClosureArgument(new Reference('bar2'))]);
1608+
$container->register('bar2', 'stdClass');
16041609
$container->compile();
16051610
$dumper = new PhpDumper($container);
16061611

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct()
2323
$this->services = $this->privates = [];
2424
$this->methodMap = [
2525
'closure' => 'getClosureService',
26+
'closure_of_service_closure' => 'getClosureOfServiceClosureService',
2627
];
2728

2829
$this->aliases = [];
@@ -42,6 +43,7 @@ public function getRemovedIds(): array
4243
{
4344
return [
4445
'bar' => true,
46+
'bar2' => true,
4547
];
4648
}
4749

@@ -54,4 +56,20 @@ protected static function getClosureService($container)
5456
{
5557
return $container->services['closure'] = (new \stdClass())->__invoke(...);
5658
}
59+
60+
/**
61+
* Gets the public 'closure_of_service_closure' shared service.
62+
*
63+
* @return \Closure
64+
*/
65+
protected static function getClosureOfServiceClosureService($container)
66+
{
67+
$containerRef = $container->ref;
68+
69+
return $container->services['closure_of_service_closure'] = ${($_ = #[\Closure(name: 'bar2', class: 'stdClass')] static function () use ($containerRef) {
70+
$container = $containerRef->get();
71+
72+
return ($container->privates['bar2'] ??= new \stdClass());
73+
}) && false ?: "_"};
74+
}
5775
}

0 commit comments

Comments
 (0)