14
14
use Symfony \Component \Config \Loader \LoaderInterface ;
15
15
use Symfony \Component \Config \Loader \LoaderResolverInterface ;
16
16
use Symfony \Component \Config \Resource \FileResource ;
17
- use Symfony \Component \Routing \Attribute \Route as RouteAnnotation ;
17
+ use Symfony \Component \Routing \Attribute \Route as RouteAttribute ;
18
18
use Symfony \Component \Routing \Exception \LogicException ;
19
19
use Symfony \Component \Routing \Route ;
20
20
use Symfony \Component \Routing \RouteCollection ;
53
53
*/
54
54
abstract class AttributeClassLoader implements LoaderInterface
55
55
{
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;
57
61
protected int $ defaultRouteIndex = 0 ;
58
62
59
63
public function __construct (
@@ -62,11 +66,24 @@ public function __construct(
62
66
}
63
67
64
68
/**
69
+ * @deprecated since Symfony 7.2, use "setRouteAttributeClass(string $class)" instead
70
+ *
65
71
* Sets the annotation class to read route properties from.
66
72
*/
67
73
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
68
84
{
69
85
$ this ->routeAnnotationClass = $ class ;
86
+ $ this ->routeAttributeClass = $ class ;
70
87
}
71
88
72
89
/**
@@ -93,8 +110,8 @@ public function load(mixed $class, ?string $type = null): RouteCollection
93
110
foreach ($ class ->getMethods () as $ method ) {
94
111
$ this ->defaultRouteIndex = 0 ;
95
112
$ 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 );
98
115
if ('__invoke ' === $ method ->name ) {
99
116
$ fqcnAlias = true ;
100
117
}
@@ -109,8 +126,8 @@ public function load(mixed $class, ?string $type = null): RouteCollection
109
126
}
110
127
if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
111
128
$ 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 ' ));
114
131
$ fqcnAlias = true ;
115
132
}
116
133
}
@@ -129,36 +146,36 @@ public function load(mixed $class, ?string $type = null): RouteCollection
129
146
}
130
147
131
148
/**
132
- * @param RouteAnnotation $annot or an object that exposes a similar interface
149
+ * @param RouteAttribute $attr or an object that exposes a similar interface
133
150
*/
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
135
152
{
136
- if ($ annot ->getEnv () && $ annot ->getEnv () !== $ this ->env ) {
153
+ if ($ attr ->getEnv () && $ attr ->getEnv () !== $ this ->env ) {
137
154
return ;
138
155
}
139
156
140
- $ name = $ annot ->getName () ?? $ this ->getDefaultRouteName ($ class , $ method );
157
+ $ name = $ attr ->getName () ?? $ this ->getDefaultRouteName ($ class , $ method );
141
158
$ name = $ globals ['name ' ].$ name ;
142
159
143
- $ requirements = $ annot ->getRequirements ();
160
+ $ requirements = $ attr ->getRequirements ();
144
161
145
162
foreach ($ requirements as $ placeholder => $ requirement ) {
146
163
if (\is_int ($ placeholder )) {
147
164
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 ()));
148
165
}
149
166
}
150
167
151
- $ defaults = array_replace ($ globals ['defaults ' ], $ annot ->getDefaults ());
168
+ $ defaults = array_replace ($ globals ['defaults ' ], $ attr ->getDefaults ());
152
169
$ 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 ()));
156
173
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 ' ];
160
177
161
- $ path = $ annot ->getLocalizedPaths () ?: $ annot ->getPath ();
178
+ $ path = $ attr ->getLocalizedPaths () ?: $ attr ->getPath ();
162
179
$ prefix = $ globals ['localized_paths ' ] ?: $ globals ['path ' ];
163
180
$ paths = [];
164
181
@@ -204,7 +221,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
204
221
205
222
foreach ($ paths as $ locale => $ path ) {
206
223
$ 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 );
208
225
if (0 !== $ locale ) {
209
226
$ route ->setDefault ('_locale ' , $ locale );
210
227
$ route ->setRequirement ('_locale ' , preg_quote ($ locale ));
@@ -254,49 +271,50 @@ protected function getGlobals(\ReflectionClass $class): array
254
271
{
255
272
$ globals = $ this ->resetGlobals ();
256
273
274
+ // to be replaced in Symfony 8.0 by $this->routeAttributeClass
257
275
if ($ attribute = $ class ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF )[0 ] ?? null ) {
258
- $ annot = $ attribute ->newInstance ();
276
+ $ attr = $ attribute ->newInstance ();
259
277
260
- if (null !== $ annot ->getName ()) {
261
- $ globals ['name ' ] = $ annot ->getName ();
278
+ if (null !== $ attr ->getName ()) {
279
+ $ globals ['name ' ] = $ attr ->getName ();
262
280
}
263
281
264
- if (null !== $ annot ->getPath ()) {
265
- $ globals ['path ' ] = $ annot ->getPath ();
282
+ if (null !== $ attr ->getPath ()) {
283
+ $ globals ['path ' ] = $ attr ->getPath ();
266
284
}
267
285
268
- $ globals ['localized_paths ' ] = $ annot ->getLocalizedPaths ();
286
+ $ globals ['localized_paths ' ] = $ attr ->getLocalizedPaths ();
269
287
270
- if (null !== $ annot ->getRequirements ()) {
271
- $ globals ['requirements ' ] = $ annot ->getRequirements ();
288
+ if (null !== $ attr ->getRequirements ()) {
289
+ $ globals ['requirements ' ] = $ attr ->getRequirements ();
272
290
}
273
291
274
- if (null !== $ annot ->getOptions ()) {
275
- $ globals ['options ' ] = $ annot ->getOptions ();
292
+ if (null !== $ attr ->getOptions ()) {
293
+ $ globals ['options ' ] = $ attr ->getOptions ();
276
294
}
277
295
278
- if (null !== $ annot ->getDefaults ()) {
279
- $ globals ['defaults ' ] = $ annot ->getDefaults ();
296
+ if (null !== $ attr ->getDefaults ()) {
297
+ $ globals ['defaults ' ] = $ attr ->getDefaults ();
280
298
}
281
299
282
- if (null !== $ annot ->getSchemes ()) {
283
- $ globals ['schemes ' ] = $ annot ->getSchemes ();
300
+ if (null !== $ attr ->getSchemes ()) {
301
+ $ globals ['schemes ' ] = $ attr ->getSchemes ();
284
302
}
285
303
286
- if (null !== $ annot ->getMethods ()) {
287
- $ globals ['methods ' ] = $ annot ->getMethods ();
304
+ if (null !== $ attr ->getMethods ()) {
305
+ $ globals ['methods ' ] = $ attr ->getMethods ();
288
306
}
289
307
290
- if (null !== $ annot ->getHost ()) {
291
- $ globals ['host ' ] = $ annot ->getHost ();
308
+ if (null !== $ attr ->getHost ()) {
309
+ $ globals ['host ' ] = $ attr ->getHost ();
292
310
}
293
311
294
- if (null !== $ annot ->getCondition ()) {
295
- $ globals ['condition ' ] = $ annot ->getCondition ();
312
+ if (null !== $ attr ->getCondition ()) {
313
+ $ globals ['condition ' ] = $ attr ->getCondition ();
296
314
}
297
315
298
- $ globals ['priority ' ] = $ annot ->getPriority () ?? 0 ;
299
- $ globals ['env ' ] = $ annot ->getEnv ();
316
+ $ globals ['priority ' ] = $ attr ->getPriority () ?? 0 ;
317
+ $ globals ['env ' ] = $ attr ->getEnv ();
300
318
301
319
foreach ($ globals ['requirements ' ] as $ placeholder => $ requirement ) {
302
320
if (\is_int ($ placeholder )) {
@@ -332,15 +350,18 @@ protected function createRoute(string $path, array $defaults, array $requirement
332
350
}
333
351
334
352
/**
353
+ * @param RouteAttribute $attr or an object that exposes a similar interface
354
+ *
335
355
* @return void
336
356
*/
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 );
338
358
339
359
/**
340
- * @return iterable<int, RouteAnnotation >
360
+ * @return iterable<int, RouteAttribute >
341
361
*/
342
- private function getAnnotations (\ReflectionClass |\ReflectionMethod $ reflection ): iterable
362
+ private function getAttributes (\ReflectionClass |\ReflectionMethod $ reflection ): iterable
343
363
{
364
+ // to be replaced in Symfony 8.0 by $this->routeAttributeClass
344
365
foreach ($ reflection ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
345
366
yield $ attribute ->newInstance ();
346
367
}
0 commit comments