Refactoring loading of plugins.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5464 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-07-25 21:08:12 +00:00
parent b29366ddc0
commit ea15f09288
3 changed files with 48 additions and 52 deletions

View file

@ -320,15 +320,15 @@
if ($name === null) { if ($name === null) {
return true; return true;
} }
$parent = 'AppController';
if (strpos($name, '.') !== false) { if (strpos($name, '.') !== false) {
list($plugin, $name) = explode('.', $name); list($plugin, $name) = explode('.', $name);
$pluginAppController = Inflector::camelize($plugin . '_app_controller'); $parent = Inflector::camelize($plugin . '_app_controller');
$plugin = Inflector::underscore($plugin); $plugin = Inflector::underscore($plugin);
$pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php'; $pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php';
if (!class_exists($pluginAppController)) { if (!class_exists($parent)) {
if (file_exists($pluginAppControllerFile)) { if (file_exists($pluginAppControllerFile)) {
require($pluginAppControllerFile); require($pluginAppControllerFile);
} else { } else {
@ -346,6 +346,7 @@
} }
if (!class_exists($name . 'Controller')) { if (!class_exists($name . 'Controller')) {
$name = Inflector::underscore($name); $name = Inflector::underscore($name);
$file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $name . '_controller.php'; $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $name . '_controller.php';
@ -365,7 +366,8 @@
} }
$className = $name . 'Controller'; $className = $name . 'Controller';
if (class_exists($className)) {
if (class_exists($className) && get_parent_class($className) == $parent) {
return true; return true;
} else { } else {
$name = Inflector::underscore($className); $name = Inflector::underscore($className);
@ -1629,4 +1631,4 @@
} }
return $val2; return $val2;
} }
?> ?>

View file

@ -110,7 +110,7 @@ class Dispatcher extends Object {
$missingAction = $missingView = $privateAction = false; $missingAction = $missingView = $privateAction = false;
$this->base = $this->baseUrl(); $this->base = $this->baseUrl();
$controller = $this->__getController($this->params); $controller = $this->__getController();
if(!is_object($controller)) { if(!is_object($controller)) {
if (preg_match('/([\\.]+)/', $controller)) { if (preg_match('/([\\.]+)/', $controller)) {
@ -473,7 +473,7 @@ class Dispatcher extends Object {
$params = $this->params; $params = $this->params;
} }
$controller = $pluginPath = null; $pluginPath = $controller = $ctrlClass = null;
if (!empty($params['controller'])) { if (!empty($params['controller'])) {
$controller = Inflector::camelize($params['controller']); $controller = Inflector::camelize($params['controller']);
@ -484,23 +484,29 @@ class Dispatcher extends Object {
$this->plugin = $params['plugin']; $this->plugin = $params['plugin'];
$pluginPath = Inflector::camelize($this->plugin).'.'; $pluginPath = Inflector::camelize($this->plugin).'.';
if(!$controller) {
$controller = Inflector::camelize($params['plugin']);
$ctrlClass = $controller.'Controller';
}
} }
if ($pluginPath . $controller && loadController($pluginPath . $controller)) { if ($pluginPath . $controller && loadController($pluginPath . $controller)) {
$controller =& new $ctrlClass(); if(!class_exists($ctrlClass) && $this->plugin) {
} elseif ($continue){ $controller = Inflector::camelize($params['plugin']);
$this->params = $this->_restructureParams($params); $ctrlClass = $controller.'Controller';
$controller = $this->__getController($this->params, false); $params = am($this->params, array('plugin'=> $params['plugin'], 'controller'=> $params['plugin']));
}
if(class_exists($ctrlClass)) {
$controller =& new $ctrlClass();
}
} elseif ($continue == true){
$params = $this->_restructureParams($params);
$controller = $this->__getController($params, false);
return $controller;
} }
if(!isset($ctrlClass)) { if(!$ctrlClass) {
return false; return false;
} }
$this->params = $params;
return $controller; return $controller;
} }
} }
?> ?>

View file

@ -27,6 +27,7 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
require_once CAKE.'dispatcher.php'; require_once CAKE.'dispatcher.php';
require_once CAKE.'app_controller.php';
class TestDispatcher extends Dispatcher { class TestDispatcher extends Dispatcher {
@ -78,7 +79,7 @@ class MyPluginController extends MyPluginAppController {
} }
} }
class SomePagesController extends MyPluginAppController { class SomePagesController extends AppController {
var $name = 'SomePages'; var $name = 'SomePages';
var $uses = array(); var $uses = array();
@ -92,7 +93,21 @@ class SomePagesController extends MyPluginAppController {
} }
} }
class TestDispatchPagesController extends Controller { class OtherPagesController extends MyPluginAppController {
var $name = 'OtherPages';
var $uses = array();
function display($page = null) {
return $page;
}
function index() {
return true;
}
}
class TestDispatchPagesController extends AppController {
var $name = 'TestDispatchPages'; var $name = 'TestDispatchPages';
var $uses = array(); var $uses = array();
@ -488,17 +503,16 @@ class DispatcherTest extends UnitTestCase {
$dispatcher =& new TestDispatcher(); $dispatcher =& new TestDispatcher();
$dispatcher->base = false; $dispatcher->base = false;
$url = setUrl('/my_plugin/some_pages/index/param:value/param2:value2'); $url = setUrl('/my_plugin/other_pages/index/param:value/param2:value2');
restore_error_handler(); restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1)); @$controller = $dispatcher->dispatch($url, array('return'=> 1));
set_error_handler('simpleTestErrorHandler'); set_error_handler('simpleTestErrorHandler');
pr($dispatcher->params);
$expected = 'my_plugin'; $expected = 'my_plugin';
$this->assertIdentical($expected, $controller->plugin); $this->assertIdentical($expected, $controller->plugin);
$expected = 'SomePages'; $expected = 'OtherPages';
$this->assertIdentical($expected, $controller->name); $this->assertIdentical($expected, $controller->name);
$expected = 'index'; $expected = 'index';
@ -507,7 +521,7 @@ class DispatcherTest extends UnitTestCase {
$expected = array('param'=>'value', 'param2'=>'value2'); $expected = array('param'=>'value', 'param2'=>'value2');
$this->assertIdentical($expected, $controller->namedArgs); $this->assertIdentical($expected, $controller->namedArgs);
$expected = '/cake/repo/branches/1.2.x.x/my_plugin/some_pages/index/param:value/param2:value2'; $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
$this->assertIdentical($expected, $controller->here); $this->assertIdentical($expected, $controller->here);
$expected = '/cake/repo/branches/1.2.x.x'; $expected = '/cake/repo/branches/1.2.x.x';
@ -539,7 +553,7 @@ class DispatcherTest extends UnitTestCase {
$this->assertIdentical($expected, $controller->action); $this->assertIdentical($expected, $controller->action);
} }
function testAutomaticPluginPluginControllerDispatch() { function testAutomaticPluginControllerMissingActionDispatch() {
$_POST = array(); $_POST = array();
$_SERVER['DOCUMENT_ROOT'] = ''; $_SERVER['DOCUMENT_ROOT'] = '';
$_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php'; $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
@ -553,38 +567,12 @@ class DispatcherTest extends UnitTestCase {
@$controller = $dispatcher->dispatch($url, array('return'=> 1)); @$controller = $dispatcher->dispatch($url, array('return'=> 1));
set_error_handler('simpleTestErrorHandler'); set_error_handler('simpleTestErrorHandler');
$expected = 'my_plugin'; $expected = 'missingAction';
$this->assertIdentical($expected, $controller->plugin);
$expected = 'MyPlugin';
$this->assertIdentical($expected, $controller->name);
$expected = 'index';
$this->assertIdentical($expected, $controller->action);
}
function testRouterPluginNullControllerDispatch() {
$_POST = array();
$_SERVER['DOCUMENT_ROOT'] = '';
$_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
Router::reload();
Router::connect('/my_plugin/:controller/:action/*', array('plugin'=> 'my_plugin'));
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = setUrl('/my_plugin/param:value/param2:value2');
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
set_error_handler('simpleTestErrorHandler');
$expected = 'missingController';
$this->assertIdentical($expected, $controller); $this->assertIdentical($expected, $controller);
} }
function tearDown() { function tearDown() {
$_GET = $this->_get; $_GET = $this->_get;
} }
} }
?> ?>