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 ;
52
51
*/
53
52
abstract class AnnotationClassLoader implements LoaderInterface
54
53
{
55
- /**
56
- * @var Reader|null
57
- *
58
- * @deprecated in Symfony 6.4, this property will be removed in Symfony 7.
59
- */
60
- protected $ reader ;
61
-
62
- /**
63
- * @var string|null
64
- */
65
- protected $ env ;
66
-
67
- /**
68
- * @var string
69
- */
70
- protected $ routeAnnotationClass = RouteAnnotation::class;
71
-
72
- /**
73
- * @var int
74
- */
75
- protected $ defaultRouteIndex = 0 ;
76
-
77
- private bool $ hasDeprecatedAnnotations = false ;
78
-
79
- /**
80
- * @param string|null $env
81
- */
82
- public function __construct ($ env = null )
83
- {
84
- if ($ env instanceof Reader || null === $ env && \func_num_args () > 1 && null !== func_get_arg (1 )) {
85
- 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__ );
54
+ protected string $ routeAnnotationClass = RouteAnnotation::class;
55
+ protected int $ defaultRouteIndex = 0 ;
86
56
87
- $ this ->reader = $ env ;
88
- $ env = \func_num_args () > 1 ? func_get_arg (1 ) : null ;
89
- }
90
-
91
- if (\is_string ($ env ) || null === $ env ) {
92
- $ this ->env = $ env ;
93
- } elseif ($ env instanceof \Stringable || \is_scalar ($ env )) {
94
- $ this ->env = (string ) $ env ;
95
- } else {
96
- throw new \TypeError (__METHOD__ .sprintf (': Parameter $env was expected to be a string or null, "%s" given. ' , get_debug_type ($ env )));
97
- }
57
+ public function __construct (
58
+ protected readonly ?string $ env = null ,
59
+ ) {
98
60
}
99
61
100
62
/**
@@ -121,48 +83,38 @@ public function load(mixed $class, string $type = null): RouteCollection
121
83
throw new \InvalidArgumentException (sprintf ('Annotations from class "%s" cannot be read as it is abstract. ' , $ class ->getName ()));
122
84
}
123
85
124
- $ this ->hasDeprecatedAnnotations = false ;
125
-
126
- try {
127
- $ globals = $ this ->getGlobals ($ class );
128
- $ collection = new RouteCollection ();
129
- $ collection ->addResource (new FileResource ($ class ->getFileName ()));
130
- if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
131
- return $ collection ;
132
- }
133
- $ fqcnAlias = false ;
134
- foreach ($ class ->getMethods () as $ method ) {
135
- $ this ->defaultRouteIndex = 0 ;
136
- $ routeNamesBefore = array_keys ($ collection ->all ());
137
- foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
138
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
139
- if ('__invoke ' === $ method ->name ) {
140
- $ fqcnAlias = true ;
141
- }
142
- }
143
-
144
- if (1 === $ collection ->count () - \count ($ routeNamesBefore )) {
145
- $ newRouteName = current (array_diff (array_keys ($ collection ->all ()), $ routeNamesBefore ));
146
- $ collection ->addAlias (sprintf ('%s::%s ' , $ class ->name , $ method ->name ), $ newRouteName );
147
- }
148
- }
149
- if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
150
- $ globals = $ this ->resetGlobals ();
151
- foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
152
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
86
+ $ globals = $ this ->getGlobals ($ class );
87
+ $ collection = new RouteCollection ();
88
+ $ collection ->addResource (new FileResource ($ class ->getFileName ()));
89
+ if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
90
+ return $ collection ;
91
+ }
92
+ $ fqcnAlias = false ;
93
+ foreach ($ class ->getMethods () as $ method ) {
94
+ $ this ->defaultRouteIndex = 0 ;
95
+ $ routeNamesBefore = array_keys ($ collection ->all ());
96
+ foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
97
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
98
+ if ('__invoke ' === $ method ->name ) {
153
99
$ fqcnAlias = true ;
154
100
}
155
101
}
156
- if ($ fqcnAlias && 1 === $ collection ->count ()) {
157
- $ collection ->addAlias ($ class ->name , $ invokeRouteName = key ($ collection ->all ()));
158
- $ collection ->addAlias (sprintf ('%s::__invoke ' , $ class ->name ), $ invokeRouteName );
159
- }
160
102
161
- if ($ this ->hasDeprecatedAnnotations ) {
162
- trigger_deprecation ('symfony/routing ' , '6.4 ' , 'Class "%s" uses Doctrine Annotations to configure routes, which is deprecated. Use PHP attributes instead. ' , $ class ->getName ());
103
+ if (1 === $ collection ->count () - \count ($ routeNamesBefore )) {
104
+ $ newRouteName = current (array_diff (array_keys ($ collection ->all ()), $ routeNamesBefore ));
105
+ $ collection ->addAlias (sprintf ('%s::%s ' , $ class ->name , $ method ->name ), $ newRouteName );
163
106
}
164
- } finally {
165
- $ this ->hasDeprecatedAnnotations = false ;
107
+ }
108
+ if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
109
+ $ globals = $ this ->resetGlobals ();
110
+ foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
111
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
112
+ $ fqcnAlias = true ;
113
+ }
114
+ }
115
+ if ($ fqcnAlias && 1 === $ collection ->count ()) {
116
+ $ collection ->addAlias ($ class ->name , $ invokeRouteName = key ($ collection ->all ()));
117
+ $ collection ->addAlias (sprintf ('%s::__invoke ' , $ class ->name ), $ invokeRouteName );
166
118
}
167
119
168
120
return $ collection ;
@@ -291,15 +243,9 @@ protected function getGlobals(\ReflectionClass $class): array
291
243
{
292
244
$ globals = $ this ->resetGlobals ();
293
245
294
- $ annot = null ;
295
246
if ($ attribute = $ class ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF )[0 ] ?? null ) {
296
247
$ annot = $ attribute ->newInstance ();
297
- }
298
- if (!$ annot && $ annot = $ this ->reader ?->getClassAnnotation($ class , $ this ->routeAnnotationClass )) {
299
- $ this ->hasDeprecatedAnnotations = true ;
300
- }
301
248
302
- if ($ annot ) {
303
249
if (null !== $ annot ->getName ()) {
304
250
$ globals ['name ' ] = $ annot ->getName ();
305
251
}
@@ -387,21 +333,5 @@ private function getAnnotations(\ReflectionClass|\ReflectionMethod $reflection):
387
333
foreach ($ reflection ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
388
334
yield $ attribute ->newInstance ();
389
335
}
390
-
391
- if (!$ this ->reader ) {
392
- return ;
393
- }
394
-
395
- $ annotations = $ reflection instanceof \ReflectionClass
396
- ? $ this ->reader ->getClassAnnotations ($ reflection )
397
- : $ this ->reader ->getMethodAnnotations ($ reflection );
398
-
399
- foreach ($ annotations as $ annotation ) {
400
- if ($ annotation instanceof $ this ->routeAnnotationClass ) {
401
- $ this ->hasDeprecatedAnnotations = true ;
402
-
403
- yield $ annotation ;
404
- }
405
- }
406
336
}
407
337
}
0 commit comments