Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Add optional $className param to ControllerEvent::getAttributes() #50335

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
Jun 3, 2023
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add optional `$className` parameter to `ControllerEvent::getAttributes()`

6.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ public function getNamedArguments(): array
}

/**
* @return array<class-string, list<object>>
* @template T of class-string|null
*
* @param T $className
*
* @return array<class-string, list<object>>|list<object>
*
* @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
*/
public function getAttributes(): array
public function getAttributes(string $className = null): array
{
return $this->controllerEvent->getAttributes();
return $this->controllerEvent->getAttributes($className);
}
}
14 changes: 10 additions & 4 deletions src/Symfony/Component/HttpKernel/Event/ControllerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ public function setController(callable $controller, array $attributes = null): v
}

/**
* @return array<class-string, list<object>>
* @template T of class-string|null
*
* @param T $className
*
* @return array<class-string, list<object>>|list<object>
*
* @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
*/
public function getAttributes(): array
public function getAttributes(string $className = null): array
{
if (isset($this->attributes)) {
return $this->attributes;
return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
}

if (\is_array($this->controller) && method_exists(...$this->controller)) {
Expand All @@ -102,6 +108,6 @@ public function getAttributes(): array
}
}

return $this->attributes;
return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

Expand Down Expand Up @@ -51,6 +52,9 @@ public function testGetAttributes()
new Bar('class'),
new Bar('method'),
],
Baz::class => [
new Baz(),
],
];

$this->assertEquals($expected, $event->getAttributes());
Expand All @@ -61,4 +65,27 @@ public function testGetAttributes()
$this->assertEquals($expected, $event->getAttributes());
$this->assertSame($controllerEvent->getAttributes(), $event->getAttributes());
}

public function testGetAttributesByClassName()
{
$controller = new AttributeController();
$request = new Request();

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

$event = new ControllerArgumentsEvent(new TestHttpKernel(), $controllerEvent, ['test'], new Request(), HttpKernelInterface::MAIN_REQUEST);

$expected = [
new Bar('class'),
new Bar('method'),
];

$this->assertEquals($expected, $event->getAttributes(Bar::class));

$expected[] = new Bar('foo');
$event->setController($controller, [Bar::class => $expected]);

$this->assertEquals($expected, $event->getAttributes(Bar::class));
$this->assertSame($controllerEvent->getAttributes(Bar::class), $event->getAttributes(Bar::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

Expand All @@ -33,16 +34,44 @@ public function testGetAttributes(callable $controller)
new Bar('class'),
new Bar('method'),
],
Baz::class => [
new Baz(),
],
];

$this->assertEquals($expected, $event->getAttributes());
}

/**
* @dataProvider provideGetAttributes
*/
public function testGetAttributesByClassName(callable $controller)
{
$event = new ControllerEvent(new TestHttpKernel(), $controller, new Request(), HttpKernelInterface::MAIN_REQUEST);

$expected = [
new Bar('class'),
new Bar('method'),
];

$this->assertEquals($expected, $event->getAttributes(Bar::class));
}

/**
* @dataProvider provideGetAttributes
*/
public function testGetAttributesByInvalidClassName(callable $controller)
{
$event = new ControllerEvent(new TestHttpKernel(), $controller, new Request(), HttpKernelInterface::MAIN_REQUEST);

$this->assertEquals([], $event->getAttributes(\stdClass::class));
}

public static function provideGetAttributes()
{
yield [[new AttributeController(), '__invoke']];
yield [new AttributeController()];
yield [(new AttributeController())->__invoke(...)];
yield [#[Bar('class'), Bar('method')] static function () {}];
yield [#[Bar('class'), Bar('method'), Baz] static function () {}];
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/Fixtures/Attribute/Baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)]
class Baz
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;

use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Foo;

#[Bar('class'), Undefined('class')]
class AttributeController
{
#[Bar('method'), Undefined('method')]
#[Bar('method'), Baz, Undefined('method')]
public function __invoke()
{
}
Expand Down