Skip to content

Commit b4bc402

Browse files
committed
make event handler optional, add a test
1 parent 0bd38e4 commit b4bc402

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
1.1
55
---
66

7+
* **2013-07-31**: DynamicRouter now accepts an EventDispatcher to trigger a RouteMatchEvent right before the matching starts
78
* **2013-07-29**: Renamed RouteAwareInterface to RouteReferrersInterface for naming consistency
89
* **2013-07-13**: NestedMatcher now expects a FinalMatcherInterface as second argument of the constructor
910
* **2013-04-30**: Dropped Symfony 2.1 support and got rid of ConfigurableUrlMatcher class

DynamicRouter.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
7171
* @param RequestMatcherInterface|UrlMatcherInterface $matcher
7272
* @param UrlGeneratorInterface $generator
7373
* @param string $uriFilterRegexp
74+
* @param EventDispatcherInterface|null $eventDispatcher
7475
*/
75-
public function __construct(RequestContext $context, $matcher, UrlGeneratorInterface $generator, EventDispatcherInterface $eventDispatcher, $uriFilterRegexp = '')
76-
{
76+
public function __construct(RequestContext $context,
77+
$matcher,
78+
UrlGeneratorInterface $generator,
79+
$uriFilterRegexp = '',
80+
EventDispatcherInterface $eventDispatcher = null
81+
) {
7782
$this->context = $context;
7883
if (! $matcher instanceof RequestMatcherInterface && ! $matcher instanceof UrlMatcherInterface) {
7984
throw new \InvalidArgumentException('Invalid $matcher');
@@ -175,8 +180,11 @@ public function supports($name)
175180
*/
176181
public function match($pathinfo)
177182
{
178-
$event = new RouterMatchEvent();
179-
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH, $event);
183+
$request = Request::create($pathinfo);
184+
if ($this->eventDispatcher) {
185+
$event = new RouterMatchEvent();
186+
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH, $event);
187+
}
180188

181189
if (! empty($this->uriFilterRegexp) && ! preg_match($this->uriFilterRegexp, $pathinfo)) {
182190
throw new ResourceNotFoundException("$pathinfo does not match the '{$this->uriFilterRegexp}' pattern");
@@ -189,7 +197,7 @@ public function match($pathinfo)
189197

190198
$defaults = $matcher->match($pathinfo);
191199

192-
return $this->applyRouteEnhancers($defaults, Request::create($pathinfo));
200+
return $this->applyRouteEnhancers($defaults, $request);
193201
}
194202

195203
/**
@@ -209,8 +217,10 @@ public function match($pathinfo)
209217
*/
210218
public function matchRequest(Request $request)
211219
{
212-
$event = new RouterMatchEvent($request);
213-
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH_REQUEST, $event);
220+
if ($this->eventDispatcher) {
221+
$event = new RouterMatchEvent($request);
222+
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH_REQUEST, $event);
223+
}
214224

215225
if (! empty($this->uriFilterRegexp)
216226
&& ! preg_match($this->uriFilterRegexp, $request->getPathInfo())
@@ -220,12 +230,11 @@ public function matchRequest(Request $request)
220230

221231
$matcher = $this->getMatcher();
222232
if ($matcher instanceof UrlMatcherInterface) {
223-
// the match method will enhance the route $defaults
224-
return $this->match($request->getPathInfo());
233+
$defaults = $matcher->match($request->getPathInfo());
234+
} else {
235+
$defaults = $matcher->matchRequest($request);
225236
}
226237

227-
$defaults = $matcher->matchRequest($request);
228-
229238
return $this->applyRouteEnhancers($defaults, $request);
230239
}
231240

Event/Events.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ final class Events
66
{
77
/**
88
* Fired before a path is matched in \Symfony\Cmf\Component\Routing\DynamicRouter#match
9+
*
10+
* The event object is RouteMatchEvent.
911
*/
1012
const PRE_DYNAMIC_MATCH = 'cmf_routing.pre_dynamic_match';
1113

1214
/**
1315
* Fired before a Request is matched in \Symfony\Cmf\Component\Routing\DynamicRouter#match
16+
*
17+
* The event object is RouteMatchEvent.
1418
*/
1519
const PRE_DYNAMIC_MATCH_REQUEST = 'cmf_routing.pre_dynamic_match_request';
1620
}

Event/RouterMatchEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class RouterMatchEvent extends Event
99
{
1010
/**
11-
* @var \Symfony\Component\HttpFoundation\Request
11+
* @var Request
1212
*/
1313
protected $request;
1414

Tests/Routing/DynamicRouterTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
44

5+
use Symfony\Cmf\Component\Routing\Event\Events;
6+
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
57
use Symfony\Component\HttpFoundation\Request;
68
use Symfony\Component\Routing\RouteCollection;
79

@@ -177,7 +179,7 @@ public function testMatchRequest()
177179
$expected = array('this' => 'that');
178180
$this->enhancer->expects($this->once())
179181
->method('enhance')
180-
->with($this->equalTo($routeDefaults), $this->equalTo($this->request)) // TODO: why do we not get the right thing?
182+
->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
181183
->will($this->returnValue($expected))
182184
;
183185

@@ -262,4 +264,24 @@ public function testRouteDebugMessageNonversatile()
262264
$router = new DynamicRouter($this->context, $this->matcher, $generator);
263265
$this->assertInternalType('string', $router->getRouteDebugMessage('test'));
264266
}
267+
268+
public function testEventHandler()
269+
{
270+
$eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
271+
$router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
272+
273+
$eventDispatcher->expects($this->once())
274+
->method('dispatch')
275+
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
276+
;
277+
278+
$routeDefaults = array('foo' => 'bar');
279+
$this->matcher->expects($this->once())
280+
->method('match')
281+
->with($this->url)
282+
->will($this->returnValue($routeDefaults))
283+
;
284+
285+
$this->assertEquals($routeDefaults, $router->match($this->url));
286+
}
265287
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
},
2121
"require-dev": {
2222
"symfony/dependency-injection": "~2.0",
23-
"symfony/config": "~2.2"
23+
"symfony/config": "~2.2",
24+
"symfony/event-dispatcher": "~2.1"
2425
},
2526
"suggest": {
26-
"symfony/http-foundation": "ChainRouter/DynamicRouter have optional support for Request instances, several enhancers require a Request instances, ~2.2"
27+
"symfony/http-foundation": "ChainRouter/DynamicRouter have optional support for Request instances, several enhancers require a Request instances, ~2.2",
28+
"symfony/event-dispatcher": "DynamicRouter can optionally trigger an event at the start of matching, ~2.1"
2729
},
2830
"autoload": {
2931
"psr-0": { "Symfony\\Cmf\\Component\\Routing": "" }

0 commit comments

Comments
 (0)