Skip to content

Commit d304eeb

Browse files
alamiraultnicolas-grekas
authored andcommitted
[Routing] Rename annotations to attribute in AttributeClassLoader
1 parent b9f4773 commit d304eeb

9 files changed

+79
-72
lines changed

CHANGELOG.md

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

77
* Add the `Requirement::UID_RFC9562` constant to validate UUIDs in the RFC 9562 format
8+
* Deprecate the `AttributeClassLoader::$routeAnnotationClass` property
89

910
7.1
1011
---

Loader/AttributeClassLoader.php

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Config\Loader\LoaderInterface;
1515
use Symfony\Component\Config\Loader\LoaderResolverInterface;
1616
use Symfony\Component\Config\Resource\FileResource;
17-
use Symfony\Component\Routing\Attribute\Route as RouteAnnotation;
17+
use Symfony\Component\Routing\Attribute\Route as RouteAttribute;
1818
use Symfony\Component\Routing\Exception\LogicException;
1919
use Symfony\Component\Routing\Route;
2020
use Symfony\Component\Routing\RouteCollection;
@@ -53,7 +53,11 @@
5353
*/
5454
abstract class AttributeClassLoader implements LoaderInterface
5555
{
56-
protected string $routeAnnotationClass = RouteAnnotation::class;
56+
/**
57+
* @deprecated since Symfony 7.2, use "setRouteAttributeClass()" instead.
58+
*/
59+
protected string $routeAnnotationClass = RouteAttribute::class;
60+
private string $routeAttributeClass = RouteAttribute::class;
5761
protected int $defaultRouteIndex = 0;
5862

5963
public function __construct(
@@ -62,11 +66,24 @@ public function __construct(
6266
}
6367

6468
/**
69+
* @deprecated since Symfony 7.2, use "setRouteAttributeClass(string $class)" instead
70+
*
6571
* Sets the annotation class to read route properties from.
6672
*/
6773
public function setRouteAnnotationClass(string $class): void
74+
{
75+
trigger_deprecation('symfony/routing', '7.2', 'The "%s()" method is deprecated, use "%s::setRouteAttributeClass()" instead.', __METHOD__, self::class);
76+
77+
$this->setRouteAttributeClass($class);
78+
}
79+
80+
/**
81+
* Sets the attribute class to read route properties from.
82+
*/
83+
public function setRouteAttributeClass(string $class): void
6884
{
6985
$this->routeAnnotationClass = $class;
86+
$this->routeAttributeClass = $class;
7087
}
7188

7289
/**
@@ -93,8 +110,8 @@ public function load(mixed $class, ?string $type = null): RouteCollection
93110
foreach ($class->getMethods() as $method) {
94111
$this->defaultRouteIndex = 0;
95112
$routeNamesBefore = array_keys($collection->all());
96-
foreach ($this->getAnnotations($method) as $annot) {
97-
$this->addRoute($collection, $annot, $globals, $class, $method);
113+
foreach ($this->getAttributes($method) as $attr) {
114+
$this->addRoute($collection, $attr, $globals, $class, $method);
98115
if ('__invoke' === $method->name) {
99116
$fqcnAlias = true;
100117
}
@@ -109,8 +126,8 @@ public function load(mixed $class, ?string $type = null): RouteCollection
109126
}
110127
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
111128
$globals = $this->resetGlobals();
112-
foreach ($this->getAnnotations($class) as $annot) {
113-
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
129+
foreach ($this->getAttributes($class) as $attr) {
130+
$this->addRoute($collection, $attr, $globals, $class, $class->getMethod('__invoke'));
114131
$fqcnAlias = true;
115132
}
116133
}
@@ -129,36 +146,36 @@ public function load(mixed $class, ?string $type = null): RouteCollection
129146
}
130147

131148
/**
132-
* @param RouteAnnotation $annot or an object that exposes a similar interface
149+
* @param RouteAttribute $attr or an object that exposes a similar interface
133150
*/
134-
protected function addRoute(RouteCollection $collection, object $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method): void
151+
protected function addRoute(RouteCollection $collection, object $attr, array $globals, \ReflectionClass $class, \ReflectionMethod $method): void
135152
{
136-
if ($annot->getEnv() && $annot->getEnv() !== $this->env) {
153+
if ($attr->getEnv() && $attr->getEnv() !== $this->env) {
137154
return;
138155
}
139156

140-
$name = $annot->getName() ?? $this->getDefaultRouteName($class, $method);
157+
$name = $attr->getName() ?? $this->getDefaultRouteName($class, $method);
141158
$name = $globals['name'].$name;
142159

143-
$requirements = $annot->getRequirements();
160+
$requirements = $attr->getRequirements();
144161

145162
foreach ($requirements as $placeholder => $requirement) {
146163
if (\is_int($placeholder)) {
147164
throw new \InvalidArgumentException(\sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()));
148165
}
149166
}
150167

151-
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
168+
$defaults = array_replace($globals['defaults'], $attr->getDefaults());
152169
$requirements = array_replace($globals['requirements'], $requirements);
153-
$options = array_replace($globals['options'], $annot->getOptions());
154-
$schemes = array_unique(array_merge($globals['schemes'], $annot->getSchemes()));
155-
$methods = array_unique(array_merge($globals['methods'], $annot->getMethods()));
170+
$options = array_replace($globals['options'], $attr->getOptions());
171+
$schemes = array_unique(array_merge($globals['schemes'], $attr->getSchemes()));
172+
$methods = array_unique(array_merge($globals['methods'], $attr->getMethods()));
156173

157-
$host = $annot->getHost() ?? $globals['host'];
158-
$condition = $annot->getCondition() ?? $globals['condition'];
159-
$priority = $annot->getPriority() ?? $globals['priority'];
174+
$host = $attr->getHost() ?? $globals['host'];
175+
$condition = $attr->getCondition() ?? $globals['condition'];
176+
$priority = $attr->getPriority() ?? $globals['priority'];
160177

161-
$path = $annot->getLocalizedPaths() ?: $annot->getPath();
178+
$path = $attr->getLocalizedPaths() ?: $attr->getPath();
162179
$prefix = $globals['localized_paths'] ?: $globals['path'];
163180
$paths = [];
164181

@@ -204,7 +221,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
204221

205222
foreach ($paths as $locale => $path) {
206223
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
207-
$this->configureRoute($route, $class, $method, $annot);
224+
$this->configureRoute($route, $class, $method, $attr);
208225
if (0 !== $locale) {
209226
$route->setDefault('_locale', $locale);
210227
$route->setRequirement('_locale', preg_quote($locale));
@@ -254,49 +271,50 @@ protected function getGlobals(\ReflectionClass $class): array
254271
{
255272
$globals = $this->resetGlobals();
256273

274+
// to be replaced in Symfony 8.0 by $this->routeAttributeClass
257275
if ($attribute = $class->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
258-
$annot = $attribute->newInstance();
276+
$attr = $attribute->newInstance();
259277

260-
if (null !== $annot->getName()) {
261-
$globals['name'] = $annot->getName();
278+
if (null !== $attr->getName()) {
279+
$globals['name'] = $attr->getName();
262280
}
263281

264-
if (null !== $annot->getPath()) {
265-
$globals['path'] = $annot->getPath();
282+
if (null !== $attr->getPath()) {
283+
$globals['path'] = $attr->getPath();
266284
}
267285

268-
$globals['localized_paths'] = $annot->getLocalizedPaths();
286+
$globals['localized_paths'] = $attr->getLocalizedPaths();
269287

270-
if (null !== $annot->getRequirements()) {
271-
$globals['requirements'] = $annot->getRequirements();
288+
if (null !== $attr->getRequirements()) {
289+
$globals['requirements'] = $attr->getRequirements();
272290
}
273291

274-
if (null !== $annot->getOptions()) {
275-
$globals['options'] = $annot->getOptions();
292+
if (null !== $attr->getOptions()) {
293+
$globals['options'] = $attr->getOptions();
276294
}
277295

278-
if (null !== $annot->getDefaults()) {
279-
$globals['defaults'] = $annot->getDefaults();
296+
if (null !== $attr->getDefaults()) {
297+
$globals['defaults'] = $attr->getDefaults();
280298
}
281299

282-
if (null !== $annot->getSchemes()) {
283-
$globals['schemes'] = $annot->getSchemes();
300+
if (null !== $attr->getSchemes()) {
301+
$globals['schemes'] = $attr->getSchemes();
284302
}
285303

286-
if (null !== $annot->getMethods()) {
287-
$globals['methods'] = $annot->getMethods();
304+
if (null !== $attr->getMethods()) {
305+
$globals['methods'] = $attr->getMethods();
288306
}
289307

290-
if (null !== $annot->getHost()) {
291-
$globals['host'] = $annot->getHost();
308+
if (null !== $attr->getHost()) {
309+
$globals['host'] = $attr->getHost();
292310
}
293311

294-
if (null !== $annot->getCondition()) {
295-
$globals['condition'] = $annot->getCondition();
312+
if (null !== $attr->getCondition()) {
313+
$globals['condition'] = $attr->getCondition();
296314
}
297315

298-
$globals['priority'] = $annot->getPriority() ?? 0;
299-
$globals['env'] = $annot->getEnv();
316+
$globals['priority'] = $attr->getPriority() ?? 0;
317+
$globals['env'] = $attr->getEnv();
300318

301319
foreach ($globals['requirements'] as $placeholder => $requirement) {
302320
if (\is_int($placeholder)) {
@@ -332,15 +350,18 @@ protected function createRoute(string $path, array $defaults, array $requirement
332350
}
333351

334352
/**
353+
* @param RouteAttribute $attr or an object that exposes a similar interface
354+
*
335355
* @return void
336356
*/
337-
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot);
357+
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr);
338358

339359
/**
340-
* @return iterable<int, RouteAnnotation>
360+
* @return iterable<int, RouteAttribute>
341361
*/
342-
private function getAnnotations(\ReflectionClass|\ReflectionMethod $reflection): iterable
362+
private function getAttributes(\ReflectionClass|\ReflectionMethod $reflection): iterable
343363
{
364+
// to be replaced in Symfony 8.0 by $this->routeAttributeClass
344365
foreach ($reflection->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
345366
yield $attribute->newInstance();
346367
}

Tests/Fixtures/AnnotationFixtures/InvokableFQCNAliasConflictController.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

Tests/Fixtures/TraceableAttributeClassLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function load(mixed $class, ?string $type = null): RouteCollection
3131
return parent::load($class, $type);
3232
}
3333

34-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
34+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
3535
{
3636
}
3737
}

Tests/Loader/AttributeClassLoaderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ public function testMethodActionControllers()
184184
$this->assertEquals(new Alias('put'), $routes->getAlias('Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\MethodActionControllers::put'));
185185
}
186186

187-
public function testInvokableClassRouteLoadWithMethodAnnotation()
187+
public function testInvokableClassRouteLoadWithMethodAttribute()
188188
{
189189
$routes = $this->loader->load(LocalizedMethodActionControllers::class);
190190
$this->assertCount(4, $routes);
191191
$this->assertEquals('/the/path', $routes->get('put.en')->getPath());
192192
$this->assertEquals('/the/path', $routes->get('post.en')->getPath());
193193
}
194194

195-
public function testGlobalDefaultsRoutesLoadWithAnnotation()
195+
public function testGlobalDefaultsRoutesLoadWithAttribute()
196196
{
197197
$routes = $this->loader->load(GlobalDefaultsClass::class);
198198
$this->assertCount(4, $routes);
@@ -213,7 +213,7 @@ public function testGlobalDefaultsRoutesLoadWithAnnotation()
213213
$this->assertSame(['https'], $routes->get('redundant_scheme')->getSchemes());
214214
}
215215

216-
public function testUtf8RoutesLoadWithAnnotation()
216+
public function testUtf8RoutesLoadWithAttribute()
217217
{
218218
$routes = $this->loader->load(Utf8ActionControllers::class);
219219
$this->assertSame(['one', 'two'], array_keys($routes->all()));

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function testImportAttributesWithPsr4Prefix(string $configFile)
337337
$loader = new PhpFileLoader($locator),
338338
new Psr4DirectoryLoader($locator),
339339
new class extends AttributeClassLoader {
340-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
340+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
341341
{
342342
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
343343
}
@@ -362,7 +362,7 @@ public function testImportAttributesFromClass()
362362
new LoaderResolver([
363363
$loader = new PhpFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')),
364364
new class extends AttributeClassLoader {
365-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
365+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
366366
{
367367
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
368368
}

Tests/Loader/Psr4DirectoryLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function getLoader(): DelegatingLoader
106106
new LoaderResolver([
107107
new Psr4DirectoryLoader($locator),
108108
new class extends AttributeClassLoader {
109-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
109+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
110110
{
111111
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
112112
}

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public function testImportAttributesWithPsr4Prefix(string $configFile)
617617
$loader = new XmlFileLoader($locator),
618618
new Psr4DirectoryLoader($locator),
619619
new class extends AttributeClassLoader {
620-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
620+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
621621
{
622622
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
623623
}
@@ -642,7 +642,7 @@ public function testImportAttributesFromClass()
642642
new LoaderResolver([
643643
$loader = new XmlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')),
644644
new class extends AttributeClassLoader {
645-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
645+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
646646
{
647647
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
648648
}

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public function testPriorityWithPrefix()
475475
new LoaderResolver([
476476
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')),
477477
new class extends AttributeClassLoader {
478-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
478+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
479479
{
480480
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
481481
}
@@ -499,7 +499,7 @@ public function testImportAttributesWithPsr4Prefix(string $configFile)
499499
$loader = new YamlFileLoader($locator),
500500
new Psr4DirectoryLoader($locator),
501501
new class extends AttributeClassLoader {
502-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
502+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
503503
{
504504
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
505505
}
@@ -524,7 +524,7 @@ public function testImportAttributesFromClass()
524524
new LoaderResolver([
525525
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')),
526526
new class extends AttributeClassLoader {
527-
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
527+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $attr): void
528528
{
529529
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
530530
}

0 commit comments

Comments
 (0)