-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Symfony/Bundle/FrameworkBundle/Resources/config/controller.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/Symfony/Component/HttpKernel/Attribute/ControllerAttributeInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
interface ControllerAttributeInterface | ||
{ | ||
} |
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/HttpKernel/EventListener/ControllerAttributeListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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. 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
src/Symfony/Component/HttpKernel/Tests/EventListener/ControllerAttributeListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Symfony/Component/HttpKernel/Tests/Fixtures/Attribute/FooController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Symfony/Component/HttpKernel/Tests/Fixtures/Attribute/RepeatableFooController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 theArgumentMetadata
one.