Skip to content

Commit

Permalink
Supporting regex on dispatcher "for" condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 20, 2014
1 parent f45c99f commit a3509a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Routing/DispatcherFilter.php
Expand Up @@ -149,7 +149,14 @@ public function matches(Event $event) {
$request = $event->data['request'];
$pass = true;
if (!empty($this->_config['for'])) {
$pass = strpos($request->here(false), $this->_config['for']) === 0;
$len = strlen('preg:');
$for = $this->_config['for'];
$url = $request->here(false);
if (substr($for, 0, $len) === 'preg:') {
$pass = (bool)preg_match(substr($for, $len), $url);
} else {
$pass = strpos($url, $for) === 0;
}
}
if ($pass && !empty($this->_config['when'])) {
$response = $event->data['response'];
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase/Routing/DispatcherFilterTest.php
Expand Up @@ -85,6 +85,15 @@ public function testMatchesWithFor() {
$request = new Request(['url' => '/blog/articles']);
$event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
$this->assertFalse($filter->matches($event), 'Does not start with /articles');

$request = new Request(['url' => '/articles/edit/1']);
$event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
$filter = new DispatcherFilter(['for' => 'preg:#^/articles/edit/\d+$#']);
$this->assertTrue($filter->matches($event));

$request = new Request(['url' => '/blog/articles/edit/1']);
$event = new Event('Dispatcher.beforeDispatch', $this, compact('request'));
$this->assertFalse($filter->matches($event), 'Does not start with /articles');
}

/**
Expand Down

0 comments on commit a3509a7

Please sign in to comment.