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

Skip to content
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
[DependencyInjection] Allow array for the value of Autowire attribute
  • Loading branch information
willemverspyck authored and fabpot committed Oct 18, 2022
commit e983f64aee5130f9213508a4e5838fd58ea49993
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class Autowire
{
public readonly string|Expression|Reference $value;
public readonly string|array|Expression|Reference $value;

/**
* Use only ONE of the following.
Expand All @@ -33,15 +33,15 @@ class Autowire
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
*/
public function __construct(
string $value = null,
string|array $value = null,
string $service = null,
string $expression = null,
) {
if (!($service xor $expression xor null !== $value)) {
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
}

if (null !== $value && str_starts_with($value, '@')) {
if (\is_string($value) && str_starts_with($value, '@')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an array, wouldn't this need to process the strings inside that array to convert their prefixes too ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to allow nesting services into arrays.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do allow that in Yaml or XML (which is how you passed an array of objects indexed by some keys for instance)

match (true) {
str_starts_with($value, '@@') => $value = substr($value, 1),
str_starts_with($value, '@=') => $expression = substr($value, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;

class AutowireTest extends TestCase
{
Expand All @@ -35,4 +37,24 @@ public function testCanUseZeroForValue()
{
$this->assertSame('0', (new Autowire(value: '0'))->value);
}

public function testCanUseArrayForValue()
{
$this->assertSame(['FOO' => 'BAR'], (new Autowire(value: ['FOO' => 'BAR']))->value);
}

public function testCanUseValueWithAtSign()
{
$this->assertInstanceOf(Reference::class, (new Autowire(value: '@service'))->value);
}

public function testCanUseValueWithDoubleAtSign()
{
$this->assertSame('@service', (new Autowire(value: '@@service'))->value);
}

public function testCanUseValueWithAtAndEqualSign()
{
$this->assertInstanceOf(Expression::class, (new Autowire(value: '@=service'))->value);
}
}