From 29b0f96046b54f9517df7991933d2ffa0d3b33e4 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 21 Feb 2021 11:40:41 +0100 Subject: [PATCH] [Routing] Construct Route annotations using named arguments --- UPGRADE-5.3.md | 5 +++ UPGRADE-6.0.md | 1 + composer.json | 3 +- .../Component/Routing/Annotation/Route.php | 3 ++ src/Symfony/Component/Routing/CHANGELOG.md | 1 + .../Routing/Tests/Annotation/RouteTest.php | 36 ++++++++++++++++--- .../Tests/Loader/AnnotationFileLoaderTest.php | 2 +- src/Symfony/Component/Routing/composer.json | 3 +- 8 files changed, 47 insertions(+), 7 deletions(-) diff --git a/UPGRADE-5.3.md b/UPGRADE-5.3.md index 83435825d6d6..e6d07e723257 100644 --- a/UPGRADE-5.3.md +++ b/UPGRADE-5.3.md @@ -68,6 +68,11 @@ PropertyInfo * Deprecated the `Type::getCollectionKeyType()` and `Type::getCollectionValueType()` methods, use `Type::getCollectionKeyTypes()` and `Type::getCollectionValueTypes()` instead +Routing +------- + + * Deprecated creating instances of the `Route` annotation class by passing an array of parameters, use named arguments instead + Security -------- diff --git a/UPGRADE-6.0.md b/UPGRADE-6.0.md index 64dc873af4a8..97521d71efad 100644 --- a/UPGRADE-6.0.md +++ b/UPGRADE-6.0.md @@ -164,6 +164,7 @@ Routing * Removed `RouteCollectionBuilder`. * Added argument `$priority` to `RouteCollection::add()` * Removed the `RouteCompiler::REGEX_DELIMITER` constant + * Removed the `$data` parameter from the constructor of the `Route` annotation class Security -------- diff --git a/composer.json b/composer.json index 41e1911b6eca..eca08b56363b 100644 --- a/composer.json +++ b/composer.json @@ -123,7 +123,7 @@ "async-aws/sqs": "^1.0", "cache/integration-tests": "dev-master", "composer/package-versions-deprecated": "^1.8", - "doctrine/annotations": "^1.10.4", + "doctrine/annotations": "^1.12", "doctrine/cache": "~1.6", "doctrine/collections": "~1.0", "doctrine/data-fixtures": "^1.1", @@ -151,6 +151,7 @@ }, "conflict": { "async-aws/core": "<1.5", + "doctrine/annotations": "<1.12", "doctrine/dbal": "<2.10", "masterminds/html5": "<2.6", "phpdocumentor/reflection-docblock": "<3.2.2", diff --git a/src/Symfony/Component/Routing/Annotation/Route.php b/src/Symfony/Component/Routing/Annotation/Route.php index 418887da26af..4751f14b2d84 100644 --- a/src/Symfony/Component/Routing/Annotation/Route.php +++ b/src/Symfony/Component/Routing/Annotation/Route.php @@ -15,6 +15,7 @@ * Annotation class for @Route(). * * @Annotation + * @NamedArgumentConstructor * @Target({"CLASS", "METHOD"}) * * @author Fabien Potencier @@ -67,6 +68,8 @@ public function __construct( $data = ['path' => $data]; } elseif (!\is_array($data)) { throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data))); + } elseif ([] !== $data) { + trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__); } if (null !== $path && !\is_string($path) && !\is_array($path)) { throw new \TypeError(sprintf('"%s": Argument $path is expected to be a string, array or null, got "%s".', __METHOD__, get_debug_type($path))); diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index b664e03aea73..5f80dde1a945 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Already encoded slashes are not decoded nor double-encoded anymore when generating URLs * Add support for per-env configuration in loaders + * Deprecate creating instances of the `Route` annotation class by passing an array of parameters 5.2.0 ----- diff --git a/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php b/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php index a482378bdec7..d0c57113adab 100644 --- a/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php @@ -12,16 +12,25 @@ namespace Symfony\Component\Routing\Tests\Annotation; use PHPUnit\Framework\TestCase; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Routing\Annotation\Route; class RouteTest extends TestCase { + use ExpectDeprecationTrait; + + /** + * @group legacy + */ public function testInvalidRouteParameter() { $this->expectException(\BadMethodCallException::class); new Route(['foo' => 'bar']); } + /** + * @group legacy + */ public function testTryingToSetLocalesDirectly() { $this->expectException(\BadMethodCallException::class); @@ -29,18 +38,30 @@ public function testTryingToSetLocalesDirectly() } /** + * @requires PHP 8 * @dataProvider getValidParameters */ - public function testRouteParameters($parameter, $value, $getter) + public function testRouteParameters(string $parameter, $value, string $getter) + { + $route = new Route(...[$parameter => $value]); + $this->assertEquals($route->$getter(), $value); + } + + /** + * @group legacy + * @dataProvider getLegacyValidParameters + */ + public function testLegacyRouteParameters(string $parameter, $value, string $getter) { + $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.'); + $route = new Route([$parameter => $value]); $this->assertEquals($route->$getter(), $value); } - public function getValidParameters() + public function getValidParameters(): iterable { return [ - ['value', '/Blog', 'getPath'], ['requirements', ['locale' => 'en'], 'getRequirements'], ['options', ['compiler_class' => 'RouteCompiler'], 'getOptions'], ['name', 'blog_index', 'getName'], @@ -49,7 +70,14 @@ public function getValidParameters() ['methods', ['GET', 'POST'], 'getMethods'], ['host', '{locale}.example.com', 'getHost'], ['condition', 'context.getMethod() == "GET"', 'getCondition'], - ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths'], ]; } + + public function getLegacyValidParameters(): iterable + { + yield from $this->getValidParameters(); + + yield ['value', '/Blog', 'getPath']; + yield ['value', ['nl' => '/hier', 'en' => '/here'], 'getLocalizedPaths']; + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php index ee66ef1804ed..0e1331bf8147 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php @@ -51,7 +51,7 @@ public function testLoadFileWithoutStartTag() public function testLoadVariadic() { - $route = new Route(['path' => '/path/to/{id}']); + $route = new Route('/path/to/{id}'); $this->reader->expects($this->once())->method('getClassAnnotation'); $this->reader->expects($this->once())->method('getMethodAnnotations') ->willReturn([$route]); diff --git a/src/Symfony/Component/Routing/composer.json b/src/Symfony/Component/Routing/composer.json index 11da29d649e5..aeccf0681f3e 100644 --- a/src/Symfony/Component/Routing/composer.json +++ b/src/Symfony/Component/Routing/composer.json @@ -26,10 +26,11 @@ "symfony/yaml": "^4.4|^5.0", "symfony/expression-language": "^4.4|^5.0", "symfony/dependency-injection": "^4.4|^5.0", - "doctrine/annotations": "^1.10.4", + "doctrine/annotations": "^1.12", "psr/log": "~1.0" }, "conflict": { + "doctrine/annotations": "<1.12", "symfony/config": "<5.3", "symfony/dependency-injection": "<4.4", "symfony/yaml": "<4.4" 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