8000 [DependencyInjection] allow extending `Autowire` attribute by kbond · Pull Request #49433 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] allow extending Autowire attribute #49433

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
Feb 21, 2023
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Enable deprecating parameters with `ContainerBuilder::deprecateParameter()`
* Add `#[AsAlias]` attribute to tell under which alias a service should be registered or to use the implemented interface if no parameter is given
* Allow to trim XML service parameters value by using `trim="true"` attribute
* Allow extending the `Autowire` attribute

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
}

if ($checkAttributes) {
foreach ($parameter->getAttributes() as $attribute) {
if (\in_array($attribute->getName(), [TaggedIterator::class, TaggedLocator::class, Autowire::class, MapDecorated::class], true)) {
foreach ([TaggedIterator::class, TaggedLocator::class, Autowire::class, MapDecorated::class] as $attributeClass) {
foreach ($parameter->getAttributes($attributeClass, Autowire::class === $attributeClass ? \ReflectionAttribute::IS_INSTANCEOF : 0) as $attribute) {
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());

continue 2;
continue 3;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ public function testAutowireAttribute()

$definition = $container->getDefinition(AutowireAttribute::class);

$this->assertCount(9, $definition->getArguments());
$this->assertCount(10, $definition->getArguments());
$this->assertEquals(new Reference('some.id'), $definition->getArgument(0));
$this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(1));
$this->assertSame('foo/bar', $definition->getArgument(2));
Expand All @@ -1244,7 +1244,8 @@ public function testAutowireAttribute()
$this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(5));
$this->assertSame('bar', $definition->getArgument(6));
$this->assertSame('@bar', $definition->getArgument(7));
$this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(8));
$this->assertSame('foo', $definition->getArgument(8));
$this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(9));

$container->compile();

Expand All @@ -1257,6 +1258,7 @@ public function testAutowireAttribute()
$this->assertSame('foo', $service->expressionAsValue);
$this->assertSame('bar', $service->rawValue);
$this->assertSame('@bar', $service->escapedRawValue);
$this->assertSame('foo', $service->customAutowire);
$this->assertNull($service->invalid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class AutowireProperty
public Foo $foo;
}

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class CustomAutowire extends Autowire
{
public function __construct(string $parameter)
{
parent::__construct(param: $parameter);
}
}

class AutowireAttribute
{
public function __construct(
Expand All @@ -52,6 +61,8 @@ public function __construct(
public string $rawValue,
#[Autowire('@@bar')]
public string $escapedRawValue,
#[CustomAutowire('some.parameter')]
public string $customAutowire,
#[Autowire(service: 'invalid.id')]
public ?\stdClass $invalid = null,
) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add `#[WithHttpStatus]` for defining status codes for exceptions
* Use an instance of `Psr\Clock\ClockInterface` to generate the current date time in `DateTimeValueResolver`
* Add `#[WithLogLevel]` for defining log levels for exceptions
* Allow extending the `Autowire` attribute

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function process(ContainerBuilder $container)
$args[$p->name] = $bindingValue;

continue;
} elseif (!$autowire || (!($autowireAttributes ??= $p->getAttributes(Autowire::class)) && (!$type || '\\' !== $target[0]))) {
} elseif (!$autowire || (!($autowireAttributes ??= $p->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)) && (!$type || '\\' !== $target[0]))) {
continue;
} elseif (is_subclass_of($type, \UnitEnum::class)) {
// do not attempt to register enum typed arguments if not already present in bindings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,15 @@ public function testAutowireAttribute()

$locator = $container->get($locatorId)->get('foo::fooAction');

$this->assertCount(7, $locator->getProvidedServices());
$this->assertCount(8, $locator->getProvidedServices());
$this->assertInstanceOf(\stdClass::class, $locator->get('service1'));
$this->assertSame('foo/bar', $locator->get('value'));
$this->assertSame('foo', $locator->get('expression'));
$this->assertInstanceOf(\stdClass::class, $locator->get('serviceAsValue'));
$this->assertInstanceOf(\stdClass::class, $locator->get('expressionAsValue'));
$this->assertSame('bar', $locator->get('rawValue'));
$this->assertSame('@bar', $locator->get('escapedRawValue'));
$this->assertSame('foo', $locator->get('customAutowire'));
$this->assertFalse($locator->has('service2'));
}
}
Expand Down Expand Up @@ -580,6 +581,15 @@ public function fooAction(Response $response, ?Response $nullableResponse)
}
}

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class CustomAutowire extends Autowire
{
public function __construct(string $parameter)
{
parent::__construct(param: $parameter);
}
}

class WithAutowireAttribute
{
public function fooAction(
Expand All @@ -597,6 +607,8 @@ public function fooAction(
string $rawValue,
#[Autowire('@@bar')]
string $escapedRawValue,
#[CustomAutowire('some.parameter')]
string $customAutowire,
#[Autowire(service: 'invalid.id')]
\stdClass $service2 = null,
) {
Expand Down
0