From 2b7723fd12c9d9f92076da2f4d6f4297ab36f4f2 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 6 May 2010 23:18:50 -0400 Subject: [PATCH] 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. --- cake/dispatcher.php | 35 ++++------------ cake/libs/controller/controller.php | 60 +++++++++++++++++++++++----- cake/tests/cases/dispatcher.test.php | 2 +- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index b6434e293..a02cdce3d 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -111,7 +111,7 @@ class Dispatcher extends Object { } $request = $this->parseParams($request, $additionalParams); - $this->params = $request; + $this->request = $request; $controller = $this->_getController(); @@ -147,25 +147,6 @@ class Dispatcher extends Object { '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 @@ class Dispatcher extends Object { */ 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 @@ class Dispatcher extends Object { * @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)) { diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index e8bce6b77..16216899a 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -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 @@ class Controller extends Object { $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,22 +1044,22 @@ class Controller extends Object { } 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]; @@ -1033,9 +1067,9 @@ class Controller extends Object { 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 @@ class Controller extends Object { ), 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 @@ class Controller extends Object { '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'; diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index bd6cd1ac2..f524bf512 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -805,7 +805,7 @@ class DispatcherTest extends CakeTestCase { $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';