Skip to content

[HttpKernel] Don't cache controller's reflector inside the request #47468

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
Sep 2, 2022
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/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add `#[Cache]` to describe the default HTTP cache headers on controllers
* Add `absolute_uri` option to surrogate fragment renderers
* Add `ValueResolverInterface` and deprecate `ArgumentValueResolverInterface`
* Add argument `$reflector` to `ArgumentResolverInterface` and `ArgumentMetadataFactoryInterface`

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFa
$this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
}

public function getArguments(Request $request, callable $controller): array
public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
{
$arguments = [];
$reflectors = $request->attributes->get('_controller_reflectors') ?? [];

foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller, ...$reflectors) as $metadata) {
foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller, $reflector) as $metadata) {
foreach ($this->argumentValueResolvers as $resolver) {
if ((!$resolver instanceof ValueResolverInterface || $resolver instanceof TraceableValueResolver) && !$resolver->supports($request, $metadata)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ interface ArgumentResolverInterface
/**
* Returns the arguments to pass to the controller.
*
* @param \ReflectionFunctionAbstract|null $reflector
*
* @throws \RuntimeException When no value could be provided for a required argument
*/
public function getArguments(Request $request, callable $controller): array;
public function getArguments(Request $request, callable $controller/* , \ReflectionFunctionAbstract $reflector = null */): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public function __construct(ArgumentResolverInterface $resolver, Stopwatch $stop
$this->stopwatch = $stopwatch;
}

public function getArguments(Request $request, callable $controller): array
/**
* @param \ReflectionFunctionAbstract|null $reflector
*/
public function getArguments(Request $request, callable $controller/* , \ReflectionFunctionAbstract $reflector = null */): array
{
$reflector = 2 < \func_num_args() ? func_get_arg(2) : null;
$e = $this->stopwatch->start('controller.get_arguments');

$ret = $this->resolver->getArguments($request, $controller);
$ret = $this->resolver->getArguments($request, $controller, $reflector);

$e->stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
*/
final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
{
public function createArgumentMetadata(string|object|array $controller, \ReflectionClass $class = null, \ReflectionFunctionAbstract $reflection = null): array
public function createArgumentMetadata(string|object|array $controller, \ReflectionFunctionAbstract $reflector = null): array
{
$arguments = [];
$reflector ??= new \ReflectionFunction($controller(...));

if (null === $reflection) {
$reflection = new \ReflectionFunction($controller(...));
$class = str_contains($reflection->name, '{closure}') ? null : $reflection->getClosureScopeClass();
}
$class = $class?->name;

foreach ($reflection->getParameters() as $param) {
foreach ($reflector->getParameters() as $param) {
$attributes = [];
foreach ($param->getAttributes() as $reflectionAttribute) {
if (class_exists($reflectionAttribute->getName())) {
$attributes[] = $reflectionAttribute->newInstance();
}
}

$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $class), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes);
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes);
}

return $arguments;
Expand All @@ -45,22 +40,17 @@ public function createArgumentMetadata(string|object|array $controller, \Reflect
/**
* Returns an associated type to the given parameter if available.
*/
private function getType(\ReflectionParameter $parameter, ?string $class): ?string
private function getType(\ReflectionParameter $parameter): ?string
{
if (!$type = $parameter->getType()) {
return null;
}
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;

if (null !== $class) {
switch (strtolower($name)) {
case 'self':
return $class;
case 'parent':
return get_parent_class($class) ?: null;
}
}

return $name;
return match (strtolower($name)) {
'self' => $parameter->getDeclaringClass()?->name,
'parent' => get_parent_class($parameter->getDeclaringClass()?->name ?? '') ?: null,
default => $name,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
interface ArgumentMetadataFactoryInterface
{
/**
* @param \ReflectionFunctionAbstract|null $reflector
*
* @return ArgumentMetadata[]
*/
public function createArgumentMetadata(string|object|array $controller): array;
public function createArgumentMetadata(string|object|array $controller/* , \ReflectionFunctionAbstract $reflector = null */): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ public function getNamedArguments(): array

$namedArguments = [];
$arguments = $this->arguments;
$r = $this->getRequest()->attributes->get('_controller_reflectors')[1] ?? new \ReflectionFunction($this->controllerEvent->getController());

foreach ($r->getParameters() as $i => $param) {
foreach ($this->controllerEvent->getControllerReflector()->getParameters() as $i => $param) {
if ($param->isVariadic()) {
$namedArguments[$param->name] = \array_slice($arguments, $i);
break;
Expand Down
29 changes: 19 additions & 10 deletions src/Symfony/Component/HttpKernel/Event/ControllerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
final class ControllerEvent extends KernelEvent
{
private string|array|object $controller;
private \ReflectionFunctionAbstract $controllerReflector;
private array $attributes;

public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, ?int $requestType)
Expand All @@ -42,6 +43,11 @@ public function getController(): callable
return $this->controller;
}

public function getControllerReflector(): \ReflectionFunctionAbstract
{
return $this->controllerReflector;
}

/**
* @param array<class-string, list<object>>|null $attributes
*/
Expand All @@ -62,17 +68,13 @@ public function setController(callable $controller, array $attributes = null): v
}

if (\is_array($controller) && method_exists(...$controller)) {
$action = new \ReflectionMethod(...$controller);
$class = new \ReflectionClass($controller[0]);
$this->controllerReflector = new \ReflectionMethod(...$controller);
} elseif (\is_string($controller) && false !== $i = strpos($controller, '::')) {
$action = new \ReflectionMethod($controller);
$class = new \ReflectionClass(substr($controller, 0, $i));
$this->controllerReflector = new \ReflectionMethod($controller);
} else {
$action = new \ReflectionFunction($controller(...));
$class = str_contains($action->name, '{closure}') ? null : $action->getClosureScopeClass();
$this->controllerReflector = new \ReflectionFunction($controller(...));
}

$this->getRequest()->attributes->set('_controller_reflectors', [$class, $action]);
$this->controller = $controller;
}

Expand All @@ -81,13 +83,20 @@ public function setController(callable $controller, array $attributes = null): v
*/
public function getAttributes(): array
{
if (isset($this->attributes) || ![$class, $action] = $this->getRequest()->attributes->get('_controller_reflectors')) {
return $this->attributes ??= [];
if (isset($this->attributes)) {
return $this->attributes;
}

if (\is_array($this->controller) && method_exists(...$this->controller)) {
$class = new \ReflectionClass($this->controller[0]);
} elseif (\is_string($this->controller) && false !== $i = strpos($this->controller, '::')) {
$class = new \ReflectionClass(substr($this->controller, 0, $i));
} else {
$class = str_contains($this->controllerReflector->name, '{closure}') ? null : $this->controllerReflector->getClosureScopeClass();
}
$this->attributes = [];

foreach (array_merge($class?->getAttributes() ?? [], $action->getAttributes()) as $attribute) {
foreach (array_merge($class?->getAttributes() ?? [], $this->controllerReflector->getAttributes()) as $attribute) {
if (class_exists($attribute->getName())) {
$this->attributes[$attribute->getName()][] = $attribute->newInstance();
}
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Re
$controller = $event->getController();

// controller arguments
$arguments = $this->argumentResolver->getArguments($request, $controller);
$arguments = $this->argumentResolver->getArguments($request, $controller, $event->getControllerReflector());

$event = new ControllerArgumentsEvent($this, $event, $arguments, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
Expand Down Expand Up @@ -173,7 +173,6 @@ private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Re
throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
}
}
$request->attributes->remove('_controller_reflectors');

return $this->filterResponse($response, $request, $type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,12 @@

class ControllerEventTest extends TestCase
{
public function testSetAttributes()
{
$request = new Request();
$request->attributes->set('_controller_reflectors', [1, 2]);
$controller = [new AttributeController(), 'action'];
$event = new ControllerEvent(new TestHttpKernel(), $controller, $request, HttpKernelInterface::MAIN_REQUEST);
$event->setController($controller, []);

$this->assertSame([], $event->getAttributes());
}

/**
* @dataProvider provideGetAttributes
*/
public function testGetAttributes(callable $controller)
{
$request = new Request();
$reflector = new \ReflectionFunction($controller(...));
$request->attributes->set('_controller_reflectors', [str_contains($reflector->name, '{closure}') ? null : $reflector->getClosureScopeClass(), $reflector]);

$event = new ControllerEvent(new TestHttpKernel(), $controller, $request, HttpKernelInterface::MAIN_REQUEST);
$event = new ControllerEvent(new TestHttpKernel(), $controller, new Request(), HttpKernelInterface::MAIN_REQUEST);

$expected = [
Bar::class => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getController(Request $request): callable|false
return $this->callController(...);
}

public function getArguments(Request $request, callable $controller): array
public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
{
return [$request];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getController(Request $request): callable|false
return $this->callController(...);
}

public function getArguments(Request $request, callable $controller): array
public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
{
return [$request];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Tests/TestHttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getController(Request $request): callable|false
return $this->callController(...);
}

public function getArguments(Request $request, callable $controller): array
public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
{
return [$request];
}
Expand Down