From bc8d224cbed652b2e769f8aa8fab85dc4d82fd99 Mon Sep 17 00:00:00 2001 From: AD7six Date: Mon, 3 Nov 2014 09:06:31 +0000 Subject: [PATCH] add a function for clearing the detector cache --- src/Network/Request.php | 19 ++++++++++++++----- tests/TestCase/Network/RequestTest.php | 7 +++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Network/Request.php b/src/Network/Request.php index 6cb9c50456f..6b753eff153 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.php @@ -137,7 +137,7 @@ class Request implements \ArrayAccess { * * @var array */ - protected $_isResults = []; + protected $_detectorCache = []; /** * Copy of php://input. Since this stream can only be read once in most SAPI's @@ -593,11 +593,20 @@ public function is($type) { return false; } - if (!isset($this->_isResults[$type])) { - $this->_isResults[$type] = $this->_is($type); + if (!isset($this->_detectorCache[$type])) { + $this->_detectorCache[$type] = $this->_is($type); } - return $this->_isResults[$type]; + return $this->_detectorCache[$type]; + } + +/** + * Clears the instance detector cache, used by the is() function + * + * @return void + */ + public function clearDetectorCache() { + $this->_detectorCache = []; } /** @@ -1084,7 +1093,7 @@ public function cookie($key) { public function env($key, $value = null) { if ($value !== null) { $this->_environment[$key] = $value; - $this->_isResults = []; + $this->clearDetectorCache(); return $this; } diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index 79f9f147743..9e80543a078 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -847,26 +847,33 @@ public function testAddDetector() { Request::addDetector('index', array('param' => 'action', 'value' => 'index')); $request->params['action'] = 'index'; + $request->clearDetectorCache(); $this->assertTrue($request->isIndex()); $request->params['action'] = 'add'; + $request->clearDetectorCache(); $this->assertFalse($request->isIndex()); $request->return = true; + $request->clearDetectorCache(); $this->assertTrue($request->isCallMe()); $request->return = false; + $request->clearDetectorCache(); $this->assertFalse($request->isCallMe()); Request::addDetector('callme', array($this, 'detectCallback')); $request->return = true; + $request->clearDetectorCache(); $this->assertTrue($request->isCallMe()); Request::addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt'))); $request->params['ext'] = 'pdf'; + $request->clearDetectorCache(); $this->assertTrue($request->is('extension')); $request->params['ext'] = 'exe'; + $request->clearDetectorCache(); $this->assertFalse($request->isExtension()); }