Skip to content

Commit

Permalink
Add argument support for detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jadb committed Mar 27, 2016
1 parent 6906157 commit e248f6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Network/Request.php
Expand Up @@ -617,6 +617,9 @@ public function __isset($name)
*/
public function is($type)
{
$args = func_get_args();
array_shift($args);

if (is_array($type)) {
$result = array_map([$this, 'is'], $type);
return count(array_filter($result)) > 0;
Expand All @@ -628,7 +631,7 @@ public function is($type)
}

if (!isset($this->_detectorCache[$type])) {
$this->_detectorCache[$type] = $this->_is($type);
$this->_detectorCache[$type] = $this->_is($type, $args);
}

return $this->_detectorCache[$type];
Expand All @@ -651,11 +654,12 @@ public function clearDetectorCache()
* this method will return true if the request matches any type.
* @return bool Whether or not the request is the type you are checking.
*/
protected function _is($type)
protected function _is($type, $args)
{
$detect = static::$_detectors[$type];
if (is_callable($detect)) {
return call_user_func($detect, $this);
array_unshift($args, $this);
return call_user_func_array($detect, $args);
}
if (isset($detect['env']) && $this->_environmentDetector($detect)) {
return true;
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -57,6 +57,23 @@ public function tearDown()
}
}

/**
* Test custom detector with extra arguments.
*
* @return void
*/
public function testCustomArgsDetector()
{
$request = new Request();
$request->addDetector('controller', function ($request, $name) {
return $request->param('controller') === $name;
});

$request->params = ['controller' => 'cake'];
$this->assertTrue($request->is('controller', 'cake'));
$this->assertTrue($request->isController('cake'));
}

/**
* Test the header detector.
*
Expand Down

0 comments on commit e248f6f

Please sign in to comment.