Skip to content

Commit 7ed3013

Browse files
Tobionarnaud-lb
authored andcommitted
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.
1 parent 94ec653 commit 7ed3013

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
153153
$name = $this->getDefaultRouteName($class, $method);
154154
}
155155

156-
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
156+
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
157157
foreach ($method->getParameters() as $param) {
158158
if ($param->isOptional()) {
159159
$defaults[$param->getName()] = $param->getDefaultValue();
160160
}
161161
}
162-
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
163-
$options = array_merge($globals['options'], $annot->getOptions());
162+
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
163+
$options = array_replace($globals['options'], $annot->getOptions());
164164

165165
$hostnamePattern = $annot->getHostnamePattern();
166166
if (null === $hostnamePattern) {

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class PhpMatcherDumper extends MatcherDumper
3737
*/
3838
public function dump(array $options = array())
3939
{
40-
$options = array_merge(array(
40+
$options = array_replace(array(
4141
'class' => 'ProjectUrlMatcher',
4242
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
4343
), $options);
@@ -296,35 +296,29 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
296296
// optimize parameters array
297297
if (($matches || $hostnameMatches) && $route->getDefaults()) {
298298
$vars = array();
299-
if ($matches) {
300-
$vars[] = '$matches';
301-
}
302299
if ($hostnameMatches) {
303300
$vars[] = '$hostnameMatches';
304301
}
305-
if (count($vars) > 1) {
306-
$matchesExpr = 'array_merge(' . implode(', ', array_reverse($vars)) . ')';
307-
} else {
308-
$matchesExpr = current($vars);
302+
if ($matches) {
303+
$vars[] = '$matches';
309304
}
305+
$vars[] = "array('_route' => '$name')";
310306

311-
$code .= sprintf(" return array_merge(\$this->mergeDefaults(%s, %s), array('_route' => '%s'));\n"
312-
, $matchesExpr, str_replace("\n", '', var_export($route->getDefaults(), true)), $name);
307+
$code .= sprintf(" return \$this->mergeDefaults(array_replace(%s), %s);\n"
308+
, implode(', ', $vars), str_replace("\n", '', var_export($route->getDefaults(), true)));
313309

314310
} elseif ($matches || $hostnameMatches) {
315311

316312
if (!$matches) {
317313
$code .= " \$matches = \$hostnameMatches;\n";
318-
} else {
319-
if ($hostnameMatches) {
320-
$code .= " \$matches = array_merge(\$hostnameMatches, \$matches);\n";
321-
}
314+
} elseif ($hostnameMatches) {
315+
$code .= " \$matches = array_replace(\$hostnameMatches, \$matches);\n";
322316
}
323317

324318
$code .= sprintf(" \$matches['_route'] = '%s';\n\n", $name);
325319
$code .= " return \$matches;\n";
326320
} elseif ($route->getDefaults()) {
327-
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_merge($route->getDefaults(), array('_route' => $name)), true)));
321+
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
328322
} else {
329323
$code .= sprintf(" return array('_route' => '%s');\n", $name);
330324
}

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
117117
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
118118
continue;
119119
}
120-
$hostnameMatches = array();
121120

121+
$hostnameMatches = array();
122122
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
123123
continue;
124124
}
@@ -147,7 +147,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
147147
continue;
148148
}
149149

150-
return array_merge($this->mergeDefaults(array_merge($matches, $hostnameMatches), $route->getDefaults()), array('_route' => $name));
150+
return $this->mergeDefaults(array_replace($matches, $hostnameMatches, array('_route' => $name)), $route->getDefaults());
151151
}
152152
}
153153

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function match($pathinfo)
2727

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

3333
if (0 === strpos($pathinfo, '/bar')) {
@@ -177,7 +177,7 @@ public function match($pathinfo)
177177
if (0 === strpos($pathinfo, '/multi')) {
178178
// helloWorld
179179
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
180-
return array_merge($this->mergeDefaults($matches, array ( 'who' => 'World!',)), array('_route' => 'helloWorld'));
180+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
181181
}
182182

183183
// overridden2
@@ -277,20 +277,20 @@ public function match($pathinfo)
277277

278278
// route12
279279
if ($pathinfo === '/route12') {
280-
return array_merge($this->mergeDefaults($hostnameMatches, array ( 'var1' => 'val',)), array('_route' => 'route12'));
280+
return $this->mergeDefaults(array_replace($hostnameMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
281281
}
282282

283283
// route13
284284
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
285-
$matches = array_merge($hostnameMatches, $matches);
285+
$matches = array_replace($hostnameMatches, $matches);
286286
$matches['_route'] = 'route13';
287287

288288
return $matches;
289289
}
290290

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

296296
}
@@ -310,7 +310,7 @@ public function match($pathinfo)
310310
if (0 === strpos($pathinfo, '/route1')) {
311311
// route16
312312
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
313-
return array_merge($this->mergeDefaults($matches, array ( 'var1' => 'val',)), array('_route' => 'route16'));
313+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
314314
}
315315

316316
// route17

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function match($pathinfo)
2727

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

3333
if (0 === strpos($pathinfo, '/bar')) {
@@ -185,7 +185,7 @@ public function match($pathinfo)
185185
if (0 === strpos($pathinfo, '/multi')) {
186186
// helloWorld
187187
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?<who>[^/]++))?$#s', $pathinfo, $matches)) {
188-
return array_merge($this->mergeDefaults($matches, array ( 'who' => 'World!',)), array('_route' => 'helloWorld'));
188+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
189189
}
190190

191191
// overridden2
@@ -289,20 +289,20 @@ public function match($pathinfo)
289289

290290
// route12
291291
if ($pathinfo === '/route12') {
292-
return array_merge($this->mergeDefaults($hostnameMatches, array ( 'var1' => 'val',)), array('_route' => 'route12'));
292+
return $this->mergeDefaults(array_replace($hostnameMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
293293
}
294294

295295
// route13
296296
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
297-
$matches = array_merge($hostnameMatches, $matches);
297+
$matches = array_replace($hostnameMatches, $matches);
298298
$matches['_route'] = 'route13';
299299

300300
return $matches;
301301
}
302302

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

308308
}
@@ -322,7 +322,7 @@ public function match($pathinfo)
322322
if (0 === strpos($pathinfo, '/route1')) {
323323
// route16
324324
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?<name>[^/]++)$#s', $pathinfo, $matches)) {
325-
return array_merge($this->mergeDefaults($matches, array ( 'var1' => 'val',)), array('_route' => 'route16'));
325+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
326326
}
327327

328328
// route17

0 commit comments

Comments
 (0)