mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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.
This commit is contained in:
parent
1a460e47f1
commit
2b7723fd12
3 changed files with 59 additions and 38 deletions
|
@ -111,7 +111,7 @@ class Dispatcher extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
$request = $this->parseParams($request, $additionalParams);
|
$request = $this->parseParams($request, $additionalParams);
|
||||||
$this->params = $request;
|
$this->request = $request;
|
||||||
|
|
||||||
$controller = $this->_getController();
|
$controller = $this->_getController();
|
||||||
|
|
||||||
|
@ -147,25 +147,6 @@ class Dispatcher extends Object {
|
||||||
'base' => $request->base
|
'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);
|
return $this->_invoke($controller, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,13 +236,13 @@ class Dispatcher extends Object {
|
||||||
*/
|
*/
|
||||||
protected function &_getController() {
|
protected function &_getController() {
|
||||||
$controller = false;
|
$controller = false;
|
||||||
$ctrlClass = $this->__loadController($this->params);
|
$ctrlClass = $this->__loadController($this->request);
|
||||||
if (!$ctrlClass) {
|
if (!$ctrlClass) {
|
||||||
return $controller;
|
return $controller;
|
||||||
}
|
}
|
||||||
$ctrlClass .= 'Controller';
|
$ctrlClass .= 'Controller';
|
||||||
if (class_exists($ctrlClass)) {
|
if (class_exists($ctrlClass)) {
|
||||||
$controller = new $ctrlClass();
|
$controller = new $ctrlClass($this->request);
|
||||||
}
|
}
|
||||||
return $controller;
|
return $controller;
|
||||||
}
|
}
|
||||||
|
@ -273,14 +254,14 @@ class Dispatcher extends Object {
|
||||||
* @return string|bool Name of controller class name
|
* @return string|bool Name of controller class name
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function __loadController($params) {
|
function __loadController($request) {
|
||||||
$pluginName = $pluginPath = $controller = null;
|
$pluginName = $pluginPath = $controller = null;
|
||||||
if (!empty($params['plugin'])) {
|
if (!empty($request->params['plugin'])) {
|
||||||
$pluginName = $controller = Inflector::camelize($params['plugin']);
|
$pluginName = $controller = Inflector::camelize($request->params['plugin']);
|
||||||
$pluginPath = $pluginName . '.';
|
$pluginPath = $pluginName . '.';
|
||||||
}
|
}
|
||||||
if (!empty($params['controller'])) {
|
if (!empty($request->params['controller'])) {
|
||||||
$controller = Inflector::camelize($params['controller']);
|
$controller = Inflector::camelize($request->params['controller']);
|
||||||
}
|
}
|
||||||
if ($pluginPath . $controller) {
|
if ($pluginPath . $controller) {
|
||||||
if (App::import('Controller', $pluginPath . $controller)) {
|
if (App::import('Controller', $pluginPath . $controller)) {
|
||||||
|
|
|
@ -326,8 +326,10 @@ class Controller extends Object {
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* 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) {
|
if ($this->name === null) {
|
||||||
$r = null;
|
$r = null;
|
||||||
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
|
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
|
||||||
|
@ -355,9 +357,41 @@ class Controller extends Object {
|
||||||
$parentMethods[$key] = strtolower($value);
|
$parentMethods[$key] = strtolower($value);
|
||||||
}
|
}
|
||||||
$this->methods = array_diff($childMethods, $parentMethods);
|
$this->methods = array_diff($childMethods, $parentMethods);
|
||||||
|
|
||||||
|
if ($request instanceof CakeRequest) {
|
||||||
|
$this->_setRequest($request);
|
||||||
|
}
|
||||||
parent::__construct();
|
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.
|
* Merge components, helpers, and uses vars from AppController and PluginAppController.
|
||||||
*
|
*
|
||||||
|
@ -1010,22 +1044,22 @@ class Controller extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($assoc && isset($this->{$object}->{$assoc})) {
|
if ($assoc && isset($this->{$object}->{$assoc})) {
|
||||||
$object =& $this->{$object}->{$assoc};
|
$object = $this->{$object}->{$assoc};
|
||||||
} elseif (
|
} elseif (
|
||||||
$assoc && isset($this->{$this->modelClass}) &&
|
$assoc && isset($this->{$this->modelClass}) &&
|
||||||
isset($this->{$this->modelClass}->{$assoc}
|
isset($this->{$this->modelClass}->{$assoc}
|
||||||
)) {
|
)) {
|
||||||
$object =& $this->{$this->modelClass}->{$assoc};
|
$object = $this->{$this->modelClass}->{$assoc};
|
||||||
} elseif (isset($this->{$object})) {
|
} elseif (isset($this->{$object})) {
|
||||||
$object =& $this->{$object};
|
$object = $this->{$object};
|
||||||
} elseif (
|
} elseif (
|
||||||
isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object}
|
isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object}
|
||||||
)) {
|
)) {
|
||||||
$object =& $this->{$this->modelClass}->{$object};
|
$object = $this->{$this->modelClass}->{$object};
|
||||||
}
|
}
|
||||||
} elseif (empty($object) || $object === null) {
|
} elseif (empty($object) || $object === null) {
|
||||||
if (isset($this->{$this->modelClass})) {
|
if (isset($this->{$this->modelClass})) {
|
||||||
$object =& $this->{$this->modelClass};
|
$object = $this->{$this->modelClass};
|
||||||
} else {
|
} else {
|
||||||
$className = null;
|
$className = null;
|
||||||
$name = $this->uses[0];
|
$name = $this->uses[0];
|
||||||
|
@ -1033,9 +1067,9 @@ class Controller extends Object {
|
||||||
list($name, $className) = explode('.', $this->uses[0]);
|
list($name, $className) = explode('.', $this->uses[0]);
|
||||||
}
|
}
|
||||||
if ($className) {
|
if ($className) {
|
||||||
$object =& $this->{$className};
|
$object = $this->{$className};
|
||||||
} else {
|
} else {
|
||||||
$object =& $this->{$name};
|
$object = $this->{$name};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1046,7 +1080,7 @@ class Controller extends Object {
|
||||||
), E_USER_WARNING);
|
), E_USER_WARNING);
|
||||||
return array();
|
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])) {
|
if (isset($this->paginate[$object->alias])) {
|
||||||
$defaults = $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),
|
'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults),
|
||||||
'options' => $options
|
'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)) {
|
if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
|
||||||
$this->helpers[] = 'Paginator';
|
$this->helpers[] = 'Paginator';
|
||||||
|
|
|
@ -805,7 +805,7 @@ class DispatcherTest extends CakeTestCase {
|
||||||
$controller = $Dispatcher->dispatch($url, array('return' => 1));
|
$controller = $Dispatcher->dispatch($url, array('return' => 1));
|
||||||
|
|
||||||
$this->assertEqual('Timesheets', $controller->name);
|
$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';
|
$url = 'test_dispatch_pages/camelCased';
|
||||||
|
|
Loading…
Reference in a new issue