Skip to content

Commit

Permalink
add a function for clearing the detector cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Nov 3, 2014
1 parent 7d5cc10 commit bc8d224
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Network/Request.php
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -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());
}

Expand Down

0 comments on commit bc8d224

Please sign in to comment.