Refactoring RequestHandler

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4694 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-27 05:02:40 +00:00
parent 70e11838a7
commit 1685fe8fc4

View file

@ -120,6 +120,13 @@ class RequestHandlerComponent extends Object {
* @see Router::parseExtensions() * @see Router::parseExtensions()
*/ */
var $__acceptTypes = array(); var $__acceptTypes = array();
/**
* The template to use when rendering the given content type.
*
* @var string
* @access private
*/
var $__renderType = null;
/** /**
* Contains the file extension parsed out by the Router * Contains the file extension parsed out by the Router
* *
@ -196,19 +203,7 @@ class RequestHandlerComponent extends Object {
$controller->params['isAjax'] = $this->isAjax(); $controller->params['isAjax'] = $this->isAjax();
if (!empty($this->ext) && !in_array($this->ext, array('html', 'htm')) && in_array($this->ext, array_keys($this->__requestContent))) { if (!empty($this->ext) && !in_array($this->ext, array('html', 'htm')) && in_array($this->ext, array_keys($this->__requestContent))) {
$controller->ext = '.ctp'; $this->renderAs($controller, $this->ext);
$controller->viewPath .= '/' . $this->ext;
$controller->layoutPath = $this->ext;
if (in_array($this->ext, array_keys($this->__requestContent))) {
$this->respondAs($this->ext);
}
if (!in_array(ucfirst($this->ext), $controller->helpers)) {
if (file_exists(HELPERS . $this->ext . '.php') || fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $this->ext . '.php')) {
$controller->helpers[] = ucfirst($this->ext);
}
}
} else { } else {
$this->setAjax($controller); $this->setAjax($controller);
} }
@ -531,6 +526,36 @@ class RequestHandlerComponent extends Object {
return $accepts[0]; return $accepts[0];
} }
} }
/**
* Sets the layout and template paths for the content type defined by $type.
*
* @param object $controller A reference to a controller object
* @param string $type
* @return void
* @access public
* @see RequestHandlerComponent::setContent()
* @see RequestHandlerComponent::respondAs()
*/
function renderAs(&$controller, $type) {
$controller->ext = '.ctp';
if (empty($this->__renderType)) {
$controller->viewPath .= '/' . $type;
} else {
$controller->viewPath = preg_replace("/\/{$type}$/", '/' . $type, $controller->viewPath);
}
$this->__renderType = $type;
$controller->layoutPath = $type;
if (in_array($type, array_keys($this->__requestContent))) {
$this->respondAs($type);
}
if (!in_array(ucfirst($type), $controller->helpers)) {
if (file_exists(HELPERS . $type . '.php') || fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $type . '.php')) {
$controller->helpers[] = ucfirst($type);
}
}
}
/** /**
* Sets the response header based on type map index name. If DEBUG is greater * Sets the response header based on type map index name. If DEBUG is greater
* than 2, the header is not set. * than 2, the header is not set.
@ -553,7 +578,7 @@ class RequestHandlerComponent extends Object {
if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) { if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
return false; return false;
} }
$options = am(array('index' => 0, 'charset' => null), $options); $options = am(array('index' => 0, 'charset' => null, 'attachment' => false), $options);
if (strpos($type, '/') === false && isset($this->__requestContent[$type])) { if (strpos($type, '/') === false && isset($this->__requestContent[$type])) {
$cType = null; $cType = null;
@ -579,12 +604,19 @@ class RequestHandlerComponent extends Object {
if ($cType != null) { if ($cType != null) {
$header = 'Content-type: ' . $cType; $header = 'Content-type: ' . $cType;
$headers = array();
if ($options['charset'] != null) { if (!empty($options['charset'])) {
$header .= '; charset=' . $options['charset']; $header .= '; charset=' . $options['charset'];
} }
if (DEBUG < 2) { if (!empty($options['attachment'])) {
header('Content-Disposition: attachment; filename="' . $options['attachment'] . '"');
}
if (Configure::read() < 2) {
header($header); header($header);
foreach ($headers as $h) {
header($h);
}
} }
$this->__responseTypeSet = $cType; $this->__responseTypeSet = $cType;
return true; return true;