Adding fix for Ticket , and additional code enhancements

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3099 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-06-14 23:43:59 +00:00
parent d09f6c4de9
commit f85cff7394

View file

@ -29,8 +29,7 @@
*/ */
if (!defined('REQUEST_MOBILE_UA')) { if (!defined('REQUEST_MOBILE_UA')) {
define('REQUEST_MOBILE_UA', define('REQUEST_MOBILE_UA', '(MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
'(AvantGo|BlackBerry|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
} }
/** /**
@ -41,22 +40,24 @@ if (!defined('REQUEST_MOBILE_UA')) {
* *
*/ */
class RequestHandlerComponent extends Object{ class RequestHandlerComponent extends Object{
var $controller = true;
var $ajaxLayout = 'ajax'; var $ajaxLayout = 'ajax';
var $disableStartup = false; var $disableStartup = false;
var $enabled = true;
var $__requestContent = array( var $__requestContent = array(
'js' => 'text/javascript', 'javascript' => 'text/javascript',
'css' => 'text/css', 'css' => 'text/css',
'html' => 'text/html', 'html' => array('text/html', '*/*'),
'form' => 'application/x-www-form-urlencoded', 'text' => 'text/plain',
'file' => 'multipart/form-data', 'form' => 'application/x-www-form-urlencoded',
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'), 'file' => 'multipart/form-data',
'xml' => array('application/xml', 'text/xml'), 'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
'rss' => 'application/rss+xml', 'xml' => array('application/xml', 'text/xml'),
'atom' => 'application/atom+xml' 'rss' => 'application/rss+xml',
'atom' => 'application/atom+xml'
); );
var $__acceptTypes = array(); var $__acceptTypes = array();
@ -70,6 +71,11 @@ class RequestHandlerComponent extends Object{
$this->__acceptTypes[$i] = $type[0]; $this->__acceptTypes[$i] = $type[0];
} }
} }
foreach ($this->__requestContent as $type => $data) {
$this->setContent($type, $data);
}
parent::__construct(); parent::__construct();
} }
/** /**
@ -79,16 +85,27 @@ class RequestHandlerComponent extends Object{
* @return null * @return null
*/ */
function startup(&$controller) { function startup(&$controller) {
if ($this->disableStartup) { if ($this->disableStartup || !$this->enabled) {
return;
}
$this->setView($controller);
}
/**
* Sets a controller's layout/View class based on request headers
*
* @param object The controller object
* @return null
*/
function setView(&$controller) {
if ($this->setAjax($controller)) {
return; return;
} }
$this->setAjax($controller);
} }
/** /**
* Sets a controller's layout based on whether or not the current call is Ajax * Sets a controller's layout based on whether or not the current call is Ajax
* *
* @param object The controller object * @param object The controller object
* @return null * @return boolean True if call is Ajax, otherwise false
*/ */
function setAjax(&$controller) { function setAjax(&$controller) {
if ($this->isAjax()) { if ($this->isAjax()) {
@ -96,7 +113,9 @@ class RequestHandlerComponent extends Object{
// Add UTF-8 header for IE6 on XPsp2 bug // Add UTF-8 header for IE6 on XPsp2 bug
header ('Content-Type: text/html; charset=UTF-8'); header ('Content-Type: text/html; charset=UTF-8');
return true;
} }
return false;
} }
/** /**
* Returns true if the current call is from Ajax, false otherwise * Returns true if the current call is from Ajax, false otherwise
@ -104,11 +123,7 @@ class RequestHandlerComponent extends Object{
* @return bool True if call is Ajax * @return bool True if call is Ajax
*/ */
function isAjax() { function isAjax() {
if (env('HTTP_X_REQUESTED_WITH') != null) { return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest";
return env('HTTP_X_REQUESTED_WITH') == "XMLHttpRequest";
} else {
return false;
}
} }
/** /**
* Returns true if the current call accepts an XML response, false otherwise * Returns true if the current call accepts an XML response, false otherwise
@ -186,6 +201,13 @@ class RequestHandlerComponent extends Object{
* @return void * @return void
*/ */
function setContent($name, $type) { function setContent($name, $type) {
if (!is_array($type) || isset($type[0])) {
$type = array(
'layout' => Inflector::underscore($name),
'view' => Inflector::camelize($name),
'content' => $type
);
}
$this->__requestContent[$name] = $type; $this->__requestContent[$name] = $type;
} }
/** /**
@ -307,7 +329,7 @@ class RequestHandlerComponent extends Object{
*/ */
function accepts($type = null) { function accepts($type = null) {
if ($type == null) { if ($type == null) {
return $this->__acceptTypes; return $this->mapType($this->__acceptTypes);
} else if(is_array($type)) { } else if(is_array($type)) {
foreach($type as $t) { foreach($type as $t) {
if ($this->accepts($t) == true) { if ($this->accepts($t) == true) {
@ -316,16 +338,12 @@ class RequestHandlerComponent extends Object{
} }
return false; return false;
} else if(is_string($type)) { } else if(is_string($type)) {
// If client only accepts */*, then assume default HTML browser
if ($type == 'html' && $this->__acceptTypes === array('*/*')) {
return true;
}
if (!in_array($type, array_keys($this->__requestContent))) { if (!in_array($type, array_keys($this->__requestContent))) {
return false; return false;
} }
$content = $this->__requestContent[$type]; $content = $this->__requestContent[$type]['content'];
if (is_array($content)) { if (is_array($content)) {
foreach($content as $c) { foreach($content as $c) {
@ -351,6 +369,40 @@ class RequestHandlerComponent extends Object{
if ($type == null) { if ($type == null) {
return $this->accepts(null); return $this->accepts(null);
} }
}
/**
* Maps a content-type back to an alias
*
* @param mixed $type
* @returns mixed
* @access public
*/
function mapType($ctype) {
if (is_array($ctype)) {
$out = array();
foreach ($ctype as $t) {
$out[] = $this->mapType($t);
}
return $out;
} else {
$keys = array_keys($this->__requestContent);
$count = count($keys);
for ($i = 0; $i < $count; $i++) {
$name = $keys[$i];
$type = $this->__requestContent[$name];
if (is_array($type['content']) && in_array($ctype, $type['content'])) {
return $name;
} elseif (!is_array($type['content']) && $type['content'] == $ctype) {
return $name;
}
}
return $ctype;
}
} }
} }