Skip to content

Commit

Permalink
Adding a normalize parameter to Object::_mergeVars(). This allows exi…
Browse files Browse the repository at this point in the history
…sting behavior to be maintained.

Updating Controller and Shell usage to match new parameters.
  • Loading branch information
markstory committed Nov 21, 2010
1 parent 8821bec commit 8a129ec
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
10 changes: 4 additions & 6 deletions cake/console/shells/shell.php
Expand Up @@ -164,15 +164,13 @@ function __construct($stdout = null, $stderr = null, $stdin = null) {
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}
$merge = array();

$parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) {
$merge[] = 'tasks';
$this->_mergeVars(array('tasks'), $parent, true);
}
if ($this->uses !== null && $this->uses !== false) {
$merge[] = 'uses';
}
if (!empty($merge)) {
$this->_mergeVars($merge, get_parent_class($this));
$this->_mergeVars(array('uses'), $parent, false);
}
}

Expand Down
19 changes: 3 additions & 16 deletions cake/libs/controller/controller.php
Expand Up @@ -441,31 +441,18 @@ protected function __mergeVars() {
array_unshift($this->uses, $plugin . $this->modelClass);
}
} elseif ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}

$this->_mergeVars($merge, 'AppController');
foreach ($merge as $prop) {
if ($prop !== 'components') {
$this->{$prop} = array_unique((array)$this->{$prop});
}
$this->_mergeVars(array('uses'), 'AppController', false);
}
$this->_mergeVars($merge, 'AppController', true);
}

if ($pluginController && $pluginName != null) {
$appVars = get_class_vars($pluginController);
$uses = $appVars['uses'];
$merge = array('components', 'helpers');

if ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
$this->_mergeVars(array('uses'), $pluginController, false);
}
$this->_mergeVars($merge, $pluginController);
foreach ($merge as $prop) {
if ($prop !== 'components') {
$this->{$prop} = array_unique((array)$this->{$prop});
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion cake/libs/object.php
Expand Up @@ -207,9 +207,10 @@ function _persist($name, $return = null, &$object, $type = null) {
*
* @param array $properties The name of the properties to merge.
* @param sting $class The class to merge the property with.
* @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
* @return void
*/
protected function _mergeVars($properties, $class) {
protected function _mergeVars($properties, $class, $normalize = true) {
$classProperties = get_class_vars($class);
foreach ($properties as $var) {
if (
Expand All @@ -218,6 +219,10 @@ protected function _mergeVars($properties, $class) {
is_array($this->{$var}) &&
$this->{$var} != $classProperties[$var]
) {
if ($normalize) {
$classProperties[$var] = Set::normalize($classProperties[$var]);
$this->{$var} = Set::normalize($this->{$var});
}
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
}
}
Expand Down
11 changes: 7 additions & 4 deletions cake/tests/cases/console/shells/shell.test.php
Expand Up @@ -70,8 +70,8 @@ protected function no_access() {

}

public function mergeVars($properties, $class) {
return $this->_mergeVars($properties, $class);
public function mergeVars($properties, $class, $normalize = true) {
return $this->_mergeVars($properties, $class, $normalize);
}
}

Expand Down Expand Up @@ -156,9 +156,12 @@ public function testConstruct() {
function testMergeVars() {
$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
$this->Shell->uses = array('Posts');
$this->Shell->mergeVars(array('tasks', 'uses'), 'TestMergeShell');
$this->Shell->mergeVars(array('tasks'), 'TestMergeShell');
$this->Shell->mergeVars(array('uses'), 'TestMergeShell', false);

$expected = array('DbConfig' => null, 'Fixture' => null, 'DbConfig' => array('one', 'two'));
$this->assertEquals($expected, $this->Shell->tasks);

$this->assertEquals(array('DbConfig', 'Fixture', 'DbConfig' => array('one', 'two')), $this->Shell->tasks);
$expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
$this->assertEquals($expected, Set::normalize($this->Shell->tasks), 'Normalized results are wrong.');
$this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
Expand Down
Expand Up @@ -201,7 +201,7 @@ function testHelperOrderPrecedence() {
'Custom' => null,
'Foo' => array('something')
);
$this->assertIdentical($Controller->helpers, $expected, 'Order is incorrect. %s');
$this->assertSame($Controller->helpers, $expected, 'Order is incorrect.');
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ function testMergeVarsWithPlugin() {
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
'Auth' => array('setting' => 'val', 'otherVal'),
);
$this->assertEqual($expected, $Controller->components, 'Components are unexpected.');
$this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
}

/**
Expand Down

0 comments on commit 8a129ec

Please sign in to comment.