11
11
12
12
namespace Symfony \Component \Routing \Loader ;
13
13
14
- use Doctrine \Common \Annotations \Reader ;
15
14
use Symfony \Component \Config \Loader \LoaderInterface ;
16
15
use Symfony \Component \Config \Loader \LoaderResolverInterface ;
17
16
use Symfony \Component \Config \Resource \FileResource ;
53
52
*/
54
53
abstract class AttributeClassLoader implements LoaderInterface
55
54
{
56
- /**
57
- * @var Reader|null
58
- *
59
- * @deprecated in Symfony 6.4, this property will be removed in Symfony 7.
60
- */
61
- protected $ reader ;
62
-
63
- /**
64
- * @var string|null
65
- */
66
- protected $ env ;
67
-
68
- /**
69
- * @var string
70
- */
71
- protected $ routeAnnotationClass = RouteAnnotation::class;
55
+ protected string $ routeAnnotationClass = RouteAnnotation::class;
56
+ protected int $ defaultRouteIndex = 0 ;
72
57
73
- /**
74
- * @var int
75
- */
76
- protected $ defaultRouteIndex = 0 ;
77
-
78
- private bool $ hasDeprecatedAnnotations = false ;
79
-
80
- /**
81
- * @param string|null $env
82
- */
83
- public function __construct ($ env = null )
84
- {
85
- if ($ env instanceof Reader || null === $ env && \func_num_args () > 1 && null !== func_get_arg (1 )) {
86
- trigger_deprecation ('symfony/routing ' , '6.4 ' , 'Passing an instance of "%s" as first and the environment as second argument to "%s" is deprecated. Pass the environment as first argument instead. ' , Reader::class, __METHOD__ );
87
-
88
- $ this ->reader = $ env ;
89
- $ env = \func_num_args () > 1 ? func_get_arg (1 ) : null ;
90
- }
91
-
92
- if (\is_string ($ env ) || null === $ env ) {
93
- $ this ->env = $ env ;
94
- } elseif ($ env instanceof \Stringable || \is_scalar ($ env )) {
95
- $ this ->env = (string ) $ env ;
96
- } else {
97
- throw new \TypeError (__METHOD__ .sprintf (': Parameter $env was expected to be a string or null, "%s" given. ' , get_debug_type ($ env )));
98
- }
58
+ public function __construct (
59
+ protected readonly ?string $ env = null ,
60
+ ) {
99
61
}
100
62
101
63
/**
102
64
* Sets the annotation class to read route properties from.
103
- *
104
- * @return void
105
65
*/
106
- public function setRouteAnnotationClass (string $ class )
66
+ public function setRouteAnnotationClass (string $ class ): void
107
67
{
108
68
$ this ->routeAnnotationClass = $ class ;
109
69
}
@@ -124,59 +84,47 @@ public function load(mixed $class, string $type = null): RouteCollection
124
84
throw new \InvalidArgumentException (sprintf ('Annotations from class "%s" cannot be read as it is abstract. ' , $ class ->getName ()));
125
85
}
126
86
127
- $ this ->hasDeprecatedAnnotations = false ;
128
-
129
- try {
130
- $ globals = $ this ->getGlobals ($ class );
131
- $ collection = new RouteCollection ();
132
- $ collection ->addResource (new FileResource ($ class ->getFileName ()));
133
- if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
134
- return $ collection ;
135
- }
136
- $ fqcnAlias = false ;
137
- foreach ($ class ->getMethods () as $ method ) {
138
- $ this ->defaultRouteIndex = 0 ;
139
- $ routeNamesBefore = array_keys ($ collection ->all ());
140
- foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
141
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
142
- if ('__invoke ' === $ method ->name ) {
143
- $ fqcnAlias = true ;
144
- }
145
- }
146
-
147
- if (1 === $ collection ->count () - \count ($ routeNamesBefore )) {
148
- $ newRouteName = current (array_diff (array_keys ($ collection ->all ()), $ routeNamesBefore ));
149
- $ collection ->addAlias (sprintf ('%s::%s ' , $ class ->name , $ method ->name ), $ newRouteName );
150
- }
151
- }
152
- if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
153
- $ globals = $ this ->resetGlobals ();
154
- foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
155
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
87
+ $ globals = $ this ->getGlobals ($ class );
88
+ $ collection = new RouteCollection ();
89
+ $ collection ->addResource (new FileResource ($ class ->getFileName ()));
90
+ if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
91
+ return $ collection ;
92
+ }
93
+ $ fqcnAlias = false ;
94
+ foreach ($ class ->getMethods () as $ method ) {
95
+ $ this ->defaultRouteIndex = 0 ;
96
+ $ routeNamesBefore = array_keys ($ collection ->all ());
97
+ foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
98
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
99
+ if ('__invoke ' === $ method ->name ) {
156
100
$ fqcnAlias = true ;
157
101
}
158
102
}
159
- if ($ fqcnAlias && 1 === $ collection ->count ()) {
160
- $ collection ->addAlias ($ class ->name , $ invokeRouteName = key ($ collection ->all ()));
161
- $ collection ->addAlias (sprintf ('%s::__invoke ' , $ class ->name ), $ invokeRouteName );
162
- }
163
103
164
- if ($ this ->hasDeprecatedAnnotations ) {
165
- trigger_deprecation ('symfony/routing ' , '6.4 ' , 'Class "%s" uses Doctrine Annotations to configure routes, which is deprecated. Use PHP attributes instead. ' , $ class ->getName ());
104
+ if (1 === $ collection ->count () - \count ($ routeNamesBefore )) {
105
+ $ newRouteName = current (array_diff (array_keys ($ collection ->all ()), $ routeNamesBefore ));
106
+ $ collection ->addAlias (sprintf ('%s::%s ' , $ class ->name , $ method ->name ), $ newRouteName );
166
107
}
167
- } finally {
168
- $ this ->hasDeprecatedAnnotations = false ;
108
+ }
109
+ if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
110
+ $ globals = $ this ->resetGlobals ();
111
+ foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
112
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
113
+ $ fqcnAlias = true ;
114
+ }
115
+ }
116
+ if ($ fqcnAlias && 1 === $ collection ->count ()) {
117
+ $ collection ->addAlias ($ class ->name , $ invokeRouteName = key ($ collection ->all ()));
118
+ $ collection ->addAlias (sprintf ('%s::__invoke ' , $ class ->name ), $ invokeRouteName );
169
119
}
170
120
171
121
return $ collection ;
172
122
}
173
123
174
124
/**
175
125
* @param RouteAnnotation $annot or an object that exposes a similar interface
176
- *
177
- * @return void
178
126
*/
179
- protected function addRoute (RouteCollection $ collection , object $ annot , array $ globals , \ReflectionClass $ class , \ReflectionMethod $ method )
127
+ protected function addRoute (RouteCollection $ collection , object $ annot , array $ globals , \ReflectionClass $ class , \ReflectionMethod $ method ): void
180
128
{
181
129
if ($ annot ->getEnv () && $ annot ->getEnv () !== $ this ->env ) {
182
130
return ;
@@ -263,11 +211,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
263
211
264
212
public function supports (mixed $ resource , string $ type = null ): bool
265
213
{
266
- if ('annotation ' === $ type ) {
267
- trigger_deprecation ('symfony/routing ' , '6.4 ' , 'The "annotation" route type is deprecated, use the "attribute" route type instead. ' );
268
- }
269
-
270
- return \is_string ($ resource ) && preg_match ('/^(?: \\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/ ' , $ resource ) && (!$ type || \in_array ($ type , ['annotation ' , 'attribute ' ], true ));
214
+ return \is_string ($ resource ) && preg_match ('/^(?: \\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/ ' , $ resource ) && (!$ type || 'attribute ' === $ type );
271
215
}
272
216
273
217
public function setResolver (LoaderResolverInterface $ resolver ): void
@@ -280,10 +224,8 @@ public function getResolver(): LoaderResolverInterface
280
224
281
225
/**
282
226
* Gets the default route name for a class method.
283
- *
284
- * @return string
285
227
*/
286
- protected function getDefaultRouteName (\ReflectionClass $ class , \ReflectionMethod $ method )
228
+ protected function getDefaultRouteName (\ReflectionClass $ class , \ReflectionMethod $ method ): string
287
229
{
288
230
$ name = str_replace ('\\' , '_ ' , $ class ->name ).'_ ' .$ method ->name ;
289
231
$ name = \function_exists ('mb_strtolower ' ) && preg_match ('//u ' , $ name ) ? mb_strtolower ($ name , 'UTF-8 ' ) : strtolower ($ name );
@@ -298,19 +240,13 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
298
240
/**
299
241
* @return array<string, mixed>
300
242
*/
301
- protected function getGlobals (\ReflectionClass $ class )
243
+ protected function getGlobals (\ReflectionClass $ class ): array
302
244
{
303
245
$ globals = $ this ->resetGlobals ();
304
246
305
- $ annot = null ;
306
247
if ($ attribute = $ class ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF )[0 ] ?? null ) {
307
248
$ annot = $ attribute ->newInstance ();
308
- }
309
- if (!$ annot && $ annot = $ this ->reader ?->getClassAnnotation($ class , $ this ->routeAnnotationClass )) {
310
- $ this ->hasDeprecatedAnnotations = true ;
311
- }
312
249
313
- if ($ annot ) {
314
250
if (null !== $ annot ->getName ()) {
315
251
$ globals ['name ' ] = $ annot ->getName ();
316
252
}
@@ -380,18 +316,12 @@ private function resetGlobals(): array
380
316
];
381
317
}
382
318
383
- /**
384
- * @return Route
385
- */
386
- protected function createRoute (string $ path , array $ defaults , array $ requirements , array $ options , ?string $ host , array $ schemes , array $ methods , ?string $ condition )
319
+ protected function createRoute (string $ path , array $ defaults , array $ requirements , array $ options , ?string $ host , array $ schemes , array $ methods , ?string $ condition ): Route
387
320
{
388
321
return new Route ($ path , $ defaults , $ requirements , $ options , $ host , $ schemes , $ methods , $ condition );
389
322
}
390
323
391
- /**
392
- * @return void
393
- */
394
- abstract protected function configureRoute (Route $ route , \ReflectionClass $ class , \ReflectionMethod $ method , object $ annot );
324
+ abstract protected function configureRoute (Route $ route , \ReflectionClass $ class , \ReflectionMethod $ method , object $ annot ): void ;
395
325
396
326
/**
397
327
* @return iterable<int, RouteAnnotation>
@@ -401,25 +331,5 @@ private function getAnnotations(\ReflectionClass|\ReflectionMethod $reflection):
401
331
foreach ($ reflection ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
402
332
yield $ attribute ->newInstance ();
403
333
}
404
-
405
- if (!$ this ->reader ) {
406
- return ;
407
- }
408
-
409
- $ annotations = $ reflection instanceof \ReflectionClass
410
- ? $ this ->reader ->getClassAnnotations ($ reflection )
411
- : $ this ->reader ->getMethodAnnotations ($ reflection );
412
-
413
- foreach ($ annotations as $ annotation ) {
414
- if ($ annotation instanceof $ this ->routeAnnotationClass ) {
415
- $ this ->hasDeprecatedAnnotations = true ;
416
-
417
- yield $ annotation ;
418
- }
419
- }
420
334
}
421
335
}
422
-
423
- if (!class_exists (AnnotationClassLoader::class, false )) {
424
- class_alias (AttributeClassLoader::class, AnnotationClassLoader::class);
425
- }
0 commit comments