-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Handle multi-attribute controller arguments #40307
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 |
---|---|---|
|
@@ -11,9 +11,6 @@ | |
|
||
namespace Symfony\Component\HttpKernel\ControllerMetadata; | ||
|
||
use Symfony\Component\HttpKernel\Attribute\ArgumentInterface; | ||
use Symfony\Component\HttpKernel\Exception\InvalidMetadataException; | ||
|
||
/** | ||
* Builds {@see ArgumentMetadata} objects based on the given Controller. | ||
* | ||
|
@@ -37,28 +34,15 @@ public function createArgumentMetadata($controller): array | |
} | ||
|
||
foreach ($reflection->getParameters() as $param) { | ||
$attribute = null; | ||
if (\PHP_VERSION_ID >= 80000) { | ||
$reflectionAttributes = $param->getAttributes(ArgumentInterface::class, \ReflectionAttribute::IS_INSTANCEOF); | ||
|
||
if (\count($reflectionAttributes) > 1) { | ||
$representative = $controller; | ||
|
||
if (\is_array($representative)) { | ||
$representative = sprintf('%s::%s()', \get_class($representative[0]), $representative[1]); | ||
} elseif (\is_object($representative)) { | ||
$representative = \get_class($representative); | ||
foreach ($param->getAttributes() as $reflectionAttribute) { | ||
if (class_exists($reflectionAttribute->getName())) { | ||
$attributes[] = $reflectionAttribute->newInstance(); | ||
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'm still not convinced we should call Alternatively: Can we make the 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. Sure, we could use some closures for that. But is it really worth it? Instantiation should not be heavy... 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. It shouldn't. Still, we trigger 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. Well, the autoloading will get triggered anyway when using 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. You are right, autoloading might be triggered anyway if we use 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. The scope of the argument value resolver covers Symfony controllers exclusively. So, after thinking twice at the possible alternatives, I propose to keep this PR as-is and iterate. 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.
👍🏻 We agree on the goal of the PR and discuss an implementation detail here. Let's merge and I'll try to create a follow-up PR to avoid unwanted
Sorry for being so persistent here. I'd like to avoid the problematic case before the bugfix becomes a BC break. |
||
} | ||
|
||
throw new InvalidMetadataException(sprintf('Controller "%s" has more than one attribute for "$%s" argument.', $representative, $param->getName())); | ||
} | ||
|
||
if (isset($reflectionAttributes[0])) { | ||
$attribute = $reflectionAttributes[0]->newInstance(); | ||
} | ||
} | ||
|
||
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attribute); | ||
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes ?? []); | ||
} | ||
|
||
return $arguments; | ||
|
Uh oh!
There was an error while loading. Please reload this page.