Skip to content
Permalink
Browse files

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

  • Loading branch information...
markstory committed Apr 27, 2010
1 parent 3a0ad6f commit c11095bc54ae094ebf05ae308ec8dde8c7695e9a
Showing with 34 additions and 32 deletions.
  1. +30 −31 cake/libs/cake_request.php
  2. +4 −1 cake/tests/cases/libs/cake_request.test.php
@@ -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 @@ public function referer($local = false) {
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,15 +239,17 @@ public function is($type) {
return false;
}
$detect = $this->_detectors[$type];
if (isset($detect['env']) && isset($detect['value'])) {
return env($detect['env']) == $detect['value'];
}
if (isset($detect['env']) && isset($detect['pattern'])) {
return (bool)preg_match($detect['pattern'], env($detect['env']));
}
if (isset($detect['env']) && isset($detect['options'])) {
$pattern = '/' . implode('|', $detect['options']) . '/i';
return (bool)preg_match($pattern, env($detect['env']));
if (isset($detect['env'])) {
if (isset($detect['value'])) {
return env($detect['env']) == $detect['value'];
}
if (isset($detect['pattern'])) {
return (bool)preg_match($detect['pattern'], env($detect['env']));
}
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);
@@ -416,6 +416,7 @@ function testIsHttpMethods() {
$_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 @@ function testisAjaxFlashAndFriends() {
$_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());
}
/**

0 comments on commit c11095b

Please sign in to comment.
You can’t perform that action at this time.