Skip to content

[DependencyInjection] Allow AutowireCallable without method #50216

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
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 @@ -32,8 +32,8 @@ public function __construct(
if (!(null !== $callable xor null !== $service)) {
throw new LogicException('#[AutowireCallable] attribute must declare exactly one of $callable or $service.');
}
if (!(null !== $callable xor null !== $method)) {
throw new LogicException('#[AutowireCallable] attribute must declare one of $callable or $method.');
if (null === $service && null !== $method) {
throw new LogicException('#[AutowireCallable] attribute cannot have a $method without a $service.');
}

parent::__construct($callable ?? [new Reference($service), $method ?? '__invoke'], lazy: $lazy);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Attribute;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;

class AutowireCallableTest extends TestCase
{
public function testNoArguments()
{
$this->expectException(LogicException::class);

new AutowireCallable();
}

public function testCallableAndService()
{
$this->expectException(LogicException::class);

new AutowireCallable(callable: 'my_callable', service: 'my_service', method: 'my_method');
}

public function testMethodOnly()
{
$this->expectException(LogicException::class);

new AutowireCallable(method: 'my_method');
}

public function testCallableAndMethod()
{
$this->expectException(LogicException::class);

new AutowireCallable(callable: 'my_callable', method: 'my_method');
}

public function testStringCallable()
{
$attribute = new AutowireCallable(callable: 'my_callable');

self::assertSame('my_callable', $attribute->value);
self::assertFalse($attribute->lazy);
}

public function testArrayCallable()
{
$attribute = new AutowireCallable(callable: ['My\StaticClass', 'my_callable']);

self::assertSame(['My\StaticClass', 'my_callable'], $attribute->value);
self::assertFalse($attribute->lazy);
}

public function testArrayCallableWithReferenceAndMethod()
{
$attribute = new AutowireCallable(callable: [new Reference('my_service'), 'my_callable']);

self::assertEquals([new Reference('my_service'), 'my_callable'], $attribute->value);
self::assertFalse($attribute->lazy);
}

public function testArrayCallableWithReferenceOnly()
{
$attribute = new AutowireCallable(callable: [new Reference('my_service')]);

self::assertEquals([new Reference('my_service')], $attribute->value);
self::assertFalse($attribute->lazy);
}

public function testArrayCallableWithServiceAndMethod()
{
$attribute = new AutowireCallable(service: 'my_service', method: 'my_callable');

self::assertEquals([new Reference('my_service'), 'my_callable'], $attribute->value);
self::assertFalse($attribute->lazy);
}

public function testArrayCallableWithServiceOnly()
{
$attribute = new AutowireCallable(service: 'my_service');

self::assertEquals([new Reference('my_service'), '__invoke'], $attribute->value);
self::assertFalse($attribute->lazy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
use Symfony\Component\DependencyInjection\Tests\Compiler\IInterface;
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\Compiler\WitherAnnotation;
Expand Down Expand Up @@ -1720,6 +1721,8 @@ public function testAutowireClosure()
$container = new ContainerBuilder();
$container->register('foo', Foo::class)
->setPublic('true');
$container->register('my_callable', MyCallable::class)
->setPublic('true');
$container->register('baz', \Closure::class)
->setFactory(['Closure', 'fromCallable'])
->setArguments(['var_dump'])
Expand Down Expand Up @@ -1873,6 +1876,8 @@ public function __construct(
public \Closure $baz,
#[AutowireCallable(service: 'foo', method: 'cloneFoo')]
public \Closure $buz,
#[AutowireCallable(service: 'my_callable')]
public \Closure $bar,
) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,10 @@ interface SingleMethodInterface
{
public function theMethod();
}

class MyCallable
{
public function __invoke(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct()
'bar' => 'getBarService',
'baz' => 'getBazService',
'foo' => 'getFooService',
'my_callable' => 'getMyCallableService',
];

$this->aliases = [];
Expand Down Expand Up @@ -53,7 +54,7 @@ protected static function getBarService($container)
$container = $containerRef->get();

return ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());
}, ($container->services['baz'] ?? self::getBazService($container)), ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo())->cloneFoo(...));
}, ($container->services['baz'] ?? self::getBazService($container)), ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo())->cloneFoo(...), ($container->services['my_callable'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\MyCallable())->__invoke(...));
}

/**
Expand All @@ -75,4 +76,14 @@ protected static function getFooService($container)
{
return $container->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();
}

/**
* Gets the public 'my_callable' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\MyCallable
*/
protected static function getMyCallableService($container)
{
return $container->services['my_callable'] = new \Symfony\Component\DependencyInjection\Tests\Compiler\MyCallable();
}
}