[Routing] Fix: annotation loader ignores method's default values

This commit is contained in:
Oleg Voronkovich 2019-04-01 19:19:31 +03:00 committed by Nicolas Grekas
parent a362b8b838
commit 9b37793cbe
3 changed files with 12 additions and 2 deletions

View File

@ -196,7 +196,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
continue;
}
foreach ($paths as $locale => $path) {
if (false !== strpos($path, sprintf('{%s}', $param->name))) {
if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) {
$defaults[$param->name] = $param->getDefaultValue();
break;
}

View File

@ -12,4 +12,12 @@ class DefaultValueController
public function action($default = 'value')
{
}
/**
* @Route("/hello/{name<\w+>}", name="hello_without_default")
* @Route("/hello/{name<\w+>?Symfony}", name="hello_with_default")
*/
public function hello(string $name = 'World')
{
}
}

View File

@ -136,9 +136,11 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
public function testDefaultValuesForMethods()
{
$routes = $this->loader->load(DefaultValueController::class);
$this->assertCount(1, $routes);
$this->assertCount(3, $routes);
$this->assertEquals('/{default}/path', $routes->get('action')->getPath());
$this->assertEquals('value', $routes->get('action')->getDefault('default'));
$this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
$this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
}
public function testMethodActionControllers()