Skip to content
Permalink
Browse files

Add argument support for detectors

  • Loading branch information...
jadb committed Mar 27, 2016
1 parent 6906157 commit e248f6f54e50480d5c6e84b0305ea28c9ce8e0b9
Showing with 24 additions and 3 deletions.
  1. +7 −3 src/Network/Request.php
  2. +17 −0 tests/TestCase/Network/RequestTest.php
@@ -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;
@@ -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];
@@ -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;
@@ -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.
*

0 comments on commit e248f6f

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