Adding __call() for undefined method handling. Tests added.

This commit is contained in:
Mark Story 2010-04-26 23:34:30 -04:00
parent 3a0ad6f1a8
commit c11095bc54
2 changed files with 34 additions and 32 deletions

View file

@ -45,6 +45,9 @@ class CakeRequest {
/**
* The built in detectors used with `is()` can be modified with `addDetector()`.
*
* There are several ways to specify a detector, see CakeRequest::addDetector() for the
* various formats and ways to define detectors.
*
* @var array
*/
protected $_detectors = array(
@ -57,28 +60,10 @@ class CakeRequest {
'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'),
'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'),
'mobile' => array('env' => 'HTTP_USER_AGENT', 'options' => array(
'Android',
'AvantGo',
'BlackBerry',
'DoCoMo',
'iPod',
'iPhone',
'J2ME',
'MIDP',
'NetFront',
'Nokia',
'Opera Mini',
'PalmOS',
'PalmSource',
'portalmmm',
'Plucker',
'ReqwirelessWeb',
'SonyEricsson',
'Symbian',
'UP\.Browser',
'webOS',
'Windows CE',
'Xiino'
'Android', 'AvantGo', 'BlackBerry', 'DoCoMo', 'iPod', 'iPhone',
'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'PalmOS', 'PalmSource',
'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\.Browser',
'webOS', 'Windows CE', 'Xiino'
))
);
/**
@ -228,6 +213,18 @@ class CakeRequest {
return '/';
}
/**
* Missing method handler, handles wrapping older style isAjax() type methods
*
* @return void
*/
public function __call($name, $params) {
if (strpos($name, 'is') === 0) {
$type = strtolower(substr($name, 2));
return $this->is($type);
}
}
/**
* Check whether or not a Request is a certain type. Uses the built in detection rules
* as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called
@ -242,16 +239,18 @@ class CakeRequest {
return false;
}
$detect = $this->_detectors[$type];
if (isset($detect['env']) && isset($detect['value'])) {
if (isset($detect['env'])) {
if (isset($detect['value'])) {
return env($detect['env']) == $detect['value'];
}
if (isset($detect['env']) && isset($detect['pattern'])) {
if (isset($detect['pattern'])) {
return (bool)preg_match($detect['pattern'], env($detect['env']));
}
if (isset($detect['env']) && isset($detect['options'])) {
if (isset($detect['options'])) {
$pattern = '/' . implode('|', $detect['options']) . '/i';
return (bool)preg_match($pattern, env($detect['env']));
}
}
if (isset($detect['callback']) && is_callable($detect['callback'])) {
return call_user_func($detect['callback'], $this);
}

View file

@ -416,6 +416,7 @@ class CakeRequestTestCase extends CakeTestCase {
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$this->assertTrue($request->is('delete'));
$this->assertTrue($request->isDelete());
$_SERVER['REQUEST_METHOD'] = 'delete';
$this->assertFalse($request->is('delete'));
@ -440,9 +441,11 @@ class CakeRequestTestCase extends CakeTestCase {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
$this->assertFalse($request->is('ajax'));
$this->assertFalse($request->isAjax());
$_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
$this->assertTrue($request->is('mobile'));
$this->assertTrue($request->isMobile());
}
/**