1414use Symfony \Component \Config \Loader \LoaderInterface ;
1515use Symfony \Component \Config \Loader \LoaderResolverInterface ;
1616use Symfony \Component \Config \Resource \FileResource ;
17- use Symfony \Component \Routing \Attribute \Route as RouteAnnotation ;
17+ use Symfony \Component \Routing \Attribute \Route as RouteAttribute ;
1818use Symfony \Component \Routing \Exception \LogicException ;
1919use Symfony \Component \Routing \Route ;
2020use Symfony \Component \Routing \RouteCollection ;
5353 */
5454abstract 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 }
0 commit comments