2626 */
2727class RestActionReader
2828{
29+ const COLLECTION_ROUTE_PREFIX = 'c ' ;
30+
2931 private $ annotationReader ;
3032 private $ paramReader ;
3133 private $ inflector ;
@@ -150,8 +152,8 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
150152 return ;
151153 }
152154
153- list ($ httpMethod , $ resources ) = $ httpMethodAndResources ;
154- $ arguments = $ this ->getMethodArguments ($ method );
155+ list ($ httpMethod , $ resources, $ isCollection , $ isInflectable ) = $ httpMethodAndResources ;
156+ $ arguments = $ this ->getMethodArguments ($ method );
155157
156158 // if we have only 1 resource & 1 argument passed, then it's object call, so
157159 // we can set collection singular name
@@ -234,8 +236,10 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
234236 }
235237 }
236238 // add route to collection
237- $ collection ->add ($ routeName .$ annotation ->getName (), new Route (
238- $ pattern , $ defaults , $ requirements , $ options , $ host , $ schemes , null , $ condition ));
239+ $ route = new Route (
240+ $ pattern , $ defaults , $ requirements , $ options , $ host , $ schemes , null , $ condition
241+ );
242+ $ this ->addRoute ($ collection , $ routeName , $ route , $ isCollection , $ isInflectable , $ annotation );
239243 }
240244
241245 } else {
@@ -247,8 +251,10 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
247251 }
248252 }
249253 // add route to collection
250- $ collection ->add ($ routeName , new Route (
251- $ pattern , $ defaults , $ requirements , $ options , $ host , $ schemes , null , $ condition ));
254+ $ route = new Route (
255+ $ pattern , $ defaults , $ requirements , $ options , $ host , $ schemes , null , $ condition
256+ );
257+ $ this ->addRoute ($ collection , $ routeName , $ route , $ isCollection , $ isInflectable );
252258 }
253259 }
254260
@@ -300,19 +306,24 @@ private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method,
300306 $ resources = preg_split (
301307 '/([A-Z][^A-Z]*)/ ' , $ matches [2 ], -1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
302308 );
309+ $ isCollection = false ;
310+ $ isInflectable = true ;
303311
304- if (0 === strpos ($ httpMethod , ' c ' )
312+ if (0 === strpos ($ httpMethod , self :: COLLECTION_ROUTE_PREFIX )
305313 && in_array (substr ($ httpMethod , 1 ), $ this ->availableHTTPMethods )
306314 ) {
315+ $ isCollection = true ;
307316 $ httpMethod = substr ($ httpMethod , 1 );
308317 if (!empty ($ resource )) {
309- $ resource [count ($ resource )-1 ] = $ this ->inflector ->pluralize (end ($ resource ));
318+ $ resourcePluralized = $ this ->inflector ->pluralize (end ($ resource ));
319+ $ isInflectable = ($ resourcePluralized != $ resource [count ($ resource ) - 1 ]);
320+ $ resource [count ($ resource )-1 ] = $ resourcePluralized ;
310321 }
311322 }
312323
313324 $ resources = array_merge ($ resource , $ resources );
314325
315- return array ($ httpMethod , $ resources );
326+ return array ($ httpMethod , $ resources, $ isCollection , $ isInflectable );
316327 }
317328
318329 /**
@@ -527,4 +538,27 @@ private function readMethodAnnotations(\ReflectionMethod $reflection, $annotatio
527538 }
528539 return $ annotations ;
529540 }
541+
542+ /**
543+ * @param RestRouteCollection $collection
544+ * @param $routeName
545+ * @param $route
546+ * @param $isCollection
547+ * @param null $annotation
548+ */
549+ private function addRoute (RestRouteCollection $ collection , $ routeName , $ route , $ isCollection , $ isInflectable , $ annotation = null )
550+ {
551+ if ($ annotation ) {
552+ $ routeName = $ routeName .$ annotation ->getName ();
553+ }
554+
555+ if ($ isCollection && !$ isInflectable ) {
556+ $ collection ->add (self ::COLLECTION_ROUTE_PREFIX .$ routeName , $ route );
557+ if (!$ collection ->get ($ routeName )) {
558+ $ collection ->add ($ routeName , $ route );
559+ }
560+ } else {
561+ $ collection ->add ($ routeName , $ route );
562+ }
563+ }
530564}
0 commit comments