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 1 commit
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
Prev Previous commit
Add ControllerAttributeListener
  • Loading branch information
codedmonkey committed Feb 17, 2022
commit 27a2403c633d93d9137d31012a14cc89af9f9b32
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
@@ -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
@@ -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
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Fixtures\Controller;

use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\FooController;

#[FooController(bar: 'class')]
class ControllerAttributeAtClassAndMethodController
{
#[FooController(bar: 'method')]
public function foo()
{
}

public function bar()
{
}
}
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\HttpKernel\Tests\Fixtures\Controller;

use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\FooController;

#[FooController(bar: 'class')]
class ControllerAttributeAtClassController
{
public function foo()
{
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy