From 88bfa70cadd2f5d13cc49ebe1d3eb50c842f0f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Thu, 20 Nov 2014 20:37:16 +0100 Subject: [PATCH] Refactoring the detector code for CakeRequest::is() and adding default detectors for JSON and XML. --- lib/Cake/Network/CakeRequest.php | 82 ++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index ca50ae47c10..b26dfcf721a 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -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'), ); /** @@ -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']; @@ -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; }