mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-12 06:56:24 +00:00
Adding __call() for undefined method handling. Tests added.
This commit is contained in:
parent
3a0ad6f1a8
commit
c11095bc54
2 changed files with 34 additions and 32 deletions
|
@ -45,6 +45,9 @@ class CakeRequest {
|
||||||
/**
|
/**
|
||||||
* The built in detectors used with `is()` can be modified with `addDetector()`.
|
* 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
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $_detectors = array(
|
protected $_detectors = array(
|
||||||
|
@ -57,28 +60,10 @@ class CakeRequest {
|
||||||
'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'),
|
'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'),
|
||||||
'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'),
|
'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'),
|
||||||
'mobile' => array('env' => 'HTTP_USER_AGENT', 'options' => array(
|
'mobile' => array('env' => 'HTTP_USER_AGENT', 'options' => array(
|
||||||
'Android',
|
'Android', 'AvantGo', 'BlackBerry', 'DoCoMo', 'iPod', 'iPhone',
|
||||||
'AvantGo',
|
'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'PalmOS', 'PalmSource',
|
||||||
'BlackBerry',
|
'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\.Browser',
|
||||||
'DoCoMo',
|
'webOS', 'Windows CE', 'Xiino'
|
||||||
'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 '/';
|
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
|
* 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
|
* as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called
|
||||||
|
@ -242,16 +239,18 @@ class CakeRequest {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$detect = $this->_detectors[$type];
|
$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'];
|
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']));
|
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';
|
$pattern = '/' . implode('|', $detect['options']) . '/i';
|
||||||
return (bool)preg_match($pattern, env($detect['env']));
|
return (bool)preg_match($pattern, env($detect['env']));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (isset($detect['callback']) && is_callable($detect['callback'])) {
|
if (isset($detect['callback']) && is_callable($detect['callback'])) {
|
||||||
return call_user_func($detect['callback'], $this);
|
return call_user_func($detect['callback'], $this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -416,6 +416,7 @@ class CakeRequestTestCase extends CakeTestCase {
|
||||||
|
|
||||||
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
||||||
$this->assertTrue($request->is('delete'));
|
$this->assertTrue($request->is('delete'));
|
||||||
|
$this->assertTrue($request->isDelete());
|
||||||
|
|
||||||
$_SERVER['REQUEST_METHOD'] = 'delete';
|
$_SERVER['REQUEST_METHOD'] = 'delete';
|
||||||
$this->assertFalse($request->is('delete'));
|
$this->assertFalse($request->is('delete'));
|
||||||
|
@ -440,9 +441,11 @@ class CakeRequestTestCase extends CakeTestCase {
|
||||||
|
|
||||||
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
|
||||||
$this->assertFalse($request->is('ajax'));
|
$this->assertFalse($request->is('ajax'));
|
||||||
|
$this->assertFalse($request->isAjax());
|
||||||
|
|
||||||
$_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
|
$_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
|
||||||
$this->assertTrue($request->is('mobile'));
|
$this->assertTrue($request->is('mobile'));
|
||||||
|
$this->assertTrue($request->isMobile());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue