From 18ab04b32b1dac634806cb88c361885091331e17 Mon Sep 17 00:00:00 2001 From: gwoo Date: Thu, 27 Sep 2007 10:58:06 +0000 Subject: [PATCH] refactor Dispatcher::__getController to pass all tests git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5697 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/basics.php | 11 ++-- cake/dispatcher.php | 83 +++++++++++++++------------- cake/tests/cases/dispatcher.test.php | 3 +- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/cake/basics.php b/cake/basics.php index fa5069516..225fd4fd0 100644 --- a/cake/basics.php +++ b/cake/basics.php @@ -340,6 +340,7 @@ if ($name === null) { return true; } + $parent = 'AppController'; if (strpos($name, '.') !== false) { list($plugin, $name) = explode('.', $name); @@ -357,7 +358,7 @@ } if (empty($name)) { - if (!class_exists(Inflector::camelize($plugin . 'controller'))) { + if (!class_exists(Inflector::camelize($plugin . 'Controller'))) { if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) { require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php'); return true; @@ -366,10 +367,8 @@ } if (!class_exists($name . 'Controller')) { - $name = Inflector::underscore($name); $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $name . '_controller.php'; - if (file_exists($file)) { require($file); return true; @@ -377,17 +376,15 @@ if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) { require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php'); return true; - } else { - return false; } } + return false; } return true; } $className = $name . 'Controller'; - - if (class_exists($className)) { + if (class_exists($className) && low(get_parent_class($className)) !== low($name . 'AppController')) { return true; } else { $name = Inflector::underscore($className); diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 0e38b6543..76661426b 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -141,7 +141,7 @@ class Dispatcher extends Object { Router::setRequestInfo(array($this->params, array('base' => $this->base, 'webroot' => $this->webroot))); return $this->cakeError('missingController', array( array( - 'className' => $controller.'Controller', + 'className' => Inflector::camelize($this->params['controller']) . 'Controller', 'webroot' => $this->webroot, 'url' => $url, 'base' => $this->base @@ -471,57 +471,64 @@ class Dispatcher extends Object { /** * Get controller to use, either plugin controller or application controller * - * @param array $params Array on where to re-set 'controller', 'action', and 'pass' indexes + * @param array $params Array * @return mixed name of controller if not loaded, or object if loaded - * @access protected + * @access private */ - function __getController($params = null, $continue = true) { - - if(!$params) { + function __getController($params = null) { + if (!is_array($params)) { $params = $this->params; } - $pluginPath = $controller = $ctrlClass = null; - - if (!empty($params['controller'])) { - $controller = Inflector::camelize($params['controller']); - $ctrlClass = $controller.'Controller'; + $controller = false; + if (!$ctrlClass = $this->__loadController($params)) { + $params = $this->_restructureParams($params); + if (!$ctrlClass = $this->__loadController($params)) { + $params = am($params, array('controller'=> $params['plugin'], 'action'=> $params['controller'])); + if (!$ctrlClass = $this->__loadController($params)) { + return false; + } + } } + if (class_exists($ctrlClass)) { + $this->params = $params; + $controller =& new $ctrlClass(); + } + + return $controller; + } +/** + * load controller and return controller class + * + * @param array $params Array + * @return mixed name of controller class name + * @access private + */ + function __loadController($params) { + $pluginName = $pluginPath = $controller = $ctrlClass = null; + if (!empty($params['plugin'])) { $this->plugin = $params['plugin']; - $pluginPath = Inflector::camelize($this->plugin).'.'; + $pluginName = Inflector::camelize($params['plugin']); + $pluginPath = $pluginName . '.'; } - if ($pluginPath . $controller && loadController($pluginPath . $controller)) { - if(!class_exists(low($ctrlClass)) && $this->plugin) { - $ctrlClass = Inflector::camelize($params['plugin']) . 'Controller'; - $pass = $params['action']; + if (!empty($params['controller'])) { + $controller = Inflector::camelize($params['controller']); + $ctrlClass = $controller . 'Controller'; + } elseif ($this->plugin) { + $this->params['controller'] = $this->plugin; + $controller = $pluginName; + $ctrlClass = $controller . 'Controller'; + } - $params = am($params, array( - 'plugin' => $params['plugin'], - 'controller' => $params['plugin'], - 'action' => $params['controller'], - )); - array_unshift($params['pass'], $pass); + if ($pluginPath . $controller) { + if (loadController($pluginPath . $controller)) { + return $ctrlClass; } - if(class_exists(low($ctrlClass))) { - $controller =& new $ctrlClass(); - } - } elseif ($continue == true){ - $params = $this->_restructureParams($params); - $controller = $this->__getController($params, false); - return $controller; } - - if (!class_exists(low($ctrlClass))) { - $controller = Inflector::camelize($this->params['controller']); - $this->plugin = null; - return $controller; - } - - $this->params = $params; - return $controller; + return false; } /** * Returns the REQUEST_URI from the server environment, or, failing that, diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index 09970349b..1c9b0dbcd 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -509,7 +509,6 @@ class DispatcherTest extends UnitTestCase { $this->assertIdentical($expected, $controller->base); } - function testAutomaticPluginDispatch() { $_POST = array(); $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php'; @@ -565,7 +564,7 @@ class DispatcherTest extends UnitTestCase { $expected = 'add'; $this->assertIdentical($controller->action, $expected); - $expected = array('param:value', 'param2:value2'); + $expected = array('param'=>'value', 'param2'=>'value2'); $this->assertEqual($controller->params['pass'], $expected); }