-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Autoconfigurable attributes on methods, properties and parameters #42039
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,66 @@ | |
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
|
||
/** | ||
* @author Alexander M. Turek <me@derrabus.de> | ||
*/ | ||
final class AttributeAutoconfigurationPass extends AbstractRecursivePass | ||
{ | ||
private $classAttributeConfigurators = []; | ||
private $methodAttributeConfigurators = []; | ||
private $propertyAttributeConfigurators = []; | ||
private $parameterAttributeConfigurators = []; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (80000 > \PHP_VERSION_ID || !$container->getAutoconfiguredAttributes()) { | ||
return; | ||
} | ||
|
||
foreach ($container->getAutoconfiguredAttributes() as $attributeName => $callable) { | ||
$callableReflector = new \ReflectionFunction(\Closure::fromCallable($callable)); | ||
if ($callableReflector->getNumberOfParameters() <= 2) { | ||
$this->classAttributeConfigurators[$attributeName] = $callable; | ||
continue; | ||
} | ||
|
||
$reflectorParameter = $callableReflector->getParameters()[2]; | ||
$parameterType = $reflectorParameter->getType(); | ||
$types = []; | ||
if ($parameterType instanceof \ReflectionUnionType) { | ||
foreach ($parameterType->getTypes() as $type) { | ||
$types[] = $type->getName(); | ||
} | ||
} elseif ($parameterType instanceof \ReflectionNamedType) { | ||
$types[] = $parameterType->getName(); | ||
ruudk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
throw new LogicException(sprintf('Argument "$%s" of attribute autoconfigurator should have a type, use one or more of "\ReflectionClass|\ReflectionMethod|\ReflectionProperty|\ReflectionParameter|\Reflector" in "%s" on line "%d".', $reflectorParameter->getName(), $callableReflector->getFileName(), $callableReflector->getStartLine())); | ||
} | ||
|
||
try { | ||
$attributeReflector = new \ReflectionClass($attributeName); | ||
} catch (\ReflectionException $e) { | ||
continue; | ||
} | ||
|
||
$targets = $attributeReflector->getAttributes(\Attribute::class)[0] ?? 0; | ||
$targets = $targets ? $targets->getArguments()[0] ?? -1 : 0; | ||
|
||
foreach (['class', 'method', 'property', 'parameter'] as $symbol) { | ||
if (['Reflector'] !== $types) { | ||
if (!\in_array('Reflection'.ucfirst($symbol), $types, true)) { | ||
continue; | ||
} | ||
if (!($targets & \constant('Attribute::TARGET_'.strtoupper($symbol)))) { | ||
throw new LogicException(sprintf('Invalid type "Reflection%s" on argument "$%s": attribute "%s" cannot target a '.$symbol.' in "%s" on line "%d".', ucfirst($symbol), $reflectorParameter->getName(), $attributeName, $callableReflector->getFileName(), $callableReflector->getStartLine())); | ||
} | ||
} | ||
$this->{$symbol.'AttributeConfigurators'}[$attributeName] = $callable; | ||
} | ||
} | ||
|
||
parent::process($container); | ||
} | ||
|
||
|
@@ -35,21 +83,74 @@ protected function processValue($value, bool $isRoot = false) | |
|| !$value->isAutoconfigured() | ||
|| $value->isAbstract() | ||
|| $value->hasTag('container.ignore_attributes') | ||
|| !($reflector = $this->container->getReflectionClass($value->getClass(), false)) | ||
|| !($classReflector = $this->container->getReflectionClass($value->getClass(), false)) | ||
) { | ||
return parent::processValue($value, $isRoot); | ||
} | ||
|
||
$autoconfiguredAttributes = $this->container->getAutoconfiguredAttributes(); | ||
$instanceof = $value->getInstanceofConditionals(); | ||
$conditionals = $instanceof[$reflector->getName()] ?? new ChildDefinition(''); | ||
foreach ($reflector->getAttributes() as $attribute) { | ||
if ($configurator = $autoconfiguredAttributes[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $reflector); | ||
$conditionals = $instanceof[$classReflector->getName()] ?? new ChildDefinition(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also put all of this new code into a new method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it really make it more clear? I don't agree, but if you give me a name for the method I'll do the change. |
||
|
||
if ($this->classAttributeConfigurators) { | ||
foreach ($classReflector->getAttributes() as $attribute) { | ||
if ($configurator = $this->classAttributeConfigurators[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $classReflector); | ||
} | ||
} | ||
} | ||
if (!isset($instanceof[$reflector->getName()]) && new ChildDefinition('') != $conditionals) { | ||
$instanceof[$reflector->getName()] = $conditionals; | ||
|
||
if ($this->parameterAttributeConfigurators && $constructorReflector = $this->getConstructor($value, false)) { | ||
foreach ($constructorReflector->getParameters() as $parameterReflector) { | ||
foreach ($parameterReflector->getAttributes() as $attribute) { | ||
if ($configurator = $this->parameterAttributeConfigurators[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $parameterReflector); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ($this->methodAttributeConfigurators || $this->parameterAttributeConfigurators) { | ||
foreach ($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflector) { | ||
if ($methodReflector->isStatic() || $methodReflector->isConstructor() || $methodReflector->isDestructor()) { | ||
continue; | ||
} | ||
|
||
if ($this->methodAttributeConfigurators) { | ||
foreach ($methodReflector->getAttributes() as $attribute) { | ||
if ($configurator = $this->methodAttributeConfigurators[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $methodReflector); | ||
} | ||
} | ||
} | ||
|
||
if ($this->parameterAttributeConfigurators) { | ||
foreach ($methodReflector->getParameters() as $parameterReflector) { | ||
foreach ($parameterReflector->getAttributes() as $attribute) { | ||
if ($configurator = $this->parameterAttributeConfigurators[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $parameterReflector); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ($this->propertyAttributeConfigurators) { | ||
foreach ($classReflector->getProperties(\ReflectionProperty::IS_PUBLIC) as $propertyReflector) { | ||
if ($propertyReflector->isStatic()) { | ||
continue; | ||
} | ||
|
||
foreach ($propertyReflector->getAttributes() as $attribute) { | ||
if ($configurator = $this->propertyAttributeConfigurators[$attribute->getName()] ?? null) { | ||
$configurator($conditionals, $attribute->newInstance(), $propertyReflector); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!isset($instanceof[$classReflector->getName()]) && new ChildDefinition('') != $conditionals) { | ||
$instanceof[$classReflector->getName()] = $conditionals; | ||
$value->setInstanceofConditionals($instanceof); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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\Fixtures\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER)] | ||
final class CustomAnyAttribute | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Fixtures\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class CustomMethodAttribute | ||
{ | ||
public function __construct( | ||
public string $someAttribute, | ||
public int $priority = 0, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Fixtures\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
final class CustomParameterAttribute | ||
{ | ||
public function __construct( | ||
public string $someAttribute, | ||
public int $priority = 0, | ||
) { | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.