Skip to content

Commit 96112c1

Browse files
committed
[Routing] Convert BackedEnums passed as controller action parameters to their value
1 parent 8ca22cb commit 96112c1

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Loader/AnnotationClassLoader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
208208
}
209209
foreach ($paths as $locale => $path) {
210210
if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) {
211-
$defaults[$param->name] = $param->getDefaultValue();
211+
if (\is_scalar($defaultValue = $param->getDefaultValue()) || null === $defaultValue) {
212+
$defaults[$param->name] = $defaultValue;
213+
} elseif ($defaultValue instanceof \BackedEnum) {
214+
$defaults[$param->name] = $defaultValue->value;
215+
}
212216
break;
213217
}
214218
}

Tests/Fixtures/AnnotationFixtures/DefaultValueController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
44

55
use Symfony\Component\Routing\Annotation\Route;
6+
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum;
7+
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum;
68

79
class DefaultValueController
810
{
@@ -20,4 +22,18 @@ public function action($default = 'value')
2022
public function hello(string $name = 'World')
2123
{
2224
}
25+
26+
/**
27+
* @Route("/enum/{default}", name="string_enum_action")
28+
*/
29+
public function stringEnumAction(TestStringBackedEnum $default = TestStringBackedEnum::Diamonds)
30+
{
31+
}
32+
33+
/**
34+
* @Route("/enum/{default<\d+>}", name="int_enum_action")
35+
*/
36+
public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds)
37+
{
38+
}
2339
}

Tests/Fixtures/AttributeFixtures/DefaultValueController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
44

55
use Symfony\Component\Routing\Annotation\Route;
6+
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum;
7+
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum;
68

79
class DefaultValueController
810
{
@@ -18,4 +20,14 @@ public function action($default = 'value')
1820
public function hello(string $name = 'World')
1921
{
2022
}
23+
24+
#[Route(path: '/enum/{default}', name: 'string_enum_action')]
25+
public function stringEnumAction(TestStringBackedEnum $default = TestStringBackedEnum::Diamonds)
26+
{
27+
}
28+
29+
#[Route(path: '/enum/{default<\d+>}', name: 'int_enum_action')]
30+
public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds)
31+
{
32+
}
2133
}

Tests/Loader/AnnotationClassLoaderTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ public function testLocalizedPathRoutesWithExplicitPathPropety()
104104
public function testDefaultValuesForMethods()
105105
{
106106
$routes = $this->loader->load($this->getNamespace().'\DefaultValueController');
107-
$this->assertCount(3, $routes);
107+
$this->assertCount(5, $routes);
108108
$this->assertEquals('/{default}/path', $routes->get('action')->getPath());
109109
$this->assertEquals('value', $routes->get('action')->getDefault('default'));
110110
$this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
111111
$this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
112+
$this->assertEquals('diamonds', $routes->get('string_enum_action')->getDefault('default'));
113+
$this->assertEquals(20, $routes->get('int_enum_action')->getDefault('default'));
112114
}
113115

114116
public function testMethodActionControllers()

0 commit comments

Comments
 (0)