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

Skip to content

[DependencyInjection] Fix generating adapters of functional interfaces #60842

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
merged 1 commit into from
Jun 20, 2025
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 @@ -40,22 +40,22 @@ public function __get(mixed $name): mixed
}

if (isset($this->initializer)) {
$this->service = ($this->initializer)();
if (\is_string($service = ($this->initializer)())) {
$service = (new \ReflectionClass($service))->newInstanceWithoutConstructor();
}
$this->service = $service;
unset($this->initializer);
}

return $this->service;
}

public static function getCode(string $initializer, array $callable, Definition $definition, ContainerBuilder $container, ?string $id): string
public static function getCode(string $initializer, array $callable, string $class, ContainerBuilder $container, ?string $id): string
{
$method = $callable[1];
$asClosure = 'Closure' === ($definition->getClass() ?: 'Closure');

if ($asClosure) {
if ($asClosure = 'Closure' === $class) {
$class = ($callable[0] instanceof Reference ? $container->findDefinition($callable[0]) : $callable[0])->getClass();
} else {
$class = $definition->getClass();
}

$r = $container->getReflectionClass($class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,14 +1060,15 @@ private function createService(Definition $definition, array &$inlineServices, b
}

if (\is_array($callable) && (
$callable[0] instanceof Reference
'Closure' !== $class
|| $callable[0] instanceof Reference
|| $callable[0] instanceof Definition && !isset($inlineServices[spl_object_hash($callable[0])])
)) {
$initializer = function () use ($callable, &$inlineServices) {
return $this->doResolveServices($callable[0], $inlineServices);
};

$proxy = eval('return '.LazyClosure::getCode('$initializer', $callable, $definition, $this, $id).';');
$proxy = eval('return '.LazyClosure::getCode('$initializer', $callable, $class, $this, $id).';');
$this->shareService($definition, $proxy, $id, $inlineServices);

return $proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,13 +1202,13 @@ private function addNewInstance(Definition $definition, string $return = '', ?st
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s).', $callable[1] ?: 'n/a'));
}

if (['...'] === $arguments && ($definition->isLazy() || 'Closure' !== ($definition->getClass() ?? 'Closure')) && (
if (['...'] === $arguments && ('Closure' !== ($class = $definition->getClass() ?: 'Closure') || $definition->isLazy() && (
$callable[0] instanceof Reference
|| ($callable[0] instanceof Definition && !$this->definitionVariables->contains($callable[0]))
)) {
))) {
$initializer = 'fn () => '.$this->dumpValue($callable[0]);

return $return.LazyClosure::getCode($initializer, $callable, $definition, $this->container, $id).$tail;
return $return.LazyClosure::getCode($initializer, $callable, $class, $this->container, $id).$tail;
}

if ($callable[0] instanceof Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ public function testThrowsWhenNotUsingInterface()
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot create adapter for service "foo" because "Symfony\Component\DependencyInjection\Tests\Argument\LazyClosureTest" is not an interface.');

LazyClosure::getCode('foo', [new \stdClass(), 'bar'], new Definition(LazyClosureTest::class), new ContainerBuilder(), 'foo');
LazyClosure::getCode('foo', [new \stdClass(), 'bar'], LazyClosureTest::class, new ContainerBuilder(), 'foo');
}

public function testThrowsOnNonFunctionalInterface()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot create adapter for service "foo" because interface "Symfony\Component\DependencyInjection\Tests\Argument\NonFunctionalInterface" doesn\'t have exactly one method.');

LazyClosure::getCode('foo', [new \stdClass(), 'bar'], new Definition(NonFunctionalInterface::class), new ContainerBuilder(), 'foo');
LazyClosure::getCode('foo', [new \stdClass(), 'bar'], NonFunctionalInterface::class, new ContainerBuilder(), 'foo');
}

public function testThrowsOnUnknownMethodInInterface()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot create lazy closure for service "bar" because its corresponding callable is invalid.');

LazyClosure::getCode('bar', [new Definition(FunctionalInterface::class), 'bar'], new Definition(\Closure::class), new ContainerBuilder(), 'bar');
LazyClosure::getCode('bar', [new Definition(FunctionalInterface::class), 'bar'], \Closure::class, new ContainerBuilder(), 'bar');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
use Symfony\Component\DependencyInjection\Tests\Compiler\MyCallable;
use Symfony\Component\DependencyInjection\Tests\Compiler\SingleMethodInterface;
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
Expand Down Expand Up @@ -522,6 +523,19 @@ public function testClosureProxy()
$this->assertInstanceOf(Foo::class, $container->get('closure_proxy')->theMethod());
}

public function testClosureProxyWithStaticMethod()
{
$container = new ContainerBuilder();
$container->register('closure_proxy', SingleMethodInterface::class)
->setPublic('true')
->setFactory(['Closure', 'fromCallable'])
->setArguments([[MyCallable::class, 'theMethodImpl']]);
$container->compile();

$this->assertInstanceOf(SingleMethodInterface::class, $container->get('closure_proxy'));
$this->assertSame(124, $container->get('closure_proxy')->theMethod());
}

public function testCreateServiceClass()
{
$builder = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,9 @@ class MyCallable
public function __invoke(): void
{
}

public static function theMethodImpl(): int
{
return 124;
}
}
Loading