8000 [Routing] fixed ApacheUrlMatcher and ApachMatcherDumper classes that … · sigues/symfony@e9d799c · GitHub
[go: up one dir, main page]

Skip to content

Commit e9d799c

Browse files
author
Hugo Hamon
committed
[Routing] fixed ApacheUrlMatcher and ApachMatcherDumper classes that did not take care of default parameters in urls.
1 parent 3dc1fe0 commit e9d799c

File tree

5 files changed

+123
-13
lines changed

5 files changed

+123
-13
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ApacheUrlMatcher extends UrlMatcher
3434
public function match($pathinfo)
3535
{
3636
$parameters = array();
37+
$defaults = array();
3738
$allow = array();
3839
$match = false;
3940

@@ -44,25 +45,28 @@ public function match($pathinfo)
4445
$name = substr($name, 9);
4546
}
4647

47-
if (0 === strpos($name, '_ROUTING_')) {
48+
if (0 === strpos($name, '_ROUTING_DEFAULTS_')) {
49+
$name = substr($name, 18);
50+
$defaults[$name] = $value;
51+
} elseif (0 === strpos($name, '_ROUTING_')) {
4852
$name = substr($name, 9);
53+
if ('_route' == $name) {
54+
$match = true;
55+
$parameters[$name] = $value;
56+
} elseif (0 === strpos($name, '_allow_')) {
57+
$allow[] = substr($name, 7);
58+
} else {
59+
$parameters[$name] = $value;
60+
}
4961
} else {
5062
continue;
5163
}
5264

53-
if ('_route' == $name) {
54-
$match = true;
55-
} elseif (0 === strpos($name, '_allow_')) {
56-
$allow[] = substr($name, 7);
57-
} else {
58-
$parameters[$name] = $value;
59-
}
60-
6165
unset($_SERVER[$key]);
6266
}
6367

6468
if ($match) {
65-
return $parameters;
69+
return $this->mergeDefaults($parameters, $defaults);
6670
} elseif (0 < count($allow)) {
6771
throw new MethodNotAllowedException($allow);
6872
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function dump(array $options = array())
6565
$variables[] = 'E=_ROUTING_'.$variable.':%'.($i + 1);
6666
}
6767
foreach ($route->getDefaults() as $key => $value) {
68-
$variables[] = 'E=_ROUTING_'.$key.':'.strtr($value, array(
68+
$variables[] = 'E=_ROUTING_DEFAULTS_'.$key.':'.strtr($value, array(
6969
':' => '\\:',
7070
'=' => '\\=',
7171
'\\' => '\\\\',

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ RewriteRule .* - [QSA,L]
44

55
# foo
66
RewriteCond %{REQUEST_URI} ^/foo/(baz|symfony)$
7-
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_def:test]
7+
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_DEFAULTS_def:test]
8+
9+
# foobar
10+
RewriteCond %{REQUEST_URI} ^/foo(?:/([^/]+?))?$
11+
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foobar,E=_ROUTING_bar:%1,E=_ROUTING_DEFAULTS_bar:toto]
812

913
# bar
1014
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
@@ -51,7 +55,7 @@ RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz5,E=_ROUTING_foo:%1]
5155

5256
# baz6
5357
RewriteCond %{REQUEST_URI} ^/test/baz$
54-
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz6,E=_ROUTING_foo:bar\ baz]
58+
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz6,E=_ROUTING_DEFAULTS_foo:bar\ baz]
5559

5660
# baz7
5761
RewriteCond %{REQUEST_URI} ^/te\ st/baz$
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Routing\Matcher;
13+
14+
use Symfony\Component\Routing\RouteCollection;
15+
use Symfony\Component\Routing\RequestContext;
16+
use Symfony\Component\Routing\Matcher\ApacheUrlMatcher;
17+
18+
class ApacheUrlMatcherTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @dataProvider getMatchData
22+
*/
23+
public function testMatch($name, $pathinfo, $server, $expect)
24+
{
25+
$collection = new RouteCollection();
26+
$context = new RequestContext();
27+
$matcher = new ApacheUrlMatcher($collection, $context);
28+
29+
$_SERVER = $server;
30+
31+
$result = $matcher->match($pathinfo, $server);
32+
$this->assertSame(var_export($expect, true), var_export($result, true));
33+
}
34+
35+
public function getMatchData()
36+
{
37+
return array(
38+
array(
39+
'Simple route',
40+
'/hello/world',
41+
array(
42+
'_ROUTING__route' => 'hello',
43+
'_ROUTING__controller' => 'AcmeBundle:Default:index',
44+
'_ROUTING_name' => 'world',
45+
),
46+
array(
47+
'_route' => 'hello',
48+
'_controller' => 'AcmeBundle:Default:index',
49+
'name' => 'world',
50+
),
51+
),
52+
array(
53+
'Route with params and defaults',
54+
'/hello/hugo',
55+
array(
56+
'_ROUTING__route' => 'hello',
57+
'_ROUTING__controller' => 'AcmeBundle:Default:index',
58+
'_ROUTING_name' => 'hugo',
59+
'_ROUTING_DEFAULTS_name' => 'world',
60+
),
61+
array(
62+
'name' => 'hugo',
63+
'_route' => 'hello',
64+
'_controller' => 'AcmeBundle:Default:index',
65+
),
66+
),
67+
array(
68+
'Route with defaults only',
69+
'/hello',
70+
array(
71+
'_ROUTING__route' => 'hello',
72+
'_ROUTING__controller' => 'AcmeBundle:Default:index',
73+
'_ROUTING_DEFAULTS_name' => 'world',
74+
),
75+
array(
76+
'name' => 'world',
77+
'_route' => 'hello',
78+
'_controller' => 'AcmeBundle:Default:index',
79+
),
80+
),
81+
array(
82+
'REDIRECT_ envs',
83+
'/hello/world',
84+
array(
85+
'REDIRECT__ROUTING__route' => 'hello',
86+
'REDIRECT__ROUTING__controller' => 'AcmeBundle:Default:index',
87+
'REDIRECT__ROUTING_name' => 'world',
88+
),
89+
array(
90+
'_route' => 'hello',
91+
'_controller' => 'AcmeBundle:Default:index',
92+
'name' => 'world',
93+
),
94+
),
95+
);
96+
}
97+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ private function getRouteCollection()
7070
array('def' => 'test'),
7171
array('bar' => 'baz|symfony')
7272
));
73+
// defaults parameters in pattern
74+
$collection->add('foobar', new Route(
75+
'/foo/{bar}',
76+
array('bar' => 'toto')
77+
));
7378
// method requirement
7479
$collection->add('bar', new Route(
7580
'/bar/{foo}',

0 commit comments

Comments
 (0)
0