Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58ca2b4

Browse filesBrowse files
committed
[Routing] Fix localized paths
1 parent 0f96ac7 commit 58ca2b4
Copy full SHA for 58ca2b4

File tree

Expand file treeCollapse file tree

4 files changed

+201
-35
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+201
-35
lines changed

‎src/Symfony/Component/Routing/Annotation/Route.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Annotation/Route.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,22 @@ public function __construct(
6969
} elseif (!\is_array($data)) {
7070
throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data)));
7171
} elseif ([] !== $data) {
72-
trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
72+
$localizedPaths = [];
73+
$deprecation = false;
74+
foreach ($data as $key => $val) {
75+
if (in_array($key, ['path', 'name', 'requirements', 'options', 'defaults', 'host', 'methods', 'schemes', 'condition', 'priority', 'locale', 'format', 'utf8', 'stateless', 'env'])) {
76+
$deprecation = true;
77+
} else {
78+
$localizedPaths[$key] = $val;
79+
unset($data[$key]);
80+
}
81+
}
82+
83+
if ($deprecation) {
84+
trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
85+
}
86+
87+
$data['path'] = $localizedPaths;
7388
}
7489
if (null !== $path && !\is_string($path) && !\is_array($path)) {
7590
throw new \TypeError(sprintf('"%s": Argument $path is expected to be a string, array or null, got "%s".', __METHOD__, get_debug_type($path)));

‎src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php
+42-34Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,81 @@
1111

1212
namespace Symfony\Component\Routing\Tests\Annotation;
1313

14+
use Doctrine\Common\Annotations\AnnotationReader;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1617
use Symfony\Component\Routing\Annotation\Route;
18+
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\FooAttributesController;
19+
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\FooController;
1720

1821
class RouteTest extends TestCase
1922
{
2023
use ExpectDeprecationTrait;
2124

22-
/**
23-
* @group legacy
24-
*/
25-
public function testInvalidRouteParameter()
25+
private function getMethodAnnotation(string $method, bool $attributes): Route
2626
{
27-
$this->expectException(\BadMethodCallException::class);
28-
new Route(['foo' => 'bar']);
27+
$class = $attributes ? FooAttributesController::class : FooController::class;
28+
$reflection = new \ReflectionMethod($class, $method);
29+
30+
if ($attributes) {
31+
$attributes = $reflection->getAttributes(Route::class);
32+
$route = $attributes[0]->newInstance();
33+
} else {
34+
$reader = new AnnotationReader();
35+
$route = $reader->getMethodAnnotation($reflection, Route::class);
36+
}
37+
38+
if (!$route instanceof Route) {
39+
throw new \Exception('Can\'t parse annotation');
40+
}
41+
42+
return $route;
2943
}
3044

3145
/**
3246
* @group legacy
3347
*/
34-
public function testTryingToSetLocalesDirectly()
48+
public function testLegacyArrayAsFirstArgumentInsteadOfAttributes()
3549
{
36-
$this->expectException(\BadMethodCallException::class);
37-
new Route(['locales' => ['nl' => 'bar']]);
50+
$this->expectDeprecation('Since symfony/routing 5.3: Passing an array as first argument to "Symfony\Component\Routing\Annotation\Route::__construct" is deprecated. Use named arguments instead.');
51+
52+
$route = $this->getMethodAnnotation('requirementsAttributeInValue', false);
53+
$this->assertEquals($route->getRequirements(), ['locale' => 'en']);
3854
}
3955

4056
/**
4157
* @requires PHP 8
4258
* @dataProvider getValidParameters
4359
*/
44-
public function testRouteParameters(string $parameter, $value, string $getter)
60+
public function testRouteParameters(string $methodName, string $getter, $expectedReturn)
4561
{
46-
$route = new Route(...[$parameter => $value]);
47-
$this->assertEquals($route->$getter(), $value);
62+
$route = $this->getMethodAnnotation($methodName, true);
63+
$this->assertEquals($route->$getter(), $expectedReturn);
4864
}
4965

5066
/**
5167
* @group legacy
52-
* @dataProvider getLegacyValidParameters
68+
* @dataProvider getValidParameters
5369
*/
54-
public function testLegacyRouteParameters(string $parameter, $value, string $getter)
70+
public function testLegacyRouteParameters(string $methodName, string $getter, $expectedReturn)
5571
{
56-
$this->expectDeprecation('Since symfony/routing 5.3: Passing an array as first argument to "Symfony\Component\Routing\Annotation\Route::__construct" is deprecated. Use named arguments instead.');
57-
58-
$route = new Route([$parameter => $value]);
59-
$this->assertEquals($route->$getter(), $value);
72+
$route = $this->getMethodAnnotation($methodName, false);
73+
$this->assertEquals($route->$getter(), $expectedReturn);
6074
}
6175

6276
public function getValidParameters(): iterable
6377
{
6478
return [
65-
['requirements', ['locale' => 'en'], 'getRequirements'],
66-
['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'],
67-
['name', 'blog_index', 'getName'],
68-
['defaults', ['_controller' => 'MyBlogBundle:Blog:index'], 'getDefaults'],
69-
['schemes', ['https'], 'getSchemes'],
70-
['methods', ['GET', 'POST'], 'getMethods'],
71-
['host', '{locale}.example.com', 'getHost'],
72-
['condition', 'context.getMethod() == "GET"', 'getCondition'],
79+
['simplePath', 'getPath', '/Blog'],
80+
['localized', 'getLocalizedPaths', ['nl' => '/hier', 'en' => '/here']],
81+
['requirements', 'getRequirements', ['locale' => 'en']],
82+
['options', 'getOptions', ['compiler_class' => 'RouteCompiler']],
83+
['name', 'getName', 'blog_index'],
84+
['defaults', 'getDefaults', ['_controller' => 'MyBlogBundle:Blog:index']],
85+
['schemes', 'getSchemes', ['https']],
86+
['methods', 'getMethods', ['GET', 'POST']],
87+
['host', 'getHost', '{locale}.example.com'],
88+
['condition', 'getCondition', 'context.getMethod() == \'GET\''],
7389
];
7490
}
75-
76-
public function getLegacyValidParameters(): iterable
77-
{
78-
yield from $this->getValidParameters();
79-
80-
yield ['value', '/Blog', 'getPath'];
81-
yield ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'];
82-
}
8391
}
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class FooAttributesController
8+
{
9+
#[Route('/Blog')]
10+
public function simplePath()
11+
{
12+
}
13+
14+
#[Route(['nl' => '/hier', 'en' => '/here'])]
15+
public function localized()
16+
{
17+
}
18+
19+
#[Route('', requirements: ['locale' => 'en'])]
20+
public function requirements()
21+
{
22+
}
23+
24+
#[Route('', options: ['compiler_class' => 'RouteCompiler'])]
25+
public function options()
26+
{
27+
}
28+
29+
#[Route('', name: 'blog_index')]
30+
public function name()
31+
{
32+
}
33+
34+
#[Route('', defaults: ['_controller' => 'MyBlogBundle:Blog:index'])]
35+
public function defaults()
36+
{
37+
}
38+
39+
#[Route('', schemes: ['https'])]
40+
public function schemes()
41+
{
42+
}
43+
44+
#[Route('', methods: ['GET', 'POST'])]
45+
public function methods()
46+
{
47+
}
48+
49+
#[Route('', host: '{locale}.example.com')]
50+
public function host()
51+
{
52+
}
53+
54+
#[Route('', condition: 'context.getMethod() == \'GET\'')]
55+
public function condition()
56+
{
57+
}
58+
}
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class FooController
8+
{
9+
/**
10+
* @Route("/Blog")
11+
*/
12+
public function simplePath()
13+
{
14+
}
15+
16+
/**
17+
* @Route({"nl":"/hier","en":"/here"})
18+
*/
19+
public function localized()
20+
{
21+
}
22+
23+
/**
24+
* @Route("", requirements={"locale":"en"})
25+
*/
26+
public function requirements()
27+
{
28+
}
29+
30+
/**
31+
* @Route({"requirements":{"locale":"en"}, "path":""})
32+
*/
33+
public function requirementsAttributeInValue()
34+
{
35+
}
36+
37+
/**
38+
* @Route("", options={"compiler_class":"RouteCompiler"})
39+
*/
40+
public function options()
41+
{
42+
}
43+
44+
/**
45+
* @Route("", name="blog_index")
46+
*/
47+
public function name()
48+
{
49+
}
50+
51+
/**
52+
* @Route("", defaults={"_controller":"MyBlogBundle:Blog:index"})
53+
*/
54+
public function defaults()
55+
{
56+
}
57+
58+
/**
59+
* @Route("", schemes={"https"})
60+
*/
61+
public function schemes()
62+
{
63+
}
64+
65+
/**
66+
* @Route("", methods={"GET","POST"})
67+
*/
68+
public function methods()
69+
{
70+
}
71+
72+
/**
73+
* @Route(host="{locale}.example.com")
74+
*/
75+
public function host()
76+
{
77+
}
78+
79+
/**
80+
* @Route("", condition="context.getMethod() == 'GET'")
81+
*/
82+
public function condition()
83+
{
84+
}
85+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.