"Fixes #3768, Controller loads wrong model when plugins controller has the controller name of a main model.

moved merging of components, helpers, and uses vars to Controller::_mergeVars(); fixes bug with PluginAppController vars not being merged before components are loaded.
  Added call to Controller::_mergeVars(); in Dispatcher::dispatch();"

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6291 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-12-31 23:27:14 +00:00
parent 6a2a24cfc9
commit 013133457c
2 changed files with 51 additions and 39 deletions

View file

@ -219,6 +219,7 @@ class Dispatcher extends Object {
}
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot)));
$controller->_mergeVars();
$controller->_initComponents();
$controller->constructClasses();

View file

@ -294,24 +294,8 @@ class Controller extends Object {
if ($this->viewPath == null) {
$this->viewPath = Inflector::underscore($this->name);
}
$this->modelClass = Inflector::classify($this->name);
$this->modelKey = Inflector::underscore($this->modelClass);
if (is_subclass_of($this, 'AppController') || is_subclass_of($this, Inflector::camelize($this->plugin) . 'AppController')) {
$appVars = get_class_vars('AppController');
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
if ($uses == $this->uses && !empty($this->uses)) {
array_unshift($this->uses, $this->modelClass);
} elseif ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
$this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
parent::__construct();
}
/**
@ -324,6 +308,56 @@ class Controller extends Object {
$component = new Component();
$component->init($this);
}
/**
* Merge components, helpers, and uses vars from AppController and PluginAppController
*
* @access protected
*/
function _mergeVars () {
$pluginController = Inflector::camelize($this->plugin) . 'AppController';
if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
$appVars = get_class_vars('AppController');
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
$plugin = null;
if (isset($this->plugin)) {
$plugin = $this->plugin . '.';
if (!is_subclass_of($this, $pluginController)) {
$pluginController = null;
}
}
if ($uses == $this->uses && !empty($this->uses)) {
array_unshift($this->uses, $plugin . $this->modelClass);
} elseif ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
$this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
if ($pluginController) {
$appVars = get_class_vars($pluginController);
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
if ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
$this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
}
/**
* Loads Model classes based on the the uses property
* see Controller::loadModel(); for more info
@ -333,29 +367,6 @@ class Controller extends Object {
* @see Controller::loadModel()
*/
function constructClasses() {
if(isset($this->plugin)) {
if(isset($this->uses[0]) && $this->uses[0] === $this->modelClass) {
$this->uses[0] = Inflector::camelize($this->plugin) . '.' . $this->modelClass;
}
$appController = Inflector::camelize($this->plugin) . 'AppController';
if (is_subclass_of($this, $appController)) {
$appVars = get_class_vars($appController);
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
if ($uses == $this->uses && !empty($this->uses)) {
array_unshift($this->uses, $this->modelClass);
} elseif ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
$this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
}
if ($this->uses === null || ($this->uses === array())) {
return false;
}