72
72
*/
73
73
abstract class AnnotationClassLoader implements LoaderInterface
74
74
{
75
+ /**
76
+ * @var Reader|null
77
+ * @deprecated in Symfony 6.3, this property will be removed in Symfony 7.
78
+ */
75
79
protected $ reader ;
80
+
81
+ /**
82
+ * @var string|null
83
+ * @internal since Symfony 6.3, this property will be private in Symfony 7.
84
+ */
76
85
protected $ env ;
77
86
78
87
/**
79
88
* @var string
89
+ * @internal since Symfony 6.3, this property will be private in Symfony 7.
80
90
*/
81
91
protected $ routeAnnotationClass = RouteAnnotation::class;
82
92
83
93
/**
84
94
* @var int
95
+ * @internal since Symfony 6.3, this property will be private in Symfony 7.
85
96
*/
86
97
protected $ defaultRouteIndex = 0 ;
87
98
88
- public function __construct (Reader $ reader = null , string $ env = null )
99
+ private bool $ hasDeprecatedAnnotations = false ;
100
+
101
+ /**
102
+ * @param string|null $env
103
+ */
104
+ public function __construct ($ env = null )
89
105
{
90
- $ this ->reader = $ reader ;
91
- $ this ->env = $ env ;
106
+ if ($ env instanceof Reader || func_num_args () > 1 && null !== func_get_arg (1 )) {
107
+ trigger_deprecation ( 'symfony/routing ' , '6.3 ' , '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__ );
108
+
109
+ $ this ->reader = $ env ;
110
+ $ this ->env = func_num_args () > 1 ? func_get_arg (1 ) : null ;
111
+ } elseif (is_string ($ env ) || null === $ env ) {
112
+ $ this ->env = $ env ;
113
+ } elseif ($ env instanceof \Stringable || is_scalar ($ env )) {
114
+ $ this ->env = (string ) $ env ;
115
+ } else {
116
+ throw new \TypeError (__METHOD__ . sprintf (': Parameter #1 ($env) was expected to be a string or null, "%s" given. ' , get_debug_type ($ env )));
117
+ }
92
118
}
93
119
94
120
/**
@@ -117,27 +143,33 @@ public function load(mixed $class, string $type = null): RouteCollection
117
143
throw new \InvalidArgumentException (sprintf ('Annotations from class "%s" cannot be read as it is abstract. ' , $ class ->getName ()));
118
144
}
119
145
120
- $ globals = $ this ->getGlobals ($ class );
121
-
122
- $ collection = new RouteCollection ();
123
- $ collection ->addResource (new FileResource ($ class ->getFileName ()));
146
+ $ this ->hasDeprecatedAnnotations = false ;
124
147
125
- if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
126
- return $ collection ;
127
- }
128
-
129
- foreach ($ class ->getMethods () as $ method ) {
130
- $ this ->defaultRouteIndex = 0 ;
131
- foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
132
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
148
+ try {
149
+ $ globals = $ this ->getGlobals ($ class );
150
+ $ collection = new RouteCollection ();
151
+ $ collection ->addResource (new FileResource ($ class ->getFileName ()));
152
+ if ($ globals ['env ' ] && $ this ->env !== $ globals ['env ' ]) {
153
+ return $ collection ;
154
+ }
155
+ foreach ($ class ->getMethods () as $ method ) {
156
+ $ this ->defaultRouteIndex = 0 ;
157
+ foreach ($ this ->getAnnotations ($ method ) as $ annot ) {
158
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ method );
159
+ }
160
+ }
161
+ if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
162
+ $ globals = $ this ->resetGlobals ();
163
+ foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
164
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
165
+ }
133
166
}
134
- }
135
167
136
- if (0 === $ collection ->count () && $ class ->hasMethod ('__invoke ' )) {
137
- $ globals = $ this ->resetGlobals ();
138
- foreach ($ this ->getAnnotations ($ class ) as $ annot ) {
139
- $ this ->addRoute ($ collection , $ annot , $ globals , $ class , $ class ->getMethod ('__invoke ' ));
168
+ if ($ this ->hasDeprecatedAnnotations ) {
169
+ trigger_deprecation ('symfony/routing ' , '6.3 ' , 'Class "%s" uses Doctrine Annotations to configure routes, which is deprecated. Use PHP attributes instead. ' , $ class ->getName ());
140
170
}
171
+ } finally {
172
+ $ this ->hasDeprecatedAnnotations = false ;
141
173
}
142
174
143
175
return $ collection ;
@@ -263,7 +295,7 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
263
295
}
264
296
265
297
/**
266
- * @return array
298
+ * @return array<string, mixed>
267
299
*/
268
300
protected function getGlobals (\ReflectionClass $ class )
269
301
{
@@ -273,8 +305,8 @@ protected function getGlobals(\ReflectionClass $class)
273
305
if ($ attribute = $ class ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF )[0 ] ?? null ) {
274
306
$ annot = $ attribute ->newInstance ();
275
307
}
276
- if (!$ annot && $ this ->reader ) {
277
- $ annot = $ this ->reader -> getClassAnnotation ( $ class , $ this -> routeAnnotationClass ) ;
308
+ if (!$ annot && $ annot = $ this ->reader ?->getClassAnnotation( $ class , $ this -> routeAnnotationClass ) ) {
309
+ $ this ->hasDeprecatedAnnotations = true ;
278
310
}
279
311
280
312
if ($ annot ) {
@@ -355,14 +387,15 @@ protected function createRoute(string $path, array $defaults, array $requirement
355
387
return new Route ($ path , $ defaults , $ requirements , $ options , $ host , $ schemes , $ methods , $ condition );
356
388
}
357
389
390
+ /**
391
+ * @return void
392
+ */
358
393
abstract protected function configureRoute (Route $ route , \ReflectionClass $ class , \ReflectionMethod $ method , object $ annot );
359
394
360
395
/**
361
- * @param \ReflectionClass|\ReflectionMethod $reflection
362
- *
363
396
* @return iterable<int, RouteAnnotation>
364
397
*/
365
- private function getAnnotations (object $ reflection ): iterable
398
+ private function getAnnotations (\ ReflectionClass | \ ReflectionMethod $ reflection ): iterable
366
399
{
367
400
foreach ($ reflection ->getAttributes ($ this ->routeAnnotationClass , \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
368
401
yield $ attribute ->newInstance ();
@@ -378,6 +411,8 @@ private function getAnnotations(object $reflection): iterable
378
411
379
412
foreach ($ annotations as $ annotation ) {
380
413
if ($ annotation instanceof $ this ->routeAnnotationClass ) {
414
+ $ this ->hasDeprecatedAnnotations = true ;
415
+
381
416
yield $ annotation ;
382
417
}
383
418
}
0 commit comments