@@ -72,23 +72,55 @@ public function match(\$pathinfo)
7272 private function compileRoutes (RouteCollection $ routes , $ supportsRedirections , $ parentPrefix = null )
7373 {
7474 $ code = array ();
75- foreach ($ routes as $ name => $ route ) {
75+
76+ $ routeIterator = $ routes ->getIterator ();
77+ $ keys = array_keys ($ routeIterator ->getArrayCopy ());
78+
79+ $ i = 0 ;
80+
81+ foreach ($ routeIterator as $ name => $ route ) {
82+ $ i ++;
83+
7684 if ($ route instanceof RouteCollection) {
7785 $ prefix = $ route ->getPrefix ();
7886 $ optimizable = $ prefix && count ($ route ->all ()) > 1 && false === strpos ($ route ->getPrefix (), '{ ' );
7987 $ indent = '' ;
8088 if ($ optimizable ) {
89+ for ($ j = $ i ; $ j < count ($ keys ); $ j ++) {
90+ if ($ keys [$ j ] === null ) continue ;
91+
92+ $ testRoute = $ routeIterator ->offsetGet ($ keys [$ j ]);
93+ $ isCollection = ($ testRoute instanceof RouteCollection);
94+
95+ $ testPrefix = $ isCollection ? $ testRoute ->getPrefix () : $ testRoute ->getPattern ();
96+
97+ if (0 === strpos ($ testPrefix , $ prefix )) {
98+ $ routeIterator ->offsetUnset ($ keys [$ j ]);
99+
100+ if ($ isCollection ) {
101+ $ route ->addCollection ($ testRoute );
102+ }
103+ else {
104+ $ route ->add ($ keys [$ j ], $ testRoute );
105+ }
106+
107+ $ i ++;
108+ $ keys [$ j ] = null ;
109+ }
110+ }
111+
81112 $ code [] = sprintf (" if (0 === strpos( \$pathinfo, '%s')) { " , $ prefix );
82113 $ indent = ' ' ;
83114 }
84-
115+
85116 foreach ($ this ->compileRoutes ($ route , $ supportsRedirections , $ prefix ) as $ line ) {
86117 foreach (explode ("\n" , $ line ) as $ l ) {
87118 $ code [] = $ indent .$ l ;
88119 }
89120 }
90121
91122 if ($ optimizable ) {
123+ $ code [] = " throw 0 < count( \$allow) ? new MethodNotAllowedException(array_unique( \$allow)) : new ResourceNotFoundException(); " ;
92124 $ code [] = " } \n" ;
93125 }
94126 } else {
0 commit comments