switch to array_replace instead of array_merge
we don't need the logic to merge numeric keys, as we don't have them. I could also improve the genrated code by PhpMatcherDumper a little by saving a function call.
This commit is contained in:
parent
94ec653818
commit
7ed3013a5b
@ -153,14 +153,14 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
|||||||
$name = $this->getDefaultRouteName($class, $method);
|
$name = $this->getDefaultRouteName($class, $method);
|
||||||
}
|
}
|
||||||
|
|
||||||
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
|
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
|
||||||
foreach ($method->getParameters() as $param) {
|
foreach ($method->getParameters() as $param) {
|
||||||
if ($param->isOptional()) {
|
if ($param->isOptional()) {
|
||||||
$defaults[$param->getName()] = $param->getDefaultValue();
|
$defaults[$param->getName()] = $param->getDefaultValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
|
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
|
||||||
$options = array_merge($globals['options'], $annot->getOptions());
|
$options = array_replace($globals['options'], $annot->getOptions());
|
||||||
|
|
||||||
$hostnamePattern = $annot->getHostnamePattern();
|
$hostnamePattern = $annot->getHostnamePattern();
|
||||||
if (null === $hostnamePattern) {
|
if (null === $hostnamePattern) {
|
||||||
|
@ -37,7 +37,7 @@ class PhpMatcherDumper extends MatcherDumper
|
|||||||
*/
|
*/
|
||||||
public function dump(array $options = array())
|
public function dump(array $options = array())
|
||||||
{
|
{
|
||||||
$options = array_merge(array(
|
$options = array_replace(array(
|
||||||
'class' => 'ProjectUrlMatcher',
|
'class' => 'ProjectUrlMatcher',
|
||||||
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
||||||
), $options);
|
), $options);
|
||||||
@ -296,35 +296,29 @@ EOF;
|
|||||||
// optimize parameters array
|
// optimize parameters array
|
||||||
if (($matches || $hostnameMatches) && $route->getDefaults()) {
|
if (($matches || $hostnameMatches) && $route->getDefaults()) {
|
||||||
$vars = array();
|
$vars = array();
|
||||||
if ($matches) {
|
|
||||||
$vars[] = '$matches';
|
|
||||||
}
|
|
||||||
if ($hostnameMatches) {
|
if ($hostnameMatches) {
|
||||||
$vars[] = '$hostnameMatches';
|
$vars[] = '$hostnameMatches';
|
||||||
}
|
}
|
||||||
if (count($vars) > 1) {
|
if ($matches) {
|
||||||
$matchesExpr = 'array_merge(' . implode(', ', array_reverse($vars)) . ')';
|
$vars[] = '$matches';
|
||||||
} else {
|
|
||||||
$matchesExpr = current($vars);
|
|
||||||
}
|
}
|
||||||
|
$vars[] = "array('_route' => '$name')";
|
||||||
|
|
||||||
$code .= sprintf(" return array_merge(\$this->mergeDefaults(%s, %s), array('_route' => '%s'));\n"
|
$code .= sprintf(" return \$this->mergeDefaults(array_replace(%s), %s);\n"
|
||||||
, $matchesExpr, str_replace("\n", '', var_export($route->getDefaults(), true)), $name);
|
, implode(', ', $vars), str_replace("\n", '', var_export($route->getDefaults(), true)));
|
||||||
|
|
||||||
} elseif ($matches || $hostnameMatches) {
|
} elseif ($matches || $hostnameMatches) {
|
||||||
|
|
||||||
if (!$matches) {
|
if (!$matches) {
|
||||||
$code .= " \$matches = \$hostnameMatches;\n";
|
$code .= " \$matches = \$hostnameMatches;\n";
|
||||||
} else {
|
} elseif ($hostnameMatches) {
|
||||||
if ($hostnameMatches) {
|
$code .= " \$matches = array_replace(\$hostnameMatches, \$matches);\n";
|
||||||
$code .= " \$matches = array_merge(\$hostnameMatches, \$matches);\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$code .= sprintf(" \$matches['_route'] = '%s';\n\n", $name);
|
$code .= sprintf(" \$matches['_route'] = '%s';\n\n", $name);
|
||||||
$code .= " return \$matches;\n";
|
$code .= " return \$matches;\n";
|
||||||
} elseif ($route->getDefaults()) {
|
} elseif ($route->getDefaults()) {
|
||||||
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_merge($route->getDefaults(), array('_route' => $name)), true)));
|
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
|
||||||
} else {
|
} else {
|
||||||
$code .= sprintf(" return array('_route' => '%s');\n", $name);
|
$code .= sprintf(" return array('_route' => '%s');\n", $name);
|
||||||
}
|
}
|
||||||
|
@ -117,8 +117,8 @@ class UrlMatcher implements UrlMatcherInterface
|
|||||||
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
|
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$hostnameMatches = array();
|
|
||||||
|
|
||||||
|
$hostnameMatches = array();
|
||||||
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
|
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -147,7 +147,7 @@ class UrlMatcher implements UrlMatcherInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_merge($this->mergeDefaults(array_merge($matches, $hostnameMatches), $route->getDefaults()), array('_route' => $name));
|
return $this->mergeDefaults(array_replace($matches, $hostnameMatches, array('_route' => $name)), $route->getDefaults());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// foo
|
// foo
|
||||||
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?<bar>baz|symfony)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?<bar>baz|symfony)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'def' => 'test',)), array('_route' => 'foo'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 === strpos($pathinfo, '/bar')) {
|
if (0 === strpos($pathinfo, '/bar')) {
|
||||||
@ -177,7 +177,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
if (0 === strpos($pathinfo, '/multi')) {
|
if (0 === strpos($pathinfo, '/multi')) {
|
||||||
// helloWorld
|
// helloWorld
|
||||||
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'who' => 'World!',)), array('_route' => 'helloWorld'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// overridden2
|
// overridden2
|
||||||
@ -277,12 +277,12 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// route12
|
// route12
|
||||||
if ($pathinfo === '/route12') {
|
if ($pathinfo === '/route12') {
|
||||||
return array_merge($this->mergeDefaults($hostnameMatches, array ( 'var1' => 'val',)), array('_route' => 'route12'));
|
return $this->mergeDefaults(array_replace($hostnameMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// route13
|
// route13
|
||||||
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
$matches = array_merge($hostnameMatches, $matches);
|
$matches = array_replace($hostnameMatches, $matches);
|
||||||
$matches['_route'] = 'route13';
|
$matches['_route'] = 'route13';
|
||||||
|
|
||||||
return $matches;
|
return $matches;
|
||||||
@ -290,7 +290,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// route14
|
// route14
|
||||||
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults(array_merge($hostnameMatches, $matches), array ( 'var1' => 'val',)), array('_route' => 'route14'));
|
return $this->mergeDefaults(array_replace($hostnameMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -310,7 +310,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
if (0 === strpos($pathinfo, '/route1')) {
|
if (0 === strpos($pathinfo, '/route1')) {
|
||||||
// route16
|
// route16
|
||||||
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'var1' => 'val',)), array('_route' => 'route16'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// route17
|
// route17
|
||||||
|
@ -27,7 +27,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
|||||||
|
|
||||||
// foo
|
// foo
|
||||||
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?<bar>baz|symfony)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?<bar>baz|symfony)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'def' => 'test',)), array('_route' => 'foo'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 === strpos($pathinfo, '/bar')) {
|
if (0 === strpos($pathinfo, '/bar')) {
|
||||||
@ -185,7 +185,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
|||||||
if (0 === strpos($pathinfo, '/multi')) {
|
if (0 === strpos($pathinfo, '/multi')) {
|
||||||
// helloWorld
|
// helloWorld
|
||||||
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'who' => 'World!',)), array('_route' => 'helloWorld'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// overridden2
|
// overridden2
|
||||||
@ -289,12 +289,12 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
|||||||
|
|
||||||
// route12
|
// route12
|
||||||
if ($pathinfo === '/route12') {
|
if ($pathinfo === '/route12') {
|
||||||
return array_merge($this->mergeDefaults($hostnameMatches, array ( 'var1' => 'val',)), array('_route' => 'route12'));
|
return $this->mergeDefaults(array_replace($hostnameMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// route13
|
// route13
|
||||||
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
$matches = array_merge($hostnameMatches, $matches);
|
$matches = array_replace($hostnameMatches, $matches);
|
||||||
$matches['_route'] = 'route13';
|
$matches['_route'] = 'route13';
|
||||||
|
|
||||||
return $matches;
|
return $matches;
|
||||||
@ -302,7 +302,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
|||||||
|
|
||||||
// route14
|
// route14
|
||||||
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults(array_merge($hostnameMatches, $matches), array ( 'var1' => 'val',)), array('_route' => 'route14'));
|
return $this->mergeDefaults(array_replace($hostnameMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -322,7 +322,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
|
|||||||
if (0 === strpos($pathinfo, '/route1')) {
|
if (0 === strpos($pathinfo, '/route1')) {
|
||||||
// route16
|
// route16
|
||||||
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
|
||||||
return array_merge($this->mergeDefaults($matches, array ( 'var1' => 'val',)), array('_route' => 'route16'));
|
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
|
||||||
}
|
}
|
||||||
|
|
||||||
// route17
|
// route17
|
||||||
|
Reference in New Issue
Block a user