@@ -72,23 +72,55 @@ public function match(\$pathinfo)
72
72
private function compileRoutes (RouteCollection $ routes , $ supportsRedirections , $ parentPrefix = null )
73
73
{
74
74
$ 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
+
76
84
if ($ route instanceof RouteCollection) {
77
85
$ prefix = $ route ->getPrefix ();
78
86
$ optimizable = $ prefix && count ($ route ->all ()) > 1 && false === strpos ($ route ->getPrefix (), '{ ' );
79
87
$ indent = '' ;
80
88
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
+
81
112
$ code [] = sprintf (" if (0 === strpos( \$pathinfo, '%s')) { " , $ prefix );
82
113
$ indent = ' ' ;
83
114
}
84
-
115
+
85
116
foreach ($ this ->compileRoutes ($ route , $ supportsRedirections , $ prefix ) as $ line ) {
86
117
foreach (explode ("\n" , $ line ) as $ l ) {
87
118
$ code [] = $ indent .$ l ;
88
119
}
89
120
}
90
121
91
122
if ($ optimizable ) {
123
+ $ code [] = " throw 0 < count( \$allow) ? new MethodNotAllowedException(array_unique( \$allow)) : new ResourceNotFoundException(); " ;
92
124
$ code [] = " } \n" ;
93
125
}
94
126
} else {
0 commit comments