Skip to content
Permalink
Browse files

Removing reference operators and making Controller::__construct take …

…a CakeRequest. If one is passed all the necessary properties will be populated. This lightens the Dispatcher, and gives more control to the end developer.
  • Loading branch information...
markstory committed May 7, 2010
1 parent 1a460e4 commit 2b7723fd12c9d9f92076da2f4d6f4297ab36f4f2
Showing with 59 additions and 38 deletions.
  1. +8 −27 cake/dispatcher.php
  2. +50 −10 cake/libs/controller/controller.php
  3. +1 −1 cake/tests/cases/dispatcher.test.php
@@ -111,7 +111,7 @@ public function dispatch($url = null, $additionalParams = array()) {
}
$request = $this->parseParams($request, $additionalParams);
$this->params = $request;
$this->request = $request;
$controller = $this->_getController();
@@ -147,25 +147,6 @@ public function dispatch($url = null, $additionalParams = array()) {
'base' => $request->base
)));
}
$controller->base = $request->base;
$controller->here = $request->here;
$controller->webroot = $request->webroot;
$controller->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
$controller->params = $request;
$controller->request = $request;
$controller->action =& $request->params['action'];
$controller->passedArgs = array_merge($request->params['pass'], $request->params['named']);
$controller->data = null;
if (!empty($request->params['data'])) {
$controller->data =& $request->params['data'];
}
if (array_key_exists('return', $request->params) && $request->params['return'] == 1) {
$controller->autoRender = false;
}
if (!empty($request->params['bare'])) {
$controller->autoLayout = false;
}
return $this->_invoke($controller, $request);
}
@@ -255,13 +236,13 @@ public function parseParams(CakeRequest $request, $additionalParams = array()) {
*/
protected function &_getController() {
$controller = false;
$ctrlClass = $this->__loadController($this->params);
$ctrlClass = $this->__loadController($this->request);
if (!$ctrlClass) {
return $controller;
}
$ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) {
$controller = new $ctrlClass();
$controller = new $ctrlClass($this->request);
}
return $controller;
}
@@ -273,14 +254,14 @@ protected function &_getController() {
* @return string|bool Name of controller class name
* @access private
*/
function __loadController($params) {
function __loadController($request) {
$pluginName = $pluginPath = $controller = null;
if (!empty($params['plugin'])) {
$pluginName = $controller = Inflector::camelize($params['plugin']);
if (!empty($request->params['plugin'])) {
$pluginName = $controller = Inflector::camelize($request->params['plugin']);
$pluginPath = $pluginName . '.';
}
if (!empty($params['controller'])) {
$controller = Inflector::camelize($params['controller']);
if (!empty($request->params['controller'])) {
$controller = Inflector::camelize($request->params['controller']);
}
if ($pluginPath . $controller) {
if (App::import('Controller', $pluginPath . $controller)) {
@@ -326,8 +326,10 @@ class Controller extends Object {
/**
* Constructor.
*
* @param CakeRequest $request Request object for this controller can be null for testing.
* But expect that features that use the params will not work.
*/
public function __construct() {
public function __construct($request = null) {
if ($this->name === null) {
$r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
@@ -355,9 +357,41 @@ public function __construct() {
$parentMethods[$key] = strtolower($value);
}
$this->methods = array_diff($childMethods, $parentMethods);
if ($request instanceof CakeRequest) {
$this->_setRequest($request);
}
parent::__construct();
}
/**
* Sets the request objects and configures a number of controller properties
* based on the contents of the request.
*
* @param CakeRequest $request
* @return void
*/
protected function _setRequest(CakeRequest $request) {
$this->base = $request->base;
$this->here = $request->here;
$this->webroot = $request->webroot;
$this->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
$this->params = $this->request = $request;
$this->action =& $request->params['action'];
$this->passedArgs = array_merge($request->params['pass'], $request->params['named']);
$this->data = null;
if (!empty($request->params['data'])) {
$this->data =& $request->params['data'];
}
if (array_key_exists('return', $request->params) && $request->params['return'] == 1) {
$this->autoRender = false;
}
if (!empty($request->params['bare'])) {
$this->autoLayout = false;
}
}
/**
* Merge components, helpers, and uses vars from AppController and PluginAppController.
*
@@ -1010,32 +1044,32 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
}
if ($assoc && isset($this->{$object}->{$assoc})) {
$object =& $this->{$object}->{$assoc};
$object = $this->{$object}->{$assoc};
} elseif (
$assoc && isset($this->{$this->modelClass}) &&
isset($this->{$this->modelClass}->{$assoc}
)) {
$object =& $this->{$this->modelClass}->{$assoc};
$object = $this->{$this->modelClass}->{$assoc};
} elseif (isset($this->{$object})) {
$object =& $this->{$object};
$object = $this->{$object};
} elseif (
isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object}
)) {
$object =& $this->{$this->modelClass}->{$object};
$object = $this->{$this->modelClass}->{$object};
}
} elseif (empty($object) || $object === null) {
if (isset($this->{$this->modelClass})) {
$object =& $this->{$this->modelClass};
$object = $this->{$this->modelClass};
} else {
$className = null;
$name = $this->uses[0];
if (strpos($this->uses[0], '.') !== false) {
list($name, $className) = explode('.', $this->uses[0]);
}
if ($className) {
$object =& $this->{$className};
$object = $this->{$className};
} else {
$object =& $this->{$name};
$object = $this->{$name};
}
}
}
@@ -1046,7 +1080,7 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
), E_USER_WARNING);
return array();
}
$options = array_merge($this->params, $this->params['url'], $this->passedArgs);
$options = array_merge($this->params->params, $this->params['url'], $this->passedArgs);
if (isset($this->paginate[$object->alias])) {
$defaults = $this->paginate[$object->alias];
@@ -1177,7 +1211,13 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults),
'options' => $options
);
$this->params['paging'][$object->alias] = $paging;
if (!isset($this->params['paging'])) {
$this->params['paging'] = array();
}
$this->params['paging'] = array_merge(
(array)$this->params['paging'],
array($object->alias => $paging)
);
if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
$this->helpers[] = 'Paginator';
@@ -805,7 +805,7 @@ public function testDispatch() {
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertEqual('Timesheets', $controller->name);
$this->assertEqual('/timesheets/index.php', $Dispatcher->params->base);
$this->assertEqual('/timesheets/index.php', $Dispatcher->request->base);
$url = 'test_dispatch_pages/camelCased';

0 comments on commit 2b7723f

Please sign in to comment.
You can’t perform that action at this time.