Skip to content

Commit

Permalink
Allowed behaviors to be aliased by setting the 'alias' key
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jan 10, 2011
1 parent 9749dc8 commit 24d90c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
44 changes: 29 additions & 15 deletions cake/libs/model/behavior_collection.php
Expand Up @@ -83,6 +83,16 @@ public function attach($behavior, $config = array()) {
* to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
* can still be used as normal.
*
* You can alias your behavior as an existing behavior by setting the 'alias' key, i.e.,
* {{{
* public $actsAs = array(
* 'AliasedTree' => array(
* 'alias' => 'Tree'
* );
* );
* }}}
* All calls to the `Tree` behavior would use `AliasedTree` instead.
*
* @param string $behavior CamelCased name of the behavior to load
* @param array $config Behavior configuration parameters
* @return boolean True on success, false on failure
Expand All @@ -91,6 +101,10 @@ public function attach($behavior, $config = array()) {
public function load($behavior, $config = array()) {
list($plugin, $name) = pluginSplit($behavior);
$class = $name . 'Behavior';
$alias = $name;
if (isset($config['alias'])) {
$alias = $config['alias'];
}

if (!App::import('Behavior', $behavior)) {
throw new MissingBehaviorFileException(array(
Expand All @@ -105,32 +119,32 @@ public function load($behavior, $config = array()) {
));
}

if (!isset($this->{$name})) {
if (!isset($this->{$alias})) {
if (ClassRegistry::isKeySet($class)) {
$this->_loaded[$name] = ClassRegistry::getObject($class);
$this->_loaded[$alias] = ClassRegistry::getObject($class);
} else {
$this->_loaded[$name] = new $class();
ClassRegistry::addObject($class, $this->_loaded[$name]);
$this->_loaded[$alias] = new $class();
ClassRegistry::addObject($class, $this->_loaded[$alias]);
if (!empty($plugin)) {
ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$name]);
ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
}
}
} elseif (isset($this->_loaded[$name]->settings) && isset($this->_loaded[$name]->settings[$this->modelName])) {
} elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
if ($config !== null && $config !== false) {
$config = array_merge($this->_loaded[$name]->settings[$this->modelName], $config);
$config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
} else {
$config = array();
}
}
if (empty($config)) {
$config = array();
}
$this->_loaded[$name]->setup(ClassRegistry::getObject($this->modelName), $config);
$this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);

foreach ($this->_loaded[$name]->mapMethods as $method => $alias) {
$this->_mappedMethods[$method] = array($name, $alias);
foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
$this->_mappedMethods[$method] = array($alias, $methodAlias);
}
$methods = get_class_methods($this->_loaded[$name]);
$methods = get_class_methods($this->_loaded[$alias]);
$parentMethods = array_flip(get_class_methods('ModelBehavior'));
$callbacks = array(
'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
Expand All @@ -144,16 +158,16 @@ public function load($behavior, $config = array()) {
!in_array($m, $callbacks)
);
if ($methodAllowed) {
$this->_methods[$m] = array($name, $m);
$this->_methods[$m] = array($alias, $m);
}
}
}

$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
if (!in_array($name, $this->_enabled) && !$configDisabled) {
$this->enable($name);
if (!in_array($alias, $this->_enabled) && !$configDisabled) {
$this->enable($alias);
} elseif ($configDisabled) {
$this->disable($name);
$this->disable($alias);
}
return true;
}
Expand Down
23 changes: 23 additions & 0 deletions cake/tests/cases/libs/model/behavior_collection.test.php
Expand Up @@ -412,6 +412,12 @@ function setup($model, $config = null) {
}
}

/**
* Extended TestBehavior
*/
class TestAliasBehavior extends TestBehavior {
}

/**
* BehaviorCollection class
*
Expand All @@ -430,6 +436,23 @@ class BehaviorCollectionTest extends CakeTestCase {
'core.attachment', 'core.tag', 'core.articles_tag'
);

/**
* Tests loading aliased behaviors
*/
function testLoadAlias() {
$Apple = new Apple();
$this->assertIdentical($Apple->Behaviors->attached(), array());

$Apple->Behaviors->load('TestAlias', array('alias' => 'Test', 'somesetting' => true));
$this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
$this->assertInstanceOf('TestAliasBehavior', $Apple->Behaviors->Test);
$this->assertTrue($Apple->Behaviors->Test->settings['Apple']['somesetting']);

$this->assertEquals($Apple->Behaviors->Test->testMethod($Apple, true), 'working');
$this->assertEquals($Apple->testMethod(true), 'working');
$this->assertEquals($Apple->Behaviors->dispatchMethod($Apple, 'testMethod'), 'working');
}

/**
* testBehaviorBinding method
*
Expand Down

0 comments on commit 24d90c1

Please sign in to comment.