Skip to content

Commit 8b1a3aa

Browse files
committed
[Routing] Construct Route annotations using named arguments
1 parent bf30fa4 commit 8b1a3aa

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>
@@ -65,6 +66,8 @@ public function __construct(
6566
$data = ['path' => $data];
6667
} elseif (!\is_array($data)) {
6768
throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data)));
69+
} elseif ([] !== $data) {
70+
trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
6871
}
6972
if (null !== $path && !\is_string($path) && !\is_array($path)) {
7073
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
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* already encoded slashes are not decoded nor double-encoded anymore when generating URLs
8+
* Deprecated creating instances of the `Route` annotation class by passing an array of parameters
89

910
5.2.0
1011
-----

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.0",
3435
"symfony/dependency-injection": "<4.4",
3536
"symfony/yaml": "<4.4"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy