8000 [Routing] PhpDumper should follow coding standards by c960657 · Pull Request #20082 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] PhpDumper should follow coding standards #20082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,18 @@ private function generateDeclaredRoutes()
$properties[] = $compiledRoute->getHostTokens();
$properties[] = $route->getSchemes();

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

return $routes;
}

protected function exportArray($array)
{
return preg_replace(array('/array \(/', '/,\n *\)/', '/,\n */', '/\n */'), array('array(', ')', ', ', ''), var_export($array, true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a risk of collision with the content of a string here, CS is not worth it imho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I thought I had this covered, but I must have missed something (I wrote this code a while back, so I don't remember my reasoning).

I cannot think of an easy way to fix this, so I think I'll just close this PR.

BTW the existing code simply strips newlines – this is also not completely kosher, but it might be enough for the specific arrays in question.

}

/**
* Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
*
Expand Down
30 changes: 20 additions & 10 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
*/
private function generateMatchMethod($supportsRedirections)
{
$code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
$code = $this->compileRoutes($this->getRoutes(), $supportsRedirections);

return <<<EOF
public function match(\$pathinfo)
Expand Down Expand Up @@ -147,13 +147,15 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
// apply extra indention at each line (except empty ones)
$groupCode = preg_replace('/^.{2,}$/m', ' $0', $groupCode);
$code .= $groupCode;
$code .= " }\n\n";
$code .= "\n }";
} else {
$code .= $groupCode;
}

$code .= "\n\n";
}

return $code;
return rtrim($code, "\n");
}

/**
Expand Down Expand Up @@ -182,12 +184,15 @@ private function compilePrefixRoutes(DumperPrefixCollection $collection, $suppor
if ($route instanceof DumperCollection) {
$code .= $this->compilePrefixRoutes($route, $supportsRedirections, $optimizedPrefix);
} else {
$code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix)."\n";
$code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix);
}
$code .= "\n\n";
}

$code = rtrim($code, "\n");

if ($optimizable) {
$code .= " }\n\n";
$code .= "\n }";
// apply extra indention at each line (except empty ones)
$code = preg_replace('/^.{2,}$/m', ' $0', $code);
}
Expand Down Expand Up @@ -300,7 +305,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
if (!$supportsRedirections) {
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
$schemes = $this->exportArray(array_flip($schemes));
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
Expand All @@ -325,17 +330,17 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
$code .= sprintf(
" return \$this->mergeDefaults(array_replace(%s), %s);\n",
implode(', ', $vars),
str_replace("\n", '', var_export($route->getDefaults(), true))
$this->exportArray($route->getDefaults())
);
} elseif ($route->getDefaults()) {
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
$code .= sprintf(" return %s;\n", $this->exportArray(array_replace($route->getDefaults(), array('_route' => $name))));
} else {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
}
$code .= " }\n";
$code .= ' }';

if ($methods) {
$code .= " $gotoname:\n";
$code .= "\n $gotoname:";
}

return $code;
Expand Down Expand Up @@ -371,6 +376,11 @@ private function groupRoutesByHostRegex(RouteCollection $routes)
return $groups;
}

protected function exportArray($array)
{
return preg_replace(array('/array \(/', '/,\n *\)/', '/,\n */', '/\n */'), array('array(', ')', ', ', ''), var_export($array, true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collision risk again?

}

/**
* Organizes the routes into a prefix tree.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function match($pathinfo)

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

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

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

Expand All @@ -51,10 +51,9 @@ public function match($pathinfo)
goto not_barhead;
}

return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array());
}
not_barhead:

}

if (0 === strpos($pathinfo, '/test')) {
Expand All @@ -73,12 +72,11 @@ public function match($pathinfo)
if ($pathinfo === '/test/baz3/') {
return array('_route' => 'baz3');
}

}

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

// baz5
Expand All @@ -88,7 +86,7 @@ public function match($pathinfo)
goto not_baz5;
}

return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array());
}
not_baz5:

Expand All @@ -99,20 +97,19 @@ public function match($pathinfo)
goto not_bazbaz6;
}

return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array());
}
not_bazbaz6:

}

// foofoo
if ($pathinfo === '/foofoo') {
return array ( 'def' => 'test', '_route' => 'foofoo',);
return array('def' => 'test', '_route' => 'foofoo');
}

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

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

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

}

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

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

// bar2
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array());
}

}

}

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

// overridden2
Expand All @@ -169,17 +163,16 @@ public function match($pathinfo)
if ($pathinfo === '/multi/hey/') {
return array('_route' => 'hey');
}

}

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

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

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

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

}

$host = $this->context->getHost();
Expand All @@ -207,31 +199,27 @@ public function match($pathinfo)
if ($pathinfo === '/c2/route2') {
return array('_route' => 'route2');
}

}

if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) {
// route3
if ($pathinfo === '/c2/route3') {
return array('_route' => 'route3');
}

}

if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
// route4
if ($pathinfo === '/route4') {
return array('_route' => 'route4');
}

}

if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
// route5
if ($pathinfo === '/route5') {
return array('_route' => 'route5');
}

}

// route6
Expand All @@ -243,47 +231,43 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/route1')) {
// route11
if ($pathinfo === '/route11') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ());
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array());
}

// route12
if ($pathinfo === '/route12') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array('var1' => 'val'));
}

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

// route14
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array('var1' => 'val'));
}

}

}

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

}

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

// route17
if ($pathinfo === '/route17') {
return array('_route' => 'route17');
}

}

if (0 === strpos($pathinfo, '/a')) {
Expand All @@ -295,16 +279,14 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/a/b')) {
// b
if (preg_match('#^/a/b/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array());
}

// c
if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ());
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array());
}

}

}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
Expand Down
Loading
0