Skip to content

[HttpKernel] Controller Attributes #45457

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

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 21 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Loader\Configurator;

use Symfony\Component\HttpKernel\EventListener\ControllerAttributeListener;

return static function (ContainerConfigurator $container) {
$container->services()
->set('controller_attribute_listener', ControllerAttributeListener::class)
->tag('kernel.event_subscriber')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\HttpKernel\Attribute;

/**
* Marker interface for controller attributes
Copy link
Contributor

Choose a reason for hiding this comment

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

For controller argument attributes we decided to remove the marker interface, should we do the same here ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No marker at all, the same way controller argument metadata works. We might need to introduce a ControllerMetadata class to mirror the ArgumentMetadata one.

*/
interface ControllerAttributeInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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\HttpKernel\EventListener;

use Doctrine\Persistence\Proxy;
Copy link
Contributor

Choose a reason for hiding this comment

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

relevant?

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Attribute\ControllerAttributeInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* The ControllerAttributeListener class parses attributes marked
* as controller attributes in controllers.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tim Goudriaan <tim@codedmonkey.com>
*/
class ControllerAttributeListener implements EventSubscriberInterface
{
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();

if (!\is_array($controller) && method_exists($controller, '__invoke')) {
$controller = [$controller, '__invoke'];
}

if (!\is_array($controller)) {
return;
}

$className = self::getRealClass(\get_class($controller[0]));
$object = new \ReflectionClass($className);
$method = $object->getMethod($controller[1]);

$classAttributes = $object->getAttributes(ControllerAttributeInterface::class, \ReflectionAttribute::IS_INSTANCEOF);
$methodAttributes = $method->getAttributes(ControllerAttributeInterface::class, \ReflectionAttribute::IS_INSTANCEOF);

$attributes = [];
foreach (array_merge($classAttributes, $methodAttributes) as $attribute) {
if ($attribute->isRepeated()) {
$attributes[$attribute->getName()][] = $attribute->newInstance();
} else {
// method attribute overrides class attribute
$attributes[$attribute->getName()] = $attribute->newInstance();
}
}

$request = $event->getRequest();
$request->attributes->set('_controller_attributes', $attributes);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [KernelEvents::CONTROLLER => 'onKernelController'];
}

private static function getRealClass(string $class): string
{
if (class_exists(Proxy::class)) {
if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
return $class;
}

return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
}

return $class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Foo;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\FooParam;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ArgumentAttributeController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\BasicTypesController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
Expand Down Expand Up @@ -121,24 +121,24 @@ public function testNullableTypesSignature()

public function testAttributeSignature()
{
$arguments = $this->factory->createArgumentMetadata([new AttributeController(), 'action']);
$arguments = $this->factory->createArgumentMetadata([new ArgumentAttributeController(), 'action']);

$this->assertEquals([
new ArgumentMetadata('baz', 'string', false, false, null, false, [new Foo('bar')]),
new ArgumentMetadata('baz', 'string', false, false, null, false, [new FooParam('bar')]),
], $arguments);
}

public function testMultipleAttributes()
{
$this->factory->createArgumentMetadata([new AttributeController(), 'multiAttributeArg']);
$this->assertCount(1, $this->factory->createArgumentMetadata([new AttributeController(), 'multiAttributeArg'])[0]->getAttributes());
$this->factory->createArgumentMetadata([new ArgumentAttributeController(), 'multiAttributeArg']);
$this->assertCount(1, $this->factory->createArgumentMetadata([new ArgumentAttributeController(), 'multiAttributeArg'])[0]->getAttributes());
}

public function testIssue41478()
{
$arguments = $this->factory->createArgumentMetadata([new AttributeController(), 'issue41478']);
$arguments = $this->factory->createArgumentMetadata([new ArgumentAttributeController(), 'issue41478']);
$this->assertEquals([
new ArgumentMetadata('baz', 'string', false, false, null, false, [new Foo('bar')]),
new ArgumentMetadata('baz', 'string', false, false, null, false, [new FooParam('bar')]),
new ArgumentMetadata('bat', 'string', false, false, null, false, []),
], $arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Foo;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\FooParam;

class ArgumentMetadataTest extends TestCase
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public function testDefaultValueUnavailable()

public function testGetAttributes()
{
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
$this->assertEquals([new Foo('bar')], $argument->getAttributes());
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new FooParam('bar')]);
$this->assertEquals([new FooParam('bar')], $argument->getAttributes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?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\HttpKernel\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\EventListener\ControllerAttributeListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\FooController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\RepeatableFooController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ControllerAttributeAtClassAndMethodController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ControllerAttributeAtClassController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ControllerAttributeAtMethodController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\RepeatableControllerAttributeAtClassAndMethodController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\RepeatableControllerAttributeAtClassController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\RepeatableControllerAttributeAtMethodController;

class ControllerAttributeListenerTest extends TestCase
{
public function testAttributeAtClass()
{
$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new ControllerAttributeAtClassController(), 'foo'],
$request,
null
);

$listener = new ControllerAttributeListener();
$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertEquals('class', $attributes[FooController::class]->bar);
}

public function testAttributeAtMethod()
{
$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new ControllerAttributeAtMethodController(), 'foo'],
$request,
null
);

$listener = new ControllerAttributeListener();
$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertEquals('method', $attributes[FooController::class]->bar);
}

public function testAttributeAtClassAndMethod()
{
$listener = new ControllerAttributeListener();

$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new ControllerAttributeAtClassAndMethodController(), 'foo'],
$request,
null
);

$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertEquals('method', $attributes[FooController::class]->bar);

$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new ControllerAttributeAtClassAndMethodController(), 'bar'],
$request,
null
);

$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertEquals('class', $attributes[FooController::class]->bar);
}

public function testRepeatableAttributeAtClass()
{
$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new RepeatableControllerAttributeAtClassController(), 'foo'],
$request,
null
);

$listener = new ControllerAttributeListener();
$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertCount(2, $attributes[RepeatableFooController::class]);
$this->assertEquals('class1', $attributes[RepeatableFooController::class][0]->bar);
$this->assertEquals('class2', $attributes[RepeatableFooController::class][1]->bar);
}

public function testRepeatableAttributeAtMethod()
{
$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new RepeatableControllerAttributeAtMethodController(), 'foo'],
$request,
null
);

$listener = new ControllerAttributeListener();
$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertCount(2, $attributes[RepeatableFooController::class]);
$this->assertEquals('method1', $attributes[RepeatableFooController::class][0]->bar);
$this->assertEquals('method2', $attributes[RepeatableFooController::class][1]->bar);
}

public function testRepeatableAttributeAtClassAndMethod()
{
$listener = new ControllerAttributeListener();

$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new RepeatableControllerAttributeAtClassAndMethodController(), 'foo'],
$request,
null
);

$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertCount(4, $attributes[RepeatableFooController::class]);
$this->assertEquals('class1', $attributes[RepeatableFooController::class][0]->bar);
$this->assertEquals('class2', $attributes[RepeatableFooController::class][1]->bar);
$this->assertEquals('method1', $attributes[RepeatableFooController::class][2]->bar);
$this->assertEquals('method2', $attributes[RepeatableFooController::class][3]->bar);

$request = new Request();

$event = new ControllerEvent(
$this->getMockBuilder(HttpKernelInterface::class)->getMock(),
[new RepeatableControllerAttributeAtClassAndMethodController(), 'bar'],
$request,
null
);

$listener->onKernelController($event);

$this->assertNotNull($attributes = $request->attributes->get('_controller_attributes'));
$this->assertCount(1, $attributes);
$this->assertCount(2, $attributes[RepeatableFooController::class]);
$this->assertEquals('class1', $attributes[RepeatableFooController::class][0]->bar);
$this->assertEquals('class2', $attributes[RepeatableFooController::class][1]->bar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures\Attribute;

use Symfony\Component\HttpKernel\Attribute\ControllerAttributeInterface;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class FooController implements ControllerAttributeInterface
{
public function __construct(
public string $bar
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Attribute;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class Foo
class FooParam
{
private $foo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures\Attribute;

use Symfony\Component\HttpKernel\Attribute\ControllerAttributeInterface;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class RepeatableFooController implements ControllerAttributeInterface
{
public function __construct(
public string $bar
) {}
}
Loading