Skip to content

Commit

Permalink
Cleaning up the RequestHandlerCompoent
Browse files Browse the repository at this point in the history
removing redundant code and shifting a few things around so there
are less nested ifs and making things easier to follow.

Removing some variable setting, returning function calls instead.
  • Loading branch information
dogmatic69 committed Sep 14, 2012
1 parent 25b4471 commit e09bf02
Showing 1 changed file with 55 additions and 59 deletions.
114 changes: 55 additions & 59 deletions lib/Cake/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -107,8 +107,7 @@ class RequestHandlerComponent extends Component {
* @param array $settings Array of settings.
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
$default = array('checkHttpCache' => true);
parent::__construct($collection, $settings + $default);
parent::__construct($collection, $settings + array('checkHttpCache' => true));
$this->addInputType('xml', array(array($this, 'convertXml')));

$Controller = $collection->getController();
Expand Down Expand Up @@ -158,9 +157,11 @@ protected function _setExtension() {
$extensions = Router::extensions();
$preferred = array_shift($accept);
$preferredTypes = $this->response->mapType($preferred);
$similarTypes = array_intersect($extensions, $preferredTypes);
if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
$this->ext = array_shift($similarTypes);
if (!in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
$similarTypes = array_intersect($extensions, $preferredTypes);
if (count($similarTypes) === 1) {
$this->ext = array_shift($similarTypes);
}
}
}

Expand Down Expand Up @@ -269,8 +270,7 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
* @return boolean false if the render process should be aborted
**/
public function beforeRender(Controller $controller) {
$shouldCheck = $this->settings['checkHttpCache'];
if ($shouldCheck && $this->response->checkNotModified($this->request)) {
if ($this->settings['checkHttpCache'] && $this->response->checkNotModified($this->request)) {
return false;
}
}
Expand Down Expand Up @@ -395,13 +395,11 @@ public function isDelete() {
* Gets Prototype version if call is Ajax, otherwise empty string.
* The Prototype library sets a special "Prototype version" HTTP header.
*
* @return string Prototype version of component making Ajax call
* @return string|boolean When Ajax the prototype version of component making the call otherwise false
*/
public function getAjaxVersion() {
if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
return env('HTTP_X_PROTOTYPE_VERSION');
}
return false;
$httpX = env('HTTP_X_PROTOTYPE_VERSION');
return ($httpX === null) ? false : $httpX;
}

/**
Expand Down Expand Up @@ -467,19 +465,20 @@ public function getClientIP($safe = true) {
public function accepts($type = null) {
$accepted = $this->request->accepts();

if ($type == null) {
if (!$type) {
return $this->mapType($accepted);
} elseif (is_array($type)) {
}
if (is_array($type)) {
foreach ($type as $t) {
$t = $this->mapAlias($t);
if (in_array($t, $accepted)) {
return true;
}
}
return false;
} elseif (is_string($type)) {
$type = $this->mapAlias($type);
return in_array($type, $accepted);
}
if (is_string($type)) {
return in_array($this->mapAlias($type), $accepted);
}
return false;
}
Expand All @@ -496,18 +495,20 @@ public function requestedWith($type = null) {
if (!$this->request->is('post') && !$this->request->is('put')) {
return null;
}

list($contentType) = explode(';', env('CONTENT_TYPE'));
if ($type == null) {
return $this->mapType($contentType);
} elseif (is_array($type)) {
if (is_array($type)) {
foreach ($type as $t) {
if ($this->requestedWith($t)) {
return $t;
}
}
return false;
} elseif (is_string($type)) {
}

list($contentType) = explode(';', env('CONTENT_TYPE'));
if (!$type) {
return $this->mapType($contentType);
}
if (is_string($type)) {
return ($type == $this->mapType($contentType));
}
}
Expand Down Expand Up @@ -535,10 +536,9 @@ public function prefers($type = null) {
if (empty($acceptRaw)) {
return $this->ext;
}
$accepts = array_shift($acceptRaw);
$accepts = $this->mapType($accepts);
$accepts = $this->mapType(array_shift($acceptRaw));

if ($type == null) {
if (!$type) {
if (empty($this->ext) && !empty($accepts)) {
return $accepts[0];
}
Expand Down Expand Up @@ -611,8 +611,11 @@ public function renderAs(Controller $controller, $type, $options = array()) {
} elseif (empty($this->_renderType)) {
$controller->viewPath .= DS . $type;
} else {
$remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
$controller->viewPath = $remove;
$controller->viewPath = preg_replace(
"/([\/\\\\]{$this->_renderType})$/",
DS . $type,
$controller->viewPath
);
}
$this->_renderType = $type;
$controller->layoutPath = $type;
Expand All @@ -622,12 +625,8 @@ public function renderAs(Controller $controller, $type, $options = array()) {
}

$helper = ucfirst($type);
$isAdded = (
in_array($helper, $controller->helpers) ||
array_key_exists($helper, $controller->helpers)
);

if (!$isAdded) {
if (!in_array($helper, $controller->helpers) && empty($controller->helpers[$helper])) {
App::uses('AppHelper', 'View/Helper');
App::uses($helper . 'Helper', 'View/Helper');
if (class_exists($helper . 'Helper')) {
Expand All @@ -653,39 +652,35 @@ public function respondAs($type, $options = array()) {
$defaults = array('index' => null, 'charset' => null, 'attachment' => false);
$options = $options + $defaults;

$cType = $type;
if (strpos($type, '/') === false) {
$cType = $this->response->getMimeType($type);
if ($cType === false) {
return false;
}
if (is_array($cType) && isset($cType[$options['index']])) {
}
if (is_array($cType)) {
if (isset($cType[$options['index']])) {
$cType = $cType[$options['index']];
}
if (is_array($cType)) {
if ($this->prefers($cType)) {
$cType = $this->prefers($cType);
} else {
$cType = $cType[0];
}
}
} else {
$cType = $type;
}

if ($cType != null) {
if (empty($this->request->params['requested'])) {
$this->response->type($cType);
if ($this->prefers($cType)) {
$cType = $this->prefers($cType);
} else {
$cType = $cType[0];
}
}

if (!empty($options['charset'])) {
$this->response->charset($options['charset']);
}
if (!empty($options['attachment'])) {
$this->response->download($options['attachment']);
}
return true;
if (!$type) {
return false;
}
return false;
if (empty($this->request->params['requested'])) {
$this->response->type($cType);
}
if (!empty($options['charset'])) {
$this->response->charset($options['charset']);
}
if (!empty($options['attachment'])) {
$this->response->download($options['attachment']);
}
return true;
}

/**
Expand Down Expand Up @@ -758,7 +753,8 @@ public function addInputType($type, $handler) {
public function viewClassMap($type = null, $viewClass = null) {
if (!$viewClass && is_string($type) && isset($this->_viewClassMap[$type])) {
return $this->_viewClassMap[$type];
} elseif (is_string($type)) {
}
if (is_string($type)) {
$this->_viewClassMap[$type] = $viewClass;
} elseif (is_array($type)) {
foreach ($type as $key => $value) {
Expand Down

0 comments on commit e09bf02

Please sign in to comment.