Skip to content

Commit

Permalink
Refactoring the detector code for CakeRequest::is() and adding defaul…
Browse files Browse the repository at this point in the history
…t detectors for JSON and XML.
  • Loading branch information
Florian Krämer committed Nov 20, 2014
1 parent fe6d3e3 commit 88bfa70
Showing 1 changed file with 68 additions and 14 deletions.
82 changes: 68 additions & 14 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -111,7 +111,9 @@ class CakeRequest implements ArrayAccess {
'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\\.Browser',
'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino'
)),
'requested' => array('param' => 'requested', 'value' => 1)
'requested' => array('param' => 'requested', 'value' => 1),
'json' => array('header' => array('application/json'), 'param' => 'ext', 'value' => 'json'),
'xml' => array('header' => array('application/xml', 'text/xml'), 'param' => 'ext', 'value' => 'xml'),
);

/**
Expand Down Expand Up @@ -498,6 +500,71 @@ public function is($type) {
return false;
}
$detect = $this->_detectors[$type];
if (isset($detect['env'])) {
if ($this->_environmentDetector($detect)) {
return true;
}
}
if (isset($detect['header'])) {
if ($this->_environmentDetector($detect)) {
return true;
}
}
if (isset($detect['param'])) {
if ($this->_paramDetector($detect)) {
return true;
}
}
if (isset($detect['callback']) && is_callable($detect['callback'])) {
return call_user_func($detect['callback'], $this);
}
return false;
}

/**
* Detects if a specific header is present
*
* @param $detect Detector options array.
* @return bool Whether or not the request is the type you are checking.
*/
protected function _headerDetector($detect) {
$headers = getallheaders();
if (isset($headers['Accept'])) {
$headers = explode(',', $headers['Accept']);
foreach ($detect['header'] as $header) {
if (in_array($header, $headers)) {
return true;
}
}
}
return false;
}

/**
* Detects if a specific header is present
*
* @param $detect Detector options array.
* @return bool Whether or not the request is the type you are checking.
*/
protected function _paramDetector($detect) {
$key = $detect['param'];
if (isset($detect['value'])) {
$value = $detect['value'];
return isset($this->params[$key]) ? $this->params[$key] == $value : false;
}
if (isset($detect['options'])) {
return isset($this->params[$key]) ? in_array($this->params[$key], $detect['options']) : false;
}
return false;
}

/**
* Detects if a specific header is present
*
* @param $detect Detector options array.
* @return bool Whether or not the request is the type you are checking.
*/
protected function _environmentDetector($detect) {
if (isset($detect['env'])) {
if (isset($detect['value'])) {
return env($detect['env']) == $detect['value'];
Expand All @@ -510,19 +577,6 @@ public function is($type) {
return (bool)preg_match($pattern, env($detect['env']));
}
}
if (isset($detect['param'])) {
$key = $detect['param'];
if (isset($detect['value'])) {
$value = $detect['value'];
return isset($this->params[$key]) ? $this->params[$key] == $value : false;
}
if (isset($detect['options'])) {
return isset($this->params[$key]) ? in_array($this->params[$key], $detect['options']) : false;
}
}
if (isset($detect['callback']) && is_callable($detect['callback'])) {
return call_user_func($detect['callback'], $this);
}
return false;
}

Expand Down

0 comments on commit 88bfa70

Please sign in to comment.