From 8f82156a51c1ab0366313952df3423f1361f71bf Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 20 Nov 2010 13:11:26 -0500 Subject: [PATCH] Adding _mergeVars to Controller (this duplicated method will be re-factored away). Updating tests in ControllerMergeVarsTest to use PHPUnit assertions. --- cake/libs/controller/controller.php | 86 ++++++++++--------- .../controller/controller_merge_vars.test.php | 8 +- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 5e37c8dfb..db0dda8a3 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -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. * * @return void */ protected function __mergeVars() { - $pluginName = Inflector::camelize($this->plugin); - $pluginController = $pluginName . 'AppController'; + $pluginName = $pluginController = null; - 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'); $uses = $appVars['uses']; $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 (!in_array($plugin . $this->modelClass, $this->uses)) { @@ -445,20 +468,11 @@ class Controller extends Object { } elseif ($this->uses !== null || $this->uses !== false) { $merge[] = 'uses'; } - - foreach ($merge as $var) { - if (!empty($appVars[$var]) && is_array($this->{$var})) { - if ($var !== 'uses') { - $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} - ); - } + + $this->_mergeVars($merge, 'AppController'); + foreach ($merge as $prop) { + if ($prop !== 'components') { + $this->{$prop} = array_unique($this->{$prop}); } } } @@ -471,20 +485,10 @@ class Controller extends Object { if ($this->uses !== null || $this->uses !== false) { $merge[] = 'uses'; } - - foreach ($merge as $var) { - if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) { - if ($var !== 'uses') { - $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} - ); - } + $this->_mergeVars($merge, $pluginController); + foreach ($merge as $prop) { + if ($prop !== 'components') { + $this->{$prop} = array_unique($this->{$prop}); } } } diff --git a/cake/tests/cases/libs/controller/controller_merge_vars.test.php b/cake/tests/cases/libs/controller/controller_merge_vars.test.php index 3f16bd7cd..4784707d4 100644 --- a/cake/tests/cases/libs/controller/controller_merge_vars.test.php +++ b/cake/tests/cases/libs/controller/controller_merge_vars.test.php @@ -19,6 +19,8 @@ * @since CakePHP(tm) v 1.2.3 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::import('Core', 'Controller'); + if (!class_exists('AppController')) { /** @@ -220,13 +222,13 @@ class ControllerMergeVarsTest extends CakeTestCase { 'Auth' => array('setting' => 'val', 'otherVal'), 'Email' => array('ports' => 'open') ); - $this->assertEqual($Controller->components, $expected, 'Components are unexpected %s'); + $this->assertEquals($expected, $Controller->components, 'Components are unexpected.'); $expected = array( 'MergeVar' => array('format' => 'html', 'terse'), 'Javascript' => null ); - $this->assertEqual($Controller->helpers, $expected, 'Helpers are unexpected %s'); + $this->assertEquals($expected, $Controller->helpers, 'Helpers are unexpected.'); $Controller = new MergePostsController(); $Controller->components = array(); @@ -237,7 +239,7 @@ class ControllerMergeVarsTest extends CakeTestCase { 'MergeVar' => array('flag', 'otherFlag', 'redirect' => false), 'Auth' => array('setting' => 'val', 'otherVal'), ); - $this->assertEqual($Controller->components, $expected, 'Components are unexpected %s'); + $this->assertEqual($expected, $Controller->components, 'Components are unexpected.'); } /**