Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6236e84

Browse files
committed
Merge pull request symfony#39 from symfony-cmf/handle-symfony-2.1
make component symfony 2.1 compatible again, provide a flexible url matcher
2 parents 39e2546 + b28c164 commit 6236e84

8 files changed

Lines changed: 205 additions & 16 deletions
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Routing\NestedMatcher;
4+
5+
use Symfony\Component\Routing\Route;
6+
use Symfony\Component\Routing\RequestContext;
7+
use Symfony\Component\Routing\RouteCollection;
8+
use Symfony\Component\HttpFoundation\Request;
9+
10+
use Symfony\Cmf\Component\Routing\NestedMatcher\FinalMatcherInterface;
11+
12+
/**
13+
* A final matcher that can proxy any matcher having the right constructor
14+
* signature, the same way the symfony core Router class does.
15+
*
16+
* @author DavidBuchmann
17+
*/
18+
class ConfigurableUrlMatcher implements FinalMatcherInterface
19+
{
20+
private $matcherClass;
21+
22+
public function __construct($matcherClass = 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher')
23+
{
24+
$this->matcherClass = $matcherClass;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function finalMatch(RouteCollection $collection, Request $request)
31+
{
32+
$context = new RequestContext();
33+
$context->fromRequest($request);
34+
$matcher = $this->getMatcher($collection, $context);
35+
$attributes = $matcher->match($request->getPathInfo());
36+
if (!empty($attributes['_route'])) {
37+
if (empty($attributes['_route_name']) && is_string($attributes['_route'])) {
38+
$attributes['_route_name'] = $attributes['_route'];
39+
}
40+
41+
if (! $attributes['_route'] instanceof Route) {
42+
$attributes['_route'] = $collection->get($attributes['_route']);
43+
}
44+
}
45+
46+
return $attributes;
47+
}
48+
49+
/**
50+
* @param RouteCollection $collection the route collection to match
51+
* @param RequestContext $context the context to match in
52+
*
53+
* @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface
54+
*/
55+
protected function getMatcher(RouteCollection $collection, RequestContext $context)
56+
{
57+
return new $this->matcherClass($collection, $context);
58+
}
59+
}

NestedMatcher/NestedMatcher.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public function setFinalMatcher(FinalMatcherInterface $final)
118118
public function matchRequest(Request $request)
119119
{
120120
$collection = $this->routeProvider->getRouteCollectionForRequest($request);
121-
122121
if (!count($collection)) {
123122
throw new ResourceNotFoundException();
124123
}
@@ -131,18 +130,6 @@ public function matchRequest(Request $request)
131130

132131
$attributes = $this->finalMatcher->finalMatch($collection, $request);
133132

134-
// Add some useful additional attributes if not already present.
135-
// Our UrlMatcher does it, but only since Symfony 2.2
136-
if (!empty($attributes['_route'])) {
137-
if (empty($attributes['_route_name']) && is_string($attributes['_route'])) {
138-
$attributes['_route_name'] = $attributes['_route'];
139-
}
140-
141-
if (! $attributes['_route'] instanceof Route) {
142-
$attributes['_route'] = $this->routeProvider->getRouteByName($attributes['_route']);
143-
}
144-
}
145-
146133
return $attributes;
147134
}
148135

NestedMatcher/UrlMatcher.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
/**
1414
* Extended UrlMatcher to provide an additional interface and enhanced features.
1515
*
16+
* This class requires Symfony 2.2 for a refactoring done to the symfony UrlMatcher
17+
*
1618
* @author Larry Garfield
1719
*/
1820
class UrlMatcher extends SymfonyUrlMatcher implements FinalMatcherInterface

ProviderBasedGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public function generate($name, $parameters = array(), $absolute = false)
4545
// the Route has a cache of its own and is not recompiled as long as it does not get modified
4646
$compiledRoute = $route->compile();
4747

48-
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $absolute, $compiledRoute->getHostnameTokens());
48+
// handle symfony 2.1 and 2.2
49+
// getHostnameTokens exists only since 2.2
50+
$hostnameTokens = null;
51+
if (method_exists($compiledRoute, 'getHostnameTokens')) {
52+
$hostnameTokens = $compiledRoute->getHostnameTokens();
53+
}
54+
55+
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $absolute, $hostnameTokens);
4956
}
5057

5158
/**
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Routing\Tests\NestedMatcher;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
use Symfony\Component\Routing\RouteCollection;
8+
use Symfony\Component\Routing\Route;
9+
use Symfony\Cmf\Component\Routing\NestedMatcher\ConfigurableUrlMatcher;
10+
11+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
12+
13+
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
14+
15+
class ConfigurableUrlMatcherTest extends CmfUnitTestCase
16+
{
17+
protected $routeDocument;
18+
protected $routeCompiled;
19+
protected $matcher;
20+
protected $context;
21+
protected $request;
22+
23+
protected $url = '/foo/bar';
24+
25+
public function setUp()
26+
{
27+
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteKey', 'compile'));
28+
$this->routeCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
29+
30+
$this->context = $this->buildMock('Symfony\\Component\\Routing\\RequestContext');
31+
$this->request = Request::create($this->url);
32+
33+
$this->matcher = new ConfigurableUrlMatcher();
34+
}
35+
36+
public function testMatch()
37+
{
38+
$this->routeCompiled->expects($this->atLeastOnce())
39+
->method('getStaticPrefix')
40+
->will($this->returnValue($this->url))
41+
;
42+
$this->routeCompiled->expects($this->atLeastOnce())
43+
->method('getRegex')
44+
->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#'))
45+
;
46+
$this->routeDocument->expects($this->atLeastOnce())
47+
->method('compile')
48+
->will($this->returnValue($this->routeCompiled))
49+
;
50+
$this->routeDocument->expects($this->atLeastOnce())
51+
->method('getDefaults')
52+
->will($this->returnValue(array('foo' => 'bar')))
53+
;
54+
55+
$mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
56+
$mockCompiled->expects($this->any())
57+
->method('getStaticPrefix')
58+
->will($this->returnValue('/no/match'))
59+
;
60+
$mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
61+
$mockRoute->expects($this->any())
62+
->method('compile')
63+
->will($this->returnValue($mockCompiled))
64+
;
65+
$routeCollection = new RouteCollection();
66+
$routeCollection->add('some', $mockRoute);
67+
$routeCollection->add('_company_more', $this->routeDocument);
68+
$routeCollection->add('other', $mockRoute);
69+
70+
$results = $this->matcher->finalMatch($routeCollection, $this->request);
71+
72+
$expected = array(
73+
'_route_name' => '_company_more',
74+
'_route' => $this->routeDocument,
75+
'foo' => 'bar',
76+
);
77+
78+
$this->assertEquals($expected, $results);
79+
}
80+
81+
public function testMatchNoRouteObject()
82+
{
83+
$this->routeCompiled->expects($this->atLeastOnce())
84+
->method('getStaticPrefix')
85+
->will($this->returnValue($this->url))
86+
;
87+
$this->routeCompiled->expects($this->atLeastOnce())
88+
->method('getRegex')
89+
->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#'))
90+
;
91+
$this->routeDocument = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
92+
$this->routeDocument->expects($this->atLeastOnce())
93+
->method('compile')
94+
->will($this->returnValue($this->routeCompiled))
95+
;
96+
$this->routeDocument->expects($this->never())
97+
->method('getRouteKey')
98+
;
99+
$this->routeDocument->expects($this->atLeastOnce())
100+
->method('getDefaults')
101+
->will($this->returnValue(array('foo' => 'bar')))
102+
;
103+
104+
$mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
105+
$mockCompiled->expects($this->any())
106+
->method('getStaticPrefix')
107+
->will($this->returnValue('/no/match'))
108+
;
109+
$mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
110+
$mockRoute->expects($this->any())
111+
->method('compile')
112+
->will($this->returnValue($mockCompiled))
113+
;
114+
$routeCollection = new RouteCollection();
115+
$routeCollection->add('some', $mockRoute);
116+
$routeCollection->add('_company_more', $this->routeDocument);
117+
$routeCollection->add('other', $mockRoute);
118+
119+
$results = $this->matcher->finalMatch($routeCollection, $this->request);
120+
121+
$expected = array(
122+
'_route_name' => '_company_more',
123+
'_route' => $this->routeDocument,
124+
'foo' => 'bar',
125+
);
126+
127+
$this->assertEquals($expected, $results);
128+
}
129+
}

Tests/NestedMatcher/UrlMatcherTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class UrlMatcherTest extends CmfUnitTestCase
2424

2525
public function setUp()
2626
{
27+
$reflection = new \ReflectionClass('Symfony\\Component\\Routing\\Matcher\\UrlMatcher');
28+
if (! $reflection->hasMethod('getAttributes')) {
29+
$this->markTestSkipped('This only works with symfony 2.2');
30+
}
31+
2732
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteKey', 'compile'));
2833
$this->routeCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
2934

Tests/Routing/ContentAwareGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function testSupports()
278278
*/
279279
class TestableContentAwareGenerator extends ContentAwareGenerator
280280
{
281-
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute, $hostnameTokens)
281+
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute, $hostnameTokens = null)
282282
{
283283
return 'result_url';
284284
}

Tests/Routing/ProviderBasedGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testSupports()
8787
*/
8888
class TestableProviderBasedGenerator extends ProviderBasedGenerator
8989
{
90-
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute, $hostnameTokens)
90+
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute, $hostnameTokens = null)
9191
{
9292
return 'result_url';
9393
}

0 commit comments

Comments
 (0)