@@ -55,7 +55,7 @@ public function dump(array $options = array())
55
55
private function addMatcher ($ supportsRedirections )
56
56
{
57
57
// we need to deep clone the routes as we will modify the structure to optimize the dump
58
- $ code = implode ( "\n" , $ this ->compileRoutes (clone $ this ->getRoutes (), $ supportsRedirections ));
58
+ $ code = rtrim ( $ this ->compileRoutes (clone $ this ->getRoutes (), $ supportsRedirections ), "\n" );
59
59
60
60
return <<<EOF
61
61
@@ -65,6 +65,7 @@ public function match(\$pathinfo)
65
65
\$pathinfo = urldecode( \$pathinfo);
66
66
67
67
$ code
68
+
68
69
throw 0 < count( \$allow) ? new MethodNotAllowedException(array_unique( \$allow)) : new ResourceNotFoundException();
69
70
}
70
71
@@ -73,7 +74,7 @@ public function match(\$pathinfo)
73
74
74
75
private function compileRoutes (RouteCollection $ routes , $ supportsRedirections , $ parentPrefix = null )
75
76
{
76
- $ code = array () ;
77
+ $ code = '' ;
77
78
78
79
$ routeIterator = $ routes ->getIterator ();
79
80
$ keys = array_keys ($ routeIterator ->getArrayCopy ());
@@ -86,10 +87,11 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
86
87
if ($ route instanceof RouteCollection) {
87
88
$ prefix = $ route ->getPrefix ();
88
89
$ optimizable = $ prefix && count ($ route ->all ()) > 1 && false === strpos ($ route ->getPrefix (), '{ ' );
89
- $ indent = '' ;
90
+ $ optimized = false ;
91
+
90
92
if ($ optimizable ) {
91
93
for ($ j = $ i ; $ j < $ keysCount ; $ j ++) {
92
- if ($ keys [$ j ] === null ) {
94
+ if (null === $ keys [$ j ]) {
93
95
continue ;
94
96
}
95
97
@@ -113,28 +115,25 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
113
115
}
114
116
115
117
if ($ prefix !== $ parentPrefix ) {
116
- $ code[] = sprintf (" if (0 === strpos( \$pathinfo, %s)) { " , var_export ($ prefix , true ));
117
- $ indent = ' ' ;
118
+ $ code . = sprintf (" if (0 === strpos( \$pathinfo, %s)) { \n " , var_export ($ prefix , true ));
119
+ $ optimized = true ;
118
120
}
119
121
}
120
122
121
- foreach ($ this ->compileRoutes ($ route , $ supportsRedirections , $ prefix ) as $ line ) {
122
- foreach (explode ("\n" , $ line ) as $ l ) {
123
- if ($ l ) {
124
- $ code [] = $ indent .$ l ;
125
- } else {
126
- $ code [] = $ l ;
123
+ if ($ optimized ) {
124
+ foreach (explode ("\n" , $ this ->compileRoutes ($ route , $ supportsRedirections , $ prefix )) as $ line ) {
125
+ if ('' !== $ line ) {
126
+ $ code .= ' ' ; // apply extra indention
127
127
}
128
+ $ code .= $ line ."\n" ;
128
129
}
129
- }
130
-
131
- if ( $ optimizable && $ prefix !== $ parentPrefix ) {
132
- $ code[] = " } \n" ;
130
+ $ code = substr ( $ code , 0 , - 2 ); // remove redundant last two line breaks
131
+ $ code .= " } \n\n" ;
132
+ } else {
133
+ $ code .= $ this -> compileRoutes ( $ route , $ supportsRedirections , $ prefix ) ;
133
134
}
134
135
} else {
135
- foreach ($ this ->compileRoute ($ route , $ name , $ supportsRedirections , $ parentPrefix ) as $ line ) {
136
- $ code [] = $ line ;
137
- }
136
+ $ code .= $ this ->compileRoute ($ route , $ name , $ supportsRedirections , $ parentPrefix )."\n" ;
138
137
}
139
138
}
140
139
@@ -143,19 +142,21 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
143
142
144
143
private function compileRoute (Route $ route , $ name , $ supportsRedirections , $ parentPrefix = null )
145
144
{
146
- $ code = array () ;
145
+ $ code = '' ;
147
146
$ compiledRoute = $ route ->compile ();
148
147
$ conditions = array ();
149
148
$ hasTrailingSlash = false ;
150
149
$ matches = false ;
151
150
$ methods = array ();
151
+
152
152
if ($ req = $ route ->getRequirement ('_method ' )) {
153
153
$ methods = explode ('| ' , strtoupper ($ req ));
154
154
// GET and HEAD are equivalent
155
155
if (in_array ('GET ' , $ methods ) && !in_array ('HEAD ' , $ methods )) {
156
156
$ methods [] = 'HEAD ' ;
157
157
}
158
158
}
159
+
159
160
$ supportsTrailingSlash = $ supportsRedirections && (!$ methods || in_array ('HEAD ' , $ methods ));
160
161
161
162
if (!count ($ compiledRoute ->getVariables ()) && false !== preg_match ('#^(.)\^(?P<url>.*?)\$\1# ' , str_replace (array ("\n" , ' ' ), '' , $ compiledRoute ->getRegex ()), $ m )) {
@@ -182,74 +183,75 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
182
183
183
184
$ conditions = implode (' && ' , $ conditions );
184
185
185
- $ gotoname = 'not_ ' .preg_replace ('/[^A-Za-z0-9_]/ ' , '' , $ name );
186
-
187
- $ code [] = <<<EOF
186
+ $ code .= <<<EOF
188
187
// $ name
189
188
if ( $ conditions) {
189
+
190
190
EOF ;
191
191
192
192
if ($ methods ) {
193
+ $ gotoname = 'not_ ' .preg_replace ('/[^A-Za-z0-9_]/ ' , '' , $ name );
194
+
193
195
if (1 === count ($ methods )) {
194
- $ code[] = <<<EOF
196
+ $ code . = <<<EOF
195
197
if ( \$this->context->getMethod() != ' $ methods [0 ]') {
196
198
\$allow[] = ' $ methods [0 ]';
197
199
goto $ gotoname;
198
200
}
201
+
199
202
EOF ;
200
203
} else {
201
- $ methods = implode ('\' , \'' , $ methods );
202
- $ code[] = <<<EOF
204
+ $ methods = implode (" ', ' " , $ methods );
205
+ $ code . = <<<EOF
203
206
if (!in_array( \$this->context->getMethod(), array(' $ methods'))) {
204
207
\$allow = array_merge( \$allow, array(' $ methods'));
205
208
goto $ gotoname;
206
209
}
210
+
207
211
EOF ;
208
212
}
209
213
}
210
214
211
215
if ($ hasTrailingSlash ) {
212
- $ code[] = sprintf ( <<<EOF
216
+ $ code .= <<<EOF
213
217
if (substr( \$pathinfo, -1) !== '/') {
214
- return \$this->redirect( \$pathinfo.'/', '%s ');
218
+ return \$this->redirect( \$pathinfo.'/', ' $ name ');
215
219
}
216
- EOF
217
- , $ name ) ;
220
+
221
+ EOF ;
218
222
}
219
223
220
224
if ($ scheme = $ route ->getRequirement ('_scheme ' )) {
221
225
if (!$ supportsRedirections ) {
222
226
throw new \LogicException ('The "_scheme" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface. ' );
223
227
}
224
228
225
- $ code[] = sprintf ( <<<EOF
229
+ $ code .= <<<EOF
226
230
if ( \$this->context->getScheme() !== ' $ scheme') {
227
- return \$this->redirect( \$pathinfo, '%s ', ' $ scheme');
231
+ return \$this->redirect( \$pathinfo, ' $ name ', ' $ scheme');
228
232
}
229
- EOF
230
- , $ name ) ;
233
+
234
+ EOF ;
231
235
}
232
236
233
237
// optimize parameters array
234
238
if (true === $ matches && $ compiledRoute ->getDefaults ()) {
235
- $ code[] = sprintf (" return array_merge( \$this->mergeDefaults( \$matches, %s), array('_route' => '%s')); "
239
+ $ code . = sprintf (" return array_merge( \$this->mergeDefaults( \$matches, %s), array('_route' => '%s')); \n "
236
240
, str_replace ("\n" , '' , var_export ($ compiledRoute ->getDefaults (), true )), $ name );
237
241
} elseif (true === $ matches ) {
238
- $ code[] = sprintf (" \$matches['_route'] = '%s'; " , $ name );
239
- $ code[] = sprintf ( " return \$matches; " , $ name ) ;
242
+ $ code . = sprintf (" \$matches['_route'] = '%s'; \n " , $ name );
243
+ $ code .= " return \$matches; \n" ;
240
244
} elseif ($ compiledRoute ->getDefaults ()) {
241
- $ code[] = sprintf (' return %s; ' , str_replace ("\n" , '' , var_export (array_merge ($ compiledRoute ->getDefaults (), array ('_route ' => $ name )), true )));
245
+ $ code . = sprintf (" return %s; \n" , str_replace ("\n" , '' , var_export (array_merge ($ compiledRoute ->getDefaults (), array ('_route ' => $ name )), true )));
242
246
} else {
243
- $ code[] = sprintf (" return array('_route' => '%s'); " , $ name );
247
+ $ code . = sprintf (" return array('_route' => '%s'); \n " , $ name );
244
248
}
245
- $ code[] = " } " ;
249
+ $ code . = " } \n " ;
246
250
247
251
if ($ methods ) {
248
- $ code[] = " $ gotoname: " ;
252
+ $ code . = " $ gotoname: \n " ;
249
253
}
250
254
251
- $ code [] = '' ;
252
-
253
255
return $ code ;
254
256
}
255
257
0 commit comments