Skip to content

Commit 7c0a39c

Browse files
committed
[Routing] optimized the output of the PHP matcher dumper
1 parent e6e8d9b commit 7c0a39c

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ private function addMatcher()
5757
$conditions = array();
5858

5959
$hasTrailingSlash = false;
60+
$matches = false;
6061
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
6162
if (substr($m['url'], -1) === '/') {
6263
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
6364
$hasTrailingSlash = true;
6465
} else {
6566
$conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
6667
}
67-
68-
$matches = 'array()';
6968
} else {
7069
if ($compiledRoute->getStaticPrefix()) {
7170
$conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
@@ -80,13 +79,13 @@ private function addMatcher()
8079
$conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
8180
}
8281

83-
$matches = '$matches';
82+
$matches = true;
8483
}
8584

8685
$conditions = implode(' && ', $conditions);
8786

8887
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
89-
88+
9089
$code[] = <<<EOF
9190
// $name
9291
if ($conditions) {
@@ -111,16 +110,21 @@ private function addMatcher()
111110
, $name);
112111
}
113112

114-
$code[] = sprintf(<<<EOF
115-
return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
116-
}
117-
EOF
118-
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
113+
// optimize parameters array
114+
if (true === $matches && $compiledRoute->getDefaults()) {
115+
$code[] = sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));"
116+
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
117+
} elseif (true === $matches) {
118+
$code[] = sprintf(" \$matches['_route'] = '%s';\n return \$matches;", $name);
119+
} elseif ($compiledRoute->getDefaults()) {
120+
$code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
121+
} else {
122+
$code[] = sprintf(" return array('_route' => '%s');", $name);
123+
}
124+
$code[] = " }";
119125

120126
if ($req) {
121-
$code[] = <<<EOF
122-
$gotoname:
123-
EOF;
127+
$code[] = " $gotoname:";
124128
}
125129

126130
$code[] = '';

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,36 @@ public function match($pathinfo)
3535
$allow = array_merge($allow, array('get', 'head'));
3636
goto not_bar;
3737
}
38-
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar'));
38+
$matches['_route'] = 'bar';
39+
return $matches;
3940
}
4041
not_bar:
4142

4243
// baz
4344
if ($pathinfo === '/test/baz') {
44-
return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz'));
45+
return array('_route' => 'baz');
4546
}
4647

4748
// baz2
4849
if ($pathinfo === '/test/baz.html') {
49-
return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz2'));
50+
return array('_route' => 'baz2');
5051
}
5152

5253
// baz3
5354
if (rtrim($pathinfo, '/') === '/test/baz3') {
5455
if (substr($pathinfo, -1) !== '/') {
5556
return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz3');
5657
}
57-
return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz3'));
58+
return array('_route' => 'baz3');
5859
}
5960

6061
// baz4
6162
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
6263
if (substr($pathinfo, -1) !== '/') {
6364
return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz4');
6465
}
65-
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz4'));
66+
$matches['_route'] = 'baz4';
67+
return $matches;
6668
}
6769

6870
// baz5
@@ -74,7 +76,8 @@ public function match($pathinfo)
7476
if (substr($pathinfo, -1) !== '/') {
7577
return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz5');
7678
}
77-
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz5'));
79+
$matches['_route'] = 'baz5';
80+
return $matches;
7881
}
7982
not_baz5:
8083

@@ -87,10 +90,16 @@ public function match($pathinfo)
8790
if (substr($pathinfo, -1) !== '/') {
8891
return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz.baz6');
8992
}
90-
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz.baz6'));
93+
$matches['_route'] = 'baz.baz6';
94+
return $matches;
9195
}
9296
not_bazbaz6:
9397

98+
// foofoo
99+
if ($pathinfo === '/foofoo') {
100+
return array ( 'def' => 'test', '_route' => 'foofoo',);
101+
}
102+
94103
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new NotFoundException();
95104
}
96105
}

tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public function testDump()
6868
array(),
6969
array('_method' => 'put')
7070
));
71+
// defaults without variable
72+
$collection->add('foofoo', new Route(
73+
'/foofoo',
74+
array('def' => 'test')
75+
));
7176

7277
$dumper = new PhpMatcherDumper($collection);
7378
$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');

0 commit comments

Comments
 (0)