mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Allowed behaviors to be aliased by setting the 'alias' key
This commit is contained in:
parent
9749dc808d
commit
24d90c17d6
2 changed files with 52 additions and 15 deletions
|
@ -83,6 +83,16 @@ class BehaviorCollection extends ObjectCollection {
|
|||
* 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 @@ class BehaviorCollection extends ObjectCollection {
|
|||
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 @@ class BehaviorCollection extends ObjectCollection {
|
|||
));
|
||||
}
|
||||
|
||||
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 @@ class BehaviorCollection extends ObjectCollection {
|
|||
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 @@ class BehaviorCollection extends ObjectCollection {
|
|||
!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;
|
||||
}
|
||||
|
|
|
@ -412,6 +412,12 @@ class Test7Behavior extends ModelBehavior{
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue