8000 [Routing] Construct Route annotations using named arguments · symfony/symfony@d70dcc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d70dcc9

Browse files
committed
[Routing] Construct Route annotations using named arguments
1 parent 858dca4 commit d70dcc9

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed

UPGRADE-5.3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ PropertyInfo
6868

6969
* Deprecated the `Type::getCollectionKeyType()` and `Type::getCollectionValueType()` methods, use `Type::getCollectionKeyTypes()` and `Type::getCollectionValueTypes()` instead
7070

71+
Routing
72+
-------
73+
74+
* Deprecated creating instances of the `Route` annotation class by passing an array of parameters, use named arguments instead
75+
7176
Security
7277
--------
7378

UPGRADE-6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Routing
164164
* Removed `RouteCollectionBuilder`.
165165
* Added argument `$priority` to `RouteCollection::add()`
166166
* Removed the `RouteCompiler::REGEX_DELIMITER` constant
167+
* Removed the `$data` parameter from the constructor of the `Route` annotation class
167168

168169
Security
169170
--------

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"async-aws/sqs": "^1.0",
124124
"cache/integration-tests": "dev-master",
125125
"composer/package-versions-deprecated": "^1.8",
126-
"doctrine/annotations": "^1.10.4",
126+
"doctrine/annotations": "^1.12",
127127
"doctrine/cache": "~1.6",
128128
"doctrine/collections": "~1.0",
129129
"doctrine/data-fixtures": "^1.1",
@@ -151,6 +151,7 @@
151151
},
152152
"conflict": {
153153
"async-aws/core": "<1.5",
154+
"doctrine/annotations": "<1.12",
154155
"doctrine/dbal": "<2.10",
155156
"masterminds/html5": "<2.6",
156157
"phpdocumentor/reflection-docblock": "<3.2.2",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Annotation class for @Route().
1616
*
1717
* @Annotation
18+
* @NamedArgumentConstructor
1819
* @Target({"CLASS", "METHOD"})
1920
*
2021
* @author Fabien Potencier <fabien@symfony.com>
@@ -67,6 +68,8 @@ public function __construct(
6768
$data = ['path' => $data];
6869
} elseif (!\is_array($data)) {
6970
throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data)));
71+
} elseif ([] !== $data) {
72+
trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
7073
}
7174
if (null !== $path && !\is_string($path) && !\is_array($path)) {
7275
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/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Already encoded slashes are not decoded nor double-encoded anymore when generating URLs
88
* Add support for per-env configuration in loaders
9+
* Deprecated creating instances of the `Route` annotation class by passing an array of parameters
910

1011
5.2.0
1112
-----

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,56 @@
1212
namespace Symfony\Component\Routing\Tests\Annotation;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Routing\Annotation\Route;
1617

1718
class RouteTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
22+
/**
23+
* @group legacy
24+
*/
1925
public function testInvalidRouteParameter()
2026
{
2127
$this->expectException(\BadMethodCallException::class);
2228
new Route(['foo' => 'bar']);
2329
}
2430

31+
/**
32+
* @group legacy
33+
*/
2534
public function testTryingToSetLocalesDirectly()
2635
{
2736
$this->expectException(\BadMethodCallException::class);
2837
new Route(['locales' => ['nl' => 'bar']]);
2938
}
3039

3140
/**
41+
* @requires PHP 8
3242
* @dataProvider getValidParameters
3343
*/
34-
public function testRouteParameters($parameter, $value, $getter)
44+
public function testRouteParameters(string $parameter, $value, string $getter)
45+
{
46+
$route = new Route(...[$parameter => $value]);
47+
$this->assertEquals($route->$getter(), $value);
48+
}
49+
50+
/**
51+
* @group legacy
52+
* @dataProvider getValidParametersLegacy
53+
*/
54+
public function testRouteParametersLegacy(string $parameter, $value, string $getter)
3555
{
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+
3658
$route = new Route([$parameter => $value]);
3759
$this->assertEquals($route->$getter(), $value);
3860
}
3961

40-
public function getValidParameters()
62+
public function getValidParameters(): iterable
4163
{
4264
return [
43-
['value', '/Blog', 'getPath'],
4465
['requirements', ['locale' => 'en'], 'getRequirements'],
4566
['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'],
4667
['name', 'blog_index', 'getName'],
@@ -49,7 +70,14 @@ public function getValidParameters()
4970
['methods', ['GET', 'POST'], 'getMethods'],
5071
['host', '{locale}.example.com', 'getHost'],
5172
['condition', 'context.getMethod() == "GET"', 'getCondition'],
52-
['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'],
5373
];
5474
}
75+
76+
public function getValidParametersLegacy(): iterable
77+
{
78+
yield from $this->getValidParameters();
79+
80+
yield ['value', '/Blog', 'getPath'];
81+
yield ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'];
82+
}
5583
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testLoadFileWithoutStartTag()
5151

5252
public function testLoadVariadic()
5353
{
54-
$route = new Route(['path' => '/path/to/{id}']);
54+
$route = new Route('/path/to/{id}');
5555
$this->reader->expects($this->once())->method('getClassAnnotation');
5656
$this->reader->expects($this->once())->method('getMethodAnnotations')
5757
->willReturn([$route]);

src/Symfony/Component/Routing/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
"symfony/yaml": "^4.4|^5.0",
2727
"symfony/expression-language": "^4.4|^5.0",
2828
"symfony/dependency-injection": "^4.4|^5.0",
29-
"doctrine/annotations": "^1.10.4",
29+
"doctrine/annotations": "^1.12",
3030
"psr/log": "~1.0"
3131
},
3232
"conflict": {
33+
"doctrine/annotations": "<1.12",
3334
"symfony/config": "<5.3",
3435
"symfony/dependency-injection": "<4.4",
3536
"symfony/yaml": "<4.4"

0 commit comments

Comments
 (0)
0