Skip to content

Commit abf4cf2

Browse files
committed
throw exception when using alias on non invokable class
1 parent 9de2cc1 commit abf4cf2

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/Symfony/Component/Routing/Loader/AttributeClassLoader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ public function load(mixed $class, ?string $type = null): RouteCollection
107107
return $collection;
108108
}
109109
$fqcnAlias = false;
110+
111+
if (!$class->hasMethod('__invoke')) {
112+
foreach ($this->getAttributes($class) as $attr) {
113+
if (0 !== \count($attr->getAliases())) {
114+
throw new \InvalidArgumentException(\sprintf('Class "%s" cannot have aliases as it does not have an "__invoke" method.', $class->name));
115+
}
116+
}
117+
}
118+
110119
foreach ($class->getMethods() as $method) {
111120
$this->defaultRouteIndex = 0;
112121
$routeNamesBefore = array_keys($collection->all());

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasClassController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22

33
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
44

5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Attribute\Route;
7+
8+
#[Route('/hello', alias: ['alias', 'completely_different_name'])]
59
class AliasClassController
610
{
711

12+
#[Route('/world')]
13+
public function actionWorld()
14+
{
15+
}
16+
17+
#[Route('/symfony')]
18+
public function actionSymfony()
19+
{
20+
}
821
}

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasInvokableController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
44

5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Attribute\Route;
7+
8+
#[Route('/path', name:'invokable_path', alias: ['alias', 'completely_different_name'])]
59
class AliasInvokableController
610
{
711

12+
public function __invoke()
13+
{
14+
}
815
}

src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\Routing\Exception\LogicException;
1717
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AbstractClassController;
1818
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController;
19+
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasClassController;
20+
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasInvokableController;
1921
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasRouteController;
2022
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass;
2123
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\DefaultValueController;
@@ -366,7 +368,7 @@ public function testDefaultRouteName()
366368
$this->assertSame('symfony_component_routing_tests_fixtures_attributefixtures_encodingclass_routeàction', $defaultName);
367369
}
368370

369-
public function testAliases()
371+
public function testAliasesOnMethod()
370372
{
371373
$routes = $this->loader->load(AliasRouteController::class);
372374
$route = $routes->get('action_with_alias');
@@ -375,4 +377,22 @@ public function testAliases()
375377
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('alias'));
376378
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('completely_different_name'));
377379
}
380+
381+
public function testThrowsWithAliasesOnClass()
382+
{
383+
$this->expectException(\InvalidArgumentException::class);
384+
$this->expectExceptionMessage('Class "Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasClassController" cannot have aliases as it does not have an "__invoke" method.');
385+
386+
$this->loader->load(AliasClassController::class);
387+
}
388+
389+
public function testAliasesOnInvokableClass()
390+
{
391+
$routes = $this->loader->load(AliasInvokableController::class);
392+
$route = $routes->get('invokable_path');
393+
$this->assertCount(1, $routes);
394+
$this->assertEquals('/path', $route->getPath());
395+
$this->assertEquals(new Alias('invokable_path'), $routes->getAlias('alias'));
396+
$this->assertEquals(new Alias('invokable_path'), $routes->getAlias('completely_different_name'));
397+
}
378398
}

0 commit comments

Comments
 (0)