mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding _mergeVars to Controller (this duplicated method will be re-factored away).
Updating tests in ControllerMergeVarsTest to use PHPUnit assertions.
This commit is contained in:
parent
e2aeae3825
commit
8f82156a51
2 changed files with 50 additions and 44 deletions
|
@ -409,29 +409,52 @@ class Controller extends Object {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges this objects $property with the property in $class' definition.
|
||||||
|
* This classes value for the property will be merged on top of $class'
|
||||||
|
*
|
||||||
|
* This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
|
||||||
|
* this method as an empty function.
|
||||||
|
*
|
||||||
|
* @param array $properties The name of the properties to merge.
|
||||||
|
* @param sting $class The class to merge the property with.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function _mergeVars($properties, $class) {
|
||||||
|
$classProperties = get_class_vars($class);
|
||||||
|
foreach ($properties as $var) {
|
||||||
|
if (
|
||||||
|
isset($classProperties[$var]) &&
|
||||||
|
!empty($classProperties[$var]) &&
|
||||||
|
is_array($this->{$var}) &&
|
||||||
|
$this->{$var} != $classProperties[$var]
|
||||||
|
) {
|
||||||
|
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge components, helpers, and uses vars from AppController and PluginAppController.
|
* Merge components, helpers, and uses vars from AppController and PluginAppController.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function __mergeVars() {
|
protected function __mergeVars() {
|
||||||
$pluginName = Inflector::camelize($this->plugin);
|
$pluginName = $pluginController = null;
|
||||||
$pluginController = $pluginName . 'AppController';
|
|
||||||
|
|
||||||
if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
|
if (!empty($this->plugin)) {
|
||||||
|
$pluginName = Inflector::camelize($this->plugin);
|
||||||
|
$pluginController = $pluginName . 'AppController';
|
||||||
|
if (!is_subclass_of($this, $pluginController)) {
|
||||||
|
$pluginController = null;
|
||||||
|
}
|
||||||
|
$plugin = $pluginName . '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_subclass_of($this, 'AppController') || !empty($pluginController)) {
|
||||||
$appVars = get_class_vars('AppController');
|
$appVars = get_class_vars('AppController');
|
||||||
$uses = $appVars['uses'];
|
$uses = $appVars['uses'];
|
||||||
$merge = array('components', 'helpers');
|
$merge = array('components', 'helpers');
|
||||||
$plugin = null;
|
|
||||||
|
|
||||||
if (!empty($this->plugin)) {
|
|
||||||
$plugin = $pluginName . '.';
|
|
||||||
if (!is_subclass_of($this, $pluginController)) {
|
|
||||||
$pluginController = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$pluginController = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($uses == $this->uses && !empty($this->uses)) {
|
if ($uses == $this->uses && !empty($this->uses)) {
|
||||||
if (!in_array($plugin . $this->modelClass, $this->uses)) {
|
if (!in_array($plugin . $this->modelClass, $this->uses)) {
|
||||||
|
@ -445,20 +468,11 @@ class Controller extends Object {
|
||||||
} elseif ($this->uses !== null || $this->uses !== false) {
|
} elseif ($this->uses !== null || $this->uses !== false) {
|
||||||
$merge[] = 'uses';
|
$merge[] = 'uses';
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($merge as $var) {
|
$this->_mergeVars($merge, 'AppController');
|
||||||
if (!empty($appVars[$var]) && is_array($this->{$var})) {
|
foreach ($merge as $prop) {
|
||||||
if ($var !== 'uses') {
|
if ($prop !== 'components') {
|
||||||
$normal = Set::normalize($this->{$var});
|
$this->{$prop} = array_unique($this->{$prop});
|
||||||
$app = Set::normalize($appVars[$var]);
|
|
||||||
if ($app !== $normal) {
|
|
||||||
$this->{$var} = Set::merge($app, $normal);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->{$var} = Set::merge(
|
|
||||||
array_diff($appVars[$var], $this->{$var}), $this->{$var}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -471,20 +485,10 @@ class Controller extends Object {
|
||||||
if ($this->uses !== null || $this->uses !== false) {
|
if ($this->uses !== null || $this->uses !== false) {
|
||||||
$merge[] = 'uses';
|
$merge[] = 'uses';
|
||||||
}
|
}
|
||||||
|
$this->_mergeVars($merge, $pluginController);
|
||||||
foreach ($merge as $var) {
|
foreach ($merge as $prop) {
|
||||||
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
if ($prop !== 'components') {
|
||||||
if ($var !== 'uses') {
|
$this->{$prop} = array_unique($this->{$prop});
|
||||||
$normal = Set::normalize($this->{$var});
|
|
||||||
$app = Set::normalize($appVars[$var]);
|
|
||||||
if ($app !== $normal) {
|
|
||||||
$this->{$var} = Set::merge($app, $normal);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->{$var} = Set::merge(
|
|
||||||
array_diff($appVars[$var], $this->{$var}), $this->{$var}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
* @since CakePHP(tm) v 1.2.3
|
* @since CakePHP(tm) v 1.2.3
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
|
App::import('Core', 'Controller');
|
||||||
|
|
||||||
if (!class_exists('AppController')) {
|
if (!class_exists('AppController')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -220,13 +222,13 @@ class ControllerMergeVarsTest extends CakeTestCase {
|
||||||
'Auth' => array('setting' => 'val', 'otherVal'),
|
'Auth' => array('setting' => 'val', 'otherVal'),
|
||||||
'Email' => array('ports' => 'open')
|
'Email' => array('ports' => 'open')
|
||||||
);
|
);
|
||||||
$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
|
$this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
|
||||||
|
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'MergeVar' => array('format' => 'html', 'terse'),
|
'MergeVar' => array('format' => 'html', 'terse'),
|
||||||
'Javascript' => null
|
'Javascript' => null
|
||||||
);
|
);
|
||||||
$this->assertEqual($Controller->helpers, $expected, 'Helpers are unexpected %s');
|
$this->assertEquals($expected, $Controller->helpers, 'Helpers are unexpected.');
|
||||||
|
|
||||||
$Controller = new MergePostsController();
|
$Controller = new MergePostsController();
|
||||||
$Controller->components = array();
|
$Controller->components = array();
|
||||||
|
@ -237,7 +239,7 @@ class ControllerMergeVarsTest extends CakeTestCase {
|
||||||
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
|
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
|
||||||
'Auth' => array('setting' => 'val', 'otherVal'),
|
'Auth' => array('setting' => 'val', 'otherVal'),
|
||||||
);
|
);
|
||||||
$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
|
$this->assertEqual($expected, $Controller->components, 'Components are unexpected.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue