58
58
abstract class AnnotationClassLoader implements LoaderInterface
59
59
{
60
60
protected $ reader ;
61
- protected $ annotationClass = 'Symfony \\Component \\Routing \\Annotation \\Route ' ;
61
+ protected $ routeAnnotationClass = 'Symfony \\Component \\Routing \\Annotation \\Route ' ;
62
+ protected $ routesAnnotationClass = 'Symfony \\Component \\Routing \\Annotation \\Routes ' ;
62
63
63
64
/**
64
65
* Constructor.
@@ -73,11 +74,21 @@ public function __construct(AnnotationReader $reader)
73
74
/**
74
75
* Sets the annotation class to read route properties from.
75
76
*
76
- * @param string $annotationClass A fully-qualified class name
77
+ * @param string $class A fully-qualified class name
77
78
*/
78
- public function setAnnotationClass ( $ annotationClass )
79
+ public function setRouteAnnotationClass ( $ class )
79
80
{
80
- $ this ->annotationClass = $ annotationClass ;
81
+ $ this ->routeAnnotationClass = $ class ;
82
+ }
83
+
84
+ /**
85
+ * Sets the annotation class to read routes properties from.
86
+ *
87
+ * @param string $class A fully-qualified class name
88
+ */
89
+ public function setRoutesAnnotationClass ($ class )
90
+ {
91
+ $ this ->routesAnnotationClass = $ class ;
81
92
}
82
93
83
94
/**
@@ -104,7 +115,7 @@ public function load($class, $type = null)
104
115
);
105
116
106
117
$ class = new \ReflectionClass ($ class );
107
- if ($ annot = $ this ->reader ->getClassAnnotation ($ class , $ this ->annotationClass )) {
118
+ if ($ annot = $ this ->reader ->getClassAnnotation ($ class , $ this ->routeAnnotationClass )) {
108
119
if (null !== $ annot ->getPattern ()) {
109
120
$ globals ['pattern ' ] = $ annot ->getPattern ();
110
121
}
@@ -124,25 +135,35 @@ public function load($class, $type = null)
124
135
125
136
$ collection = new RouteCollection ();
126
137
$ collection ->addResource (new FileResource ($ class ->getFileName ()));
138
+
127
139
foreach ($ class ->getMethods () as $ method ) {
128
- if ($ annot = $ this ->reader ->getMethodAnnotation ($ method , $ this ->annotationClass )) {
129
- if ( null === $ annot -> getName () ) {
130
- $ annot -> setName ( $ this ->getDefaultRouteName ( $ class , $ method) );
140
+ if ($ annots = $ this ->reader ->getMethodAnnotation ($ method , $ this ->routesAnnotationClass )) {
141
+ foreach ( $ annots -> getRoutes () as $ annot ) {
142
+ $ this ->addRoute ( $ collection , $ annot , $ globals , $ annot , $ class , $ method );
131
143
}
144
+ } elseif ($ annot = $ this ->reader ->getMethodAnnotation ($ method , $ this ->routeAnnotationClass )) {
145
+ $ this ->addRoute ($ collection , $ annot , $ globals , $ annot , $ class , $ method );
146
+ }
147
+ }
132
148
133
- $ defaults = array_merge ($ globals ['defaults ' ], $ annot ->getDefaults ());
134
- $ requirements = array_merge ($ globals ['requirements ' ], $ annot ->getRequirements ());
135
- $ options = array_merge ($ globals ['options ' ], $ annot ->getOptions ());
149
+ return $ collection ;
150
+ }
151
+
152
+ protected function addRoute (RouteCollection $ collection , $ annot , $ globals , $ annot , \ReflectionClass $ class , \ReflectionMethod $ method )
153
+ {
154
+ if (null === $ annot ->getName ()) {
155
+ $ annot ->setName ($ this ->getDefaultRouteName ($ class , $ method ));
156
+ }
136
157
137
- $ route = new Route ($ globals ['pattern ' ].$ annot ->getPattern (), $ defaults , $ requirements , $ options );
158
+ $ defaults = array_merge ($ globals ['defaults ' ], $ annot ->getDefaults ());
159
+ $ requirements = array_merge ($ globals ['requirements ' ], $ annot ->getRequirements ());
160
+ $ options = array_merge ($ globals ['options ' ], $ annot ->getOptions ());
138
161
139
- $ this -> configureRoute ( $ route , $ class , $ method );
162
+ $ route = new Route ( $ globals [ ' pattern ' ]. $ annot -> getPattern (), $ defaults , $ requirements , $ options );
140
163
141
- $ collection ->add ($ annot ->getName (), $ route );
142
- }
143
- }
164
+ $ this ->configureRoute ($ route , $ class , $ method , $ annot );
144
165
145
- return $ collection ;
166
+ $ collection-> add ( $ annot -> getName (), $ route ) ;
146
167
}
147
168
148
169
/**
@@ -189,5 +210,5 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
189
210
return strtolower (str_replace ('\\' , '_ ' , $ class ->getName ()).'_ ' .$ method ->getName ());
190
211
}
191
212
192
- abstract protected function configureRoute (Route $ route , \ReflectionClass $ class , \ReflectionMethod $ method );
213
+ abstract protected function configureRoute (Route $ route , \ReflectionClass $ class , \ReflectionMethod $ method, $ annot );
193
214
}
0 commit comments