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

Skip to content

[DependencyInjection] Improve dumping closure of service closure #49426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,10 @@ private function addNewInstance(Definition $definition, string $return = '', str

if (['Closure', 'fromCallable'] === $callable && [0] === array_keys($definition->getArguments())) {
$callable = $definition->getArgument(0);
if ($callable instanceof ServiceClosureArgument) {
return $return.$this->dumpValue($callable).$tail;
}

$arguments = ['...'];

if ($callable instanceof Reference || $callable instanceof Definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,11 @@ public function testClosure()
->setFactory(['Closure', 'fromCallable'])
->setArguments([new Reference('bar')]);
$container->register('bar', 'stdClass');
$container->register('closure_of_service_closure', 'Closure')
->setPublic('true')
->setFactory(['Closure', 'fromCallable'])
->setArguments([new ServiceClosureArgument(new Reference('bar2'))]);
$container->register('bar2', 'stdClass');
$container->compile();
$dumper = new PhpDumper($container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
$this->services = $this->privates = [];
$this->methodMap = [
'closure' => 'getClosureService',
'closure_of_service_closure' => 'getClosureOfServiceClosureService',
];

$this->aliases = [];
Expand All @@ -42,6 +43,7 @@ public function getRemovedIds(): array
{
return [
'bar' => true,
'bar2' => true,
];
}

Expand All @@ -54,4 +56,20 @@ protected static function getClosureService($container)
{
return $container->services['closure'] = (new \stdClass())->__invoke(...);
}

/**
* Gets the public 'closure_of_service_closure' shared service.
*
* @return \Closure
*/
protected static function getClosureOfServiceClosureService($container)
{
$containerRef = $container->ref;

return $container->services['closure_of_service_closure'] = #[\Closure(name: 'bar2', class: 'stdClass')] static function () use ($containerRef) {
$container = $containerRef->get();

return ($container->privates['bar2'] ??= new \stdClass());
};
}
}