Skip to content
Permalink
Browse files

Adding _mergeVars to Controller (this duplicated method will be re-fa…

…ctored away).

Updating tests in ControllerMergeVarsTest to use PHPUnit assertions.
  • Loading branch information...
markstory committed Nov 20, 2010
1 parent e2aeae3 commit 8f82156a51c1ab0366313952df3423f1361f71bf
@@ -409,29 +409,52 @@ protected function _setRequest(CakeRequest $request) {
}
}
/**
* 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 @@ protected function __mergeVars() {
} 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 @@ protected function __mergeVars() {
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});
}
}
}
@@ -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 @@ function testMergeVarsWithPlugin() {
'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 @@ function testMergeVarsWithPlugin() {
'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.');
}
/**

0 comments on commit 8f82156

Please sign in to comment.
You can’t perform that action at this time.