Browse Source

add a function for clearing the detector cache

AD7six 11 years ago
parent
commit
bc8d224cbe
2 changed files with 21 additions and 5 deletions
  1. 14 5
      src/Network/Request.php
  2. 7 0
      tests/TestCase/Network/RequestTest.php

+ 14 - 5
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 @@ class Request implements \ArrayAccess {
 			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 @@ class Request implements \ArrayAccess {
 	public function env($key, $value = null) {
 		if ($value !== null) {
 			$this->_environment[$key] = $value;
-			$this->_isResults = [];
+			$this->clearDetectorCache();
 			return $this;
 		}
 

+ 7 - 0
tests/TestCase/Network/RequestTest.php

@@ -847,26 +847,33 @@ class RequestTest extends TestCase {
 
 		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());
 	}