diff --git a/src/Network/Request.php b/src/Network/Request.php index 5b3793d0f01..94e33995ed4 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.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; diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index be5ed719476..6a3000b733c 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -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. *