Permalink
Browse files

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.
  • Loading branch information...
1 parent 94ec653 commit 7ed3013a5b9323f7d5d526aa5fe8bdaaeaad2950 @Tobion Tobion committed with arnaud-lb Nov 11, 2012
@@ -153,14 +153,14 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
$name = $this->getDefaultRouteName($class, $method);
}
- $defaults = array_merge($globals['defaults'], $annot->getDefaults());
+ $defaults = array_replace($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if ($param->isOptional()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
- $requirements = array_merge($globals['requirements'], $annot->getRequirements());
- $options = array_merge($globals['options'], $annot->getOptions());
+ $requirements = array_replace($globals['requirements'], $annot->getRequirements());
+ $options = array_replace($globals['options'], $annot->getOptions());
$hostnamePattern = $annot->getHostnamePattern();
if (null === $hostnamePattern) {
@@ -37,7 +37,7 @@ class PhpMatcherDumper extends MatcherDumper
*/
public function dump(array $options = array())
{
- $options = array_merge(array(
+ $options = array_replace(array(
'class' => 'ProjectUrlMatcher',
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
), $options);
@@ -296,35 +296,29 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
// optimize parameters array
if (($matches || $hostnameMatches) && $route->getDefaults()) {
$vars = array();
- if ($matches) {
- $vars[] = '$matches';
- }
if ($hostnameMatches) {
$vars[] = '$hostnameMatches';
}
- if (count($vars) > 1) {
- $matchesExpr = 'array_merge(' . implode(', ', array_reverse($vars)) . ')';
- } else {
- $matchesExpr = current($vars);
+ if ($matches) {
+ $vars[] = '$matches';
}
+ $vars[] = "array('_route' => '$name')";
- $code .= sprintf(" return array_merge(\$this->mergeDefaults(%s, %s), array('_route' => '%s'));\n"
- , $matchesExpr, str_replace("\n", '', var_export($route->getDefaults(), true)), $name);
+ $code .= sprintf(" return \$this->mergeDefaults(array_replace(%s), %s);\n"
+ , implode(', ', $vars), str_replace("\n", '', var_export($route->getDefaults(), true)));
} elseif ($matches || $hostnameMatches) {
if (!$matches) {
$code .= " \$matches = \$hostnameMatches;\n";
- } else {
- if ($hostnameMatches) {
- $code .= " \$matches = array_merge(\$hostnameMatches, \$matches);\n";
- }
+ } elseif ($hostnameMatches) {
+ $code .= " \$matches = array_replace(\$hostnameMatches, \$matches);\n";
}
$code .= sprintf(" \$matches['_route'] = '%s';\n\n", $name);
$code .= " return \$matches;\n";
} 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 {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
}
@@ -117,8 +117,8 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
continue;
}
- $hostnameMatches = array();
+ $hostnameMatches = array();
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
continue;
}
@@ -147,7 +147,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
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 @@ public function match($pathinfo)
// foo
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')) {
@@ -177,7 +177,7 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/multi')) {
// helloWorld
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
@@ -277,20 +277,20 @@ public function match($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
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';
return $matches;
}
// route14
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 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/route1')) {
// route16
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
@@ -27,7 +27,7 @@ public function match($pathinfo)
// foo
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')) {
@@ -185,7 +185,7 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/multi')) {
// helloWorld
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
@@ -289,20 +289,20 @@ public function match($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
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';
return $matches;
}
// route14
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 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/route1')) {
// route16
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

0 comments on commit 7ed3013

Please sign in to comment.