From 24d90c17d6708c4b5d5afb4c4d77eddc8d9fcddf Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sun, 9 Jan 2011 18:28:05 -0800 Subject: [PATCH] Allowed behaviors to be aliased by setting the 'alias' key --- cake/libs/model/behavior_collection.php | 44 ++++++++++++------- .../libs/model/behavior_collection.test.php | 23 ++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/cake/libs/model/behavior_collection.php b/cake/libs/model/behavior_collection.php index 965c315f4b0..6e0d8e56fcb 100644 --- a/cake/libs/model/behavior_collection.php +++ b/cake/libs/model/behavior_collection.php @@ -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 @@ -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( @@ -105,19 +119,19 @@ 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(); } @@ -125,12 +139,12 @@ public function load($behavior, $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', @@ -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; } diff --git a/cake/tests/cases/libs/model/behavior_collection.test.php b/cake/tests/cases/libs/model/behavior_collection.test.php index 6fa7756b793..6f22d335c90 100644 --- a/cake/tests/cases/libs/model/behavior_collection.test.php +++ b/cake/tests/cases/libs/model/behavior_collection.test.php @@ -412,6 +412,12 @@ function setup($model, $config = null) { } } +/** + * Extended TestBehavior + */ +class TestAliasBehavior extends TestBehavior { +} + /** * BehaviorCollection class * @@ -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 *