Skip to content

Commit 7cc5623

Browse files
committed
Merge pull request symfony#43 from symfony-cmf/route-name
route name is in _route now, object in _route_object
2 parents 04da0f0 + eb32d17 commit 7cc5623

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

NestedMatcher/ConfigurableUrlMatcher.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ public function finalMatch(RouteCollection $collection, Request $request)
3636
$attributes = $matcher->match($request->getPathInfo());
3737

3838
// cleanup route attributes
39-
if (! isset($attributes['_route']) || ! $attributes['_route'] instanceof Route) {
40-
$name = $attributes['_route'];
41-
$route = $collection->get($attributes['_route']);
42-
$attributes['_route'] = $route;
39+
if (! isset($attributes[RouteObjectInterface::ROUTE_OBJECT])
40+
|| ! $attributes[RouteObjectInterface::ROUTE_OBJECT] instanceof Route
41+
) {
42+
$name = $attributes['_route']; // this is the field coming from symfony core
43+
$route = $collection->get($name);
44+
$attributes[RouteObjectInterface::ROUTE_OBJECT] = $route;
4345

4446
if ($route instanceof RouteObjectInterface && is_string($route->getRouteKey())) {
4547
$name = $route->getRouteKey();
4648
}
4749

48-
if (empty($attributes['_route_name']) && is_string($name)) {
49-
$attributes['_route_name'] = $name;
50+
if (is_string($name)) {
51+
$attributes[RouteObjectInterface::ROUTE_NAME] = $name;
5052
}
5153
}
5254

NestedMatcher/UrlMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ protected function getAttributes(Route $route, $name, array $attributes)
3939
if ($route instanceof RouteObjectInterface && is_string($route->getRouteKey())) {
4040
$name = $route->getRouteKey();
4141
}
42-
$attributes['_route_name'] = $name;
43-
$attributes['_route'] = $route;
42+
$attributes[RouteObjectInterface::ROUTE_NAME] = $name;
43+
$attributes[RouteObjectInterface::ROUTE_OBJECT] = $route;
4444
return $this->mergeDefaults($attributes, $route->getDefaults());
4545
}
4646
}

RouteObjectInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
*/
1717
interface RouteObjectInterface
1818
{
19+
/**
20+
* Field name that will hold the route name that was matched.
21+
*/
22+
const ROUTE_NAME = '_route';
23+
24+
/**
25+
* Field name of the route object that was matched.
26+
*/
27+
const ROUTE_OBJECT = '_route_object';
28+
1929
/**
2030
* Constant for the field that is given to the ControllerAliasMapper.
2131
* The value must be configured in the controllers_by_alias mapping.

Tests/Enhancer/RouteContentEnhancerTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\HttpFoundation\Request;
66

77
use Symfony\Cmf\Component\Routing\Enhancer\RouteContentEnhancer;
8+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
89

910
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
1011

@@ -22,7 +23,7 @@ public function setUp()
2223
$this->document = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Enhancer\\RouteObject',
2324
array('getRouteContent', 'getRouteDefaults', 'getUrl'));
2425

25-
$this->mapper = new RouteContentEnhancer('_route', '_content');
26+
$this->mapper = new RouteContentEnhancer(RouteObjectInterface::ROUTE_OBJECT, '_content');
2627

2728
$this->request = Request::create('/test');
2829
}
@@ -34,8 +35,8 @@ public function testContent()
3435
->method('getRouteContent')
3536
->will($this->returnValue($targetDocument));
3637

37-
$defaults = array('_route' => $this->document);
38-
$expected = array('_route' => $this->document, '_content' => $targetDocument);
38+
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->document);
39+
$expected = array(RouteObjectInterface::ROUTE_OBJECT => $this->document, '_content' => $targetDocument);
3940

4041
$this->assertEquals($expected, $this->mapper->enhance($defaults, $this->request));
4142
}
@@ -47,7 +48,7 @@ public function testFieldAlreadyThere()
4748
->method('getRouteContent')
4849
;
4950

50-
$defaults = array('_route' => $this->document, '_content' => 'foo');
51+
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->document, '_content' => 'foo');
5152

5253
$this->assertEquals($defaults, $this->mapper->enhance($defaults, $this->request));
5354
}
@@ -58,13 +59,13 @@ public function testNoContent()
5859
->method('getRouteContent')
5960
->will($this->returnValue(null));
6061

61-
$defaults = array('_route' => $this->document);
62+
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->document);
6263
$this->assertEquals($defaults, $this->mapper->enhance($defaults, $this->request));
6364
}
6465

6566
public function testNoCmfRoute()
6667
{
67-
$defaults = array('_route' => $this->buildMock('Symfony\\Component\\Routing\\Route'));
68+
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->buildMock('Symfony\\Component\\Routing\\Route'));
6869
$this->assertEquals($defaults, $this->mapper->enhance($defaults, $this->request));
6970
}
7071
}

Tests/NestedMatcher/ConfigurableUrlMatcherTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public function testMatch()
5151
->method('getDefaults')
5252
->will($this->returnValue(array('foo' => 'bar')))
5353
;
54+
$this->routeDocument->expects($this->any())
55+
->method('getRouteKey')
56+
->will($this->returnValue('/routes/company/more'))
57+
;
5458

59+
// add some other routes to the collection
5560
$mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
5661
$mockCompiled->expects($this->any())
5762
->method('getStaticPrefix')
@@ -62,16 +67,18 @@ public function testMatch()
6267
->method('compile')
6368
->will($this->returnValue($mockCompiled))
6469
;
70+
6571
$routeCollection = new RouteCollection();
6672
$routeCollection->add('some', $mockRoute);
6773
$routeCollection->add('_company_more', $this->routeDocument);
6874
$routeCollection->add('other', $mockRoute);
6975

7076
$results = $this->matcher->finalMatch($routeCollection, $this->request);
7177

78+
// the matched route returns a key
7279
$expected = array(
73-
'_route_name' => '_company_more',
74-
'_route' => $this->routeDocument,
80+
RouteObjectInterface::ROUTE_NAME => '/routes/company/more',
81+
RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
7582
'foo' => 'bar',
7683
);
7784

@@ -118,9 +125,10 @@ public function testMatchNoRouteObject()
118125

119126
$results = $this->matcher->finalMatch($routeCollection, $this->request);
120127

128+
// the matched route does not return a key
121129
$expected = array(
122-
'_route_name' => '_company_more',
123-
'_route' => $this->routeDocument,
130+
RouteObjectInterface::ROUTE_NAME => '_company_more',
131+
RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
124132
'foo' => 'bar',
125133
);
126134

Tests/NestedMatcher/UrlMatcherTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public function doTestMatchRouteKey($routeKey)
8989
$results = $this->matcher->finalMatch($routeCollection, $this->request);
9090

9191
$expected = array(
92-
'_route_name' => ($routeKey) ? $routeKey : '_company_more',
93-
'_route' => $this->routeDocument,
92+
RouteObjectInterface::ROUTE_NAME => ($routeKey) ? $routeKey : '_company_more',
93+
RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
9494
'foo' => 'bar',
9595
);
9696

@@ -138,8 +138,8 @@ public function testMatchNoRouteObject()
138138
$results = $this->matcher->finalMatch($routeCollection, $this->request);
139139

140140
$expected = array(
141-
'_route_name' => '_company_more',
142-
'_route' => $this->routeDocument,
141+
RouteObjectInterface::ROUTE_NAME => '_company_more',
142+
RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
143143
'foo' => 'bar',
144144
);
145145

0 commit comments

Comments
 (0)