Skip to content
Permalink
Browse files

Supporting regex on dispatcher "for" condition

  • Loading branch information...
jrbasso committed May 20, 2014
1 parent f45c99f commit a3509a7202b0c239522bcfa4b4304528b945cdcf
Showing with 17 additions and 1 deletion.
  1. +8 −1 src/Routing/DispatcherFilter.php
  2. +9 −0 tests/TestCase/Routing/DispatcherFilterTest.php
@@ -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'];
@@ -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');
}
/**

0 comments on commit a3509a7

Please sign in to comment.
You can’t perform that action at this time.