@@ -134,7 +134,6 @@ public function match(\$pathinfo)
134
134
private function compileRoutes (RouteCollection $ routes , $ supportsRedirections )
135
135
{
136
136
$ fetchedHost = false ;
137
-
138
137
$ groups = $ this ->groupRoutesByHostRegex ($ routes );
139
138
$ code = '' ;
140
139
@@ -148,8 +147,8 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
148
147
$ code .= sprintf (" if (preg_match(%s, \$host, \$hostMatches)) { \n" , var_export ($ regex , true ));
149
148
}
150
149
151
- $ tree = $ this ->buildPrefixTree ($ collection );
152
- $ groupCode = $ this ->compilePrefixRoutes ($ tree , $ supportsRedirections );
150
+ $ tree = $ this ->buildStaticPrefixCollection ($ collection );
151
+ $ groupCode = $ this ->compileStaticPrefixRoutes ($ tree , $ supportsRedirections );
153
152
154
153
if (null !== $ regex ) {
155
154
// apply extra indention at each line (except empty ones)
@@ -164,37 +163,51 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
164
163
return $ code ;
165
164
}
166
165
166
+ private function buildStaticPrefixCollection (DumperCollection $ collection )
167
+ {
168
+ $ prefixCollection = new StaticPrefixCollection ();
169
+
170
+ foreach ($ collection as $ dumperRoute ) {
171
+ $ prefix = $ dumperRoute ->getRoute ()->compile ()->getStaticPrefix ();
172
+ $ prefixCollection ->addRoute ($ prefix , $ dumperRoute );
173
+ }
174
+
175
+ $ prefixCollection ->optimizeGroups ();
176
+
177
+ return $ prefixCollection ;
178
+ }
179
+
167
180
/**
168
- * Generates PHP code recursively to match a tree of routes.
181
+ * Generates PHP code to match a tree of routes.
169
182
*
170
- * @param DumperPrefixCollection $collection A DumperPrefixCollection instance
183
+ * @param StaticPrefixCollection $collection A StaticPrefixCollection instance
171
184
* @param bool $supportsRedirections Whether redirections are supported by the base class
172
- * @param string $parentPrefix Prefix of the parent collection
185
+ * @param string $ifOrElseIf Either "if" or "elseif" to influence chaining.
173
186
*
174
187
* @return string PHP code
175
188
*/
176
- private function compilePrefixRoutes ( DumperPrefixCollection $ collection , $ supportsRedirections , $ parentPrefix = '' )
189
+ private function compileStaticPrefixRoutes ( StaticPrefixCollection $ collection , $ supportsRedirections , $ ifOrElseIf = 'if ' )
177
190
{
178
191
$ code = '' ;
179
192
$ prefix = $ collection ->getPrefix ();
180
- $ optimizable = 1 < strlen ($ prefix ) && 1 < count ($ collection ->all ());
181
- $ optimizedPrefix = $ parentPrefix ;
182
-
183
- if ($ optimizable ) {
184
- $ optimizedPrefix = $ prefix ;
185
193
186
- $ code .= sprintf (" if (0 === strpos( \$pathinfo, %s)) { \n" , var_export ($ prefix , true ));
194
+ if (!empty ($ prefix ) && '/ ' !== $ prefix ) {
195
+ $ code .= sprintf (" %s (0 === strpos( \$pathinfo, %s)) { \n" , $ ifOrElseIf , var_export ($ prefix , true ));
187
196
}
188
197
189
- foreach ($ collection as $ route ) {
190
- if ($ route instanceof DumperCollection) {
191
- $ code .= $ this ->compilePrefixRoutes ($ route , $ supportsRedirections , $ optimizedPrefix );
198
+ $ ifOrElseIf = 'if ' ;
199
+
200
+ foreach ($ collection ->getItems () as $ route ) {
201
+ if ($ route instanceof StaticPrefixCollection) {
202
+ $ code .= $ this ->compileStaticPrefixRoutes ($ route , $ supportsRedirections , $ ifOrElseIf );
203
+ $ ifOrElseIf = 'elseif ' ;
192
204
} else {
193
- $ code .= $ this ->compileRoute ($ route ->getRoute (), $ route ->getName (), $ supportsRedirections , $ optimizedPrefix )."\n" ;
205
+ $ code .= $ this ->compileRoute ($ route [1 ]->getRoute (), $ route [1 ]->getName (), $ supportsRedirections , $ prefix )."\n" ;
206
+ $ ifOrElseIf = 'if ' ;
194
207
}
195
208
}
196
209
197
- if ($ optimizable ) {
210
+ if (! empty ( $ prefix ) && ' / ' !== $ prefix ) {
198
211
$ code .= " } \n\n" ;
199
212
// apply extra indention at each line (except empty ones)
200
213
$ code = preg_replace ('/^.{2,}$/m ' , ' $0 ' , $ code );
@@ -387,7 +400,6 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
387
400
private function groupRoutesByHostRegex (RouteCollection $ routes )
388
401
{
389
402
$ groups = new DumperCollection ();
390
-
391
403
$ currentGroup = new DumperCollection ();
392
404
$ currentGroup ->setAttribute ('host_regex ' , null );
393
405
$ groups ->add ($ currentGroup );
@@ -405,30 +417,6 @@ private function groupRoutesByHostRegex(RouteCollection $routes)
405
417
return $ groups ;
406
418
}
407
419
408
- /**
409
- * Organizes the routes into a prefix tree.
410
- *
411
- * Routes order is preserved such that traversing the tree will traverse the
412
- * routes in the origin order.
413
- *
414
- * @param DumperCollection $collection A collection of routes
415
- *
416
- * @return DumperPrefixCollection
417
- */
418
- private function buildPrefixTree (DumperCollection $ collection )
419
- {
420
- $ tree = new DumperPrefixCollection ();
421
- $ current = $ tree ;
422
-
423
- foreach ($ collection as $ route ) {
424
- $ current = $ current ->addPrefixRoute ($ route );
425
- }
426
-
427
- $ tree ->mergeSlashNodes ();
428
-
429
- return $ tree ;
430
- }
431
-
432
420
private function getExpressionLanguage ()
433
421
{
434
422
if (null === $ this ->expressionLanguage ) {
0 commit comments