8000 [Routing] PhpDumper should follow coding standards · symfony/symfony@7cf4772 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cf4772

Browse files
committed
[Routing] PhpDumper should follow coding standards
1 parent 469818c commit 7cf4772

File tree

5 files changed

+81
-103
lines changed

5 files changed

+81
-103
lines changed

src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,18 @@ private function generateDeclaredRoutes()
9393
$properties[] = $compiledRoute->getHostTokens();
9494
$properties[] = $route->getSchemes();
9595

96-
$routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
96+
$routes .= sprintf(" '%s' => %s,\n", $name, $this->exportArray($properties));
9797
}
98-
$routes .= ' )';
98+
$routes .= ' )';
9999

100100
return $routes;
101101
}
102102

103+
protected function exportArray($array)
104+
{
105+
return preg_replace(array('/array \(/', '/,\n *\)/', '/,\n */', '/\n */'), array('array(', ')', ', ', ''), var_export($array, true));
106+
}
107+
103108
/**
104109
* Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
105110
*

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
9898
*/
9999
private function generateMatchMethod($supportsRedirections)
100100
{
101-
$code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
101+
$code = $this->compileRoutes($this->getRoutes(), $supportsRedirections);
102102

103103
return <<<EOF
104104
public function match(\$pathinfo)
@@ -147,13 +147,15 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
147147
// apply extra indention at each line (except empty ones)
148148
$groupCode = preg_replace('/^.{2,}$/m', ' $0', $groupCode);
149149
$code .= $groupCode;
150-
$code .= " }\n\n";
150+
$code .= "\n }";
151151
} else {
152152
$code .= $groupCode;
153153
}
154+
155+
$code .= "\n\n";
154156
}
155157

156-
return $code;
158+
return rtrim($code, "\n");
157159
}
158160

159161
/**
@@ -182,12 +184,15 @@ private function compilePrefixRoutes(DumperPrefixCollection $collection, $suppor
182184
if ($route instanceof DumperCollection) {
183185
$code .= $this->compilePrefixRoutes($route, $supportsRedirections, $optimizedPrefix);
184186
} else {
185-
$code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix)."\n";
187+
$code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix);
186188
}
189+
$code .= "\n\n";
187190
}
188191

192+
$code = rtrim($code, "\n");
193+
189194
if ($optimizable) {
190-
$code .= " }\n\n";
195+
$code .= "\n }";
191196
// apply extra indention at each line (except empty ones)
192197
$code = preg_replace('/^.{2,}$/m', ' $0', $code);
193198
}
@@ -300,7 +305,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
300305
if (!$supportsRedirections) {
301306
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
302307
}
303-
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
308+
$schemes = $this->exportArray(array_flip($schemes));
304309
$code .= <<<EOF
305310
\$requiredSchemes = $schemes;
306311
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
@@ -325,17 +330,17 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
325330
$code .= sprintf(
326331
" return \$this->mergeDefaults(array_replace(%s), %s);\n",
327332
implode(', ', $vars),
328-
str_replace("\n", '', var_export($route->getDefaults(), true))
333+
$this->exportArray($route->getDefaults())
329334
);
330335
} elseif ($route->getDefaults()) {
331-
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
336+
$code .= sprintf(" return %s;\n", $this->exportArray(array_replace($route->getDefaults(), array('_route' => $name))));
332337
} else {
333338
$code .= sprintf(" return array('_route' => '%s');\n", $name);
334339
}
335-
$code .= " }\n";
340+
$code .= ' }';
336341

337342
if ($methods) {
338-
$code .= " $gotoname:\n";
343+
$code .= "\n $gotoname:";
339344
}
340345

341346
return $code;
@@ -371,6 +376,11 @@ private function groupRoutesByHostRegex(RouteCollection $routes)
371376
return $groups;
372377
}
373378

379+
protected function exportArray($array)
380+
{
381+
return preg_replace(array('/array \(/', '/,\n *\)/', '/,\n */', '/\n */'), array('array(', ')', ', ', ''), var_export($array, true));
382+
}
383+
374384
/**
375385
* Organizes the routes into a prefix tree.
376386
*

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function match($pathinfo)
2929

3030
// foo
3131
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
32-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',));
32+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array('def' => 'test'));
3333
}
3434

3535
if (0 === strpos($pathinfo, '/bar')) {
@@ -40,7 +40,7 @@ public function match($pathinfo)
4040
goto not_bar;
4141
}
4242

43-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ());
43+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array());
4444
}
4545
not_bar:
4646

@@ -51,10 +51,9 @@ public function match($pathinfo)
5151
goto not_barhead;
5252
}
5353

54-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
54+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array());
5555
}
5656
not_barhead:
57-
5857
}
5958

6059
if (0 === strpos($pathinfo, '/test')) {
@@ -73,12 +72,11 @@ public function match($pathinfo)
7372
if ($pathinfo === '/test/baz3/') {
7473
return array('_route' => 'baz3');
7574
}
76-
7775
}
7876

7977
// baz4
8078
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
81-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
79+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array());
8280
}
8381

8482
// baz5
@@ -88,7 +86,7 @@ public function match($pathinfo)
8886
goto not_baz5;
8987
}
9088

91-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
89+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array());
9290
}
9391
not_baz5:
9492

@@ -99,20 +97,19 @@ public function match($pathinfo)
9997
goto not_bazbaz6;
10098
}
10199

102-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
100+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array());
103101
}
104102
not_bazbaz6:
105-
106103
}
107104

108105
// foofoo
109106
if ($pathinfo === '/foofoo') {
110-
return array ( 'def' => 'test', '_route' => 'foofoo',);
107+
return array('def' => 'test', '_route' => 'foofoo');
111108
}
112109

113110
// quoter
114111
if (preg_match('#^/(?P<quoter>[\']+)$#s', $pathinfo, $matches)) {
115-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ());
112+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array());
116113
}
117114

118115
// space
@@ -124,40 +121,37 @@ public function match($pathinfo)
124121
if (0 === strpos($pathinfo, '/a/b\'b')) {
125122
// foo1
126123
if (preg_match('#^/a/b\'b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
127-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ());
124+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array());
128125
}
129126

130127
// bar1
131128
if (preg_match('#^/a/b\'b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
132-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ());
129+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array());
133130
}
134-
135131
}
136132

137133
// overridden
138134
if (preg_match('#^/a/(?P<var>.*)$#s', $pathinfo, $matches)) {
139-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ());
135+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array());
140136
}
141137

142138
if (0 === strpos($pathinfo, '/a/b\'b')) {
143139
// foo2
144140
if (preg_match('#^/a/b\'b/(?P<foo1>[^/]++)$#s', $pathinfo, $matches)) {
145-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ());
141+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array());
146142
}
147143

148144
// bar2
149145
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]++)$#s', $pathinfo, $matches)) {
150-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ());
146+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array());
151147
}
152-
153148
}
154-
155149
}
156150

157151
if (0 === strpos($pathinfo, '/multi')) {
158152
// helloWorld
159153
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?P<who>[^/]++))?$#s', $pathinfo, $matches)) {
160-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
154+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array('who' => 'World!'));
161155
}
162156

163157
// overridden2
@@ -169,17 +163,16 @@ public function match($pathinfo)
169163
if ($pathinfo === '/multi/hey/') {
170164
return array('_route' => 'hey');
171165
}
172-
173166
}
174167

175168
// foo3
176169
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
177-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ());
170+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array());
178171
}
179172

180173
// bar3
181174
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
182-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ());
175+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array());
183176
}
184177

185178
if (0 === strpos($pathinfo, '/aba')) {
@@ -190,9 +183,8 @@ public function match($pathinfo)
190183

191184
// foo4
192185
if (preg_match('#^/aba/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
193-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ());
186+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array());
194187
}
195-
196188
}
197189

198190
$host = $this->context->getHost();
@@ -207,31 +199,27 @@ public function match($pathinfo)
207199
if ($pathinfo === '/c2/route2') {
208200
return array('_route' => 'route2');
209201
}
210-
211202
}
212203

213204
if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) {
214205
// route3
215206
if ($pathinfo === '/c2/route3') {
216207
return array('_route' => 'route3');
217208
}
218-
219209
}
220210

221211
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
222212
// route4
223213
if ($pathinfo === '/route4') {
224214
return array('_route' => 'route4');
225215
}
226-
227216
}
228217

229218
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
230219
// route5
231220
if ($pathinfo === '/route5') {
232221
return array('_route' => 'route5');
233222
}
234-
235223
}
236224

237225
// route6
@@ -243,47 +231,43 @@ public function match($pathinfo)
243231
if (0 === strpos($pathinfo, '/route1')) {
244232
// route11
245233
if ($pathinfo === '/route11') {
246-
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ());
234+
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array());
247235
}
248236

249237
// route12
250238
if ($pathinfo === '/route12') {
251-
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
239+
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array('var1' => 'val'));
252240
}
253241

254242
// route13
255243
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
256-
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ());
244+
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array());
257245
}
258246

259247
// route14
260248
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
261-
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
249+
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array('var1' => 'val'));
262250
}
263-
264251
}
265-
266252
}
267253

268254
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
269255
// route15
270256
if (0 === strpos($pathinfo, '/route15') && preg_match('#^/route15/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
271-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ());
257+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array());
272258
}
273-
274259
}
275260

276261
if (0 === strpos($pathinfo, '/route1')) {
277262
// route16
278263
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
279-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
264+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array('var1' => 'val'));
280265
}
281266

282267
// route17
283268
if ($pathinfo === '/route17') {
284269
return array('_route' => 'route17');
285270
}
286-
287271
}
288272

289273
if (0 === strpos($pathinfo, '/a')) {
@@ -295,16 +279,14 @@ public function match($pathinfo)
295279
if (0 === strpos($pathinfo, '/a/b' 741A )) {
296280
// b
297281
if (preg_match('#^/a/b/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
298-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ());
282+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array());
299283
}
300284

301285
// c
302286
if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
303-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ());
287+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array());
304288
}
305-
306289
}
307-
308290
}
309291

310292
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();

0 commit comments

Comments
 (0)
0