2010-07-04 04:43:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BehaviorCollection
|
|
|
|
*
|
|
|
|
* Provides managment and interface for interacting with collections of behaviors.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model
|
2010-07-04 04:43:39 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::import('Core', 'ObjectCollection');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model behavior collection class.
|
|
|
|
*
|
|
|
|
* Defines the Behavior interface, and contains common model interaction functionality.
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model
|
2010-07-04 04:43:39 +00:00
|
|
|
*/
|
|
|
|
class BehaviorCollection extends ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a reference to the attached name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $modelName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keeps a list of all methods of attached behaviors
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-12-26 22:10:16 +00:00
|
|
|
protected $_methods = array();
|
2010-07-04 04:43:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keeps a list of all methods which have been mapped with regular expressions
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-12-26 22:10:16 +00:00
|
|
|
protected $_mappedMethods = array();
|
2010-07-04 04:43:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches a model object and loads a list of behaviors
|
|
|
|
*
|
|
|
|
* @todo Make this method a constructor instead..
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function init($modelName, $behaviors = array()) {
|
|
|
|
$this->modelName = $modelName;
|
|
|
|
|
|
|
|
if (!empty($behaviors)) {
|
2010-11-21 19:42:13 +00:00
|
|
|
foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
|
|
|
|
$this->load($config['class'], $config['settings']);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backwards compatible alias for load()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @deprecated Replaced with load()
|
|
|
|
*/
|
|
|
|
public function attach($behavior, $config = array()) {
|
|
|
|
return $this->load($behavior, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-07 04:07:27 +00:00
|
|
|
* Loads a behavior into the collection. You can use use `$config['enabled'] = false`
|
|
|
|
* to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
|
|
|
|
* can still be used as normal.
|
2010-07-04 04:43:39 +00:00
|
|
|
*
|
2011-01-14 02:06:32 +00:00
|
|
|
* You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
|
2011-01-10 02:28:05 +00:00
|
|
|
* {{{
|
|
|
|
* public $actsAs = array(
|
2011-01-14 02:06:32 +00:00
|
|
|
* 'Tree' => array(
|
|
|
|
* 'className' => 'AliasedTree'
|
2011-01-10 02:28:05 +00:00
|
|
|
* );
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* All calls to the `Tree` behavior would use `AliasedTree` instead.
|
|
|
|
*
|
2010-07-04 04:43:39 +00:00
|
|
|
* @param string $behavior CamelCased name of the behavior to load
|
|
|
|
* @param array $config Behavior configuration parameters
|
|
|
|
* @return boolean True on success, false on failure
|
|
|
|
* @throws MissingBehaviorFileException or MissingBehaviorClassException when a behavior could not be found.
|
|
|
|
*/
|
2010-11-07 03:48:27 +00:00
|
|
|
public function load($behavior, $config = array()) {
|
2011-01-14 02:04:06 +00:00
|
|
|
if (isset($config['className'])) {
|
2011-01-15 01:44:33 +00:00
|
|
|
$alias = $behavior;
|
|
|
|
$behavior = $config['className'];
|
2011-01-10 02:28:05 +00:00
|
|
|
}
|
2011-01-15 01:44:33 +00:00
|
|
|
list($plugin, $name) = pluginSplit($behavior);
|
|
|
|
if (!isset($alias)) {
|
|
|
|
$alias = $name;
|
|
|
|
}
|
|
|
|
|
2011-01-14 02:04:06 +00:00
|
|
|
$class = $name . 'Behavior';
|
2010-07-04 04:43:39 +00:00
|
|
|
|
|
|
|
if (!App::import('Behavior', $behavior)) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingBehaviorFileException(array(
|
|
|
|
'file' => Inflector::underscore($behavior) . '.php',
|
|
|
|
'class' => $class
|
|
|
|
));
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
if (!class_exists($class)) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingBehaviorClassException(array(
|
|
|
|
'file' => Inflector::underscore($behavior) . '.php',
|
|
|
|
'class' => $class
|
|
|
|
));
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 02:28:05 +00:00
|
|
|
if (!isset($this->{$alias})) {
|
2010-07-04 04:43:39 +00:00
|
|
|
if (ClassRegistry::isKeySet($class)) {
|
2011-01-10 02:28:05 +00:00
|
|
|
$this->_loaded[$alias] = ClassRegistry::getObject($class);
|
2010-07-04 04:43:39 +00:00
|
|
|
} else {
|
2011-01-10 02:28:05 +00:00
|
|
|
$this->_loaded[$alias] = new $class();
|
|
|
|
ClassRegistry::addObject($class, $this->_loaded[$alias]);
|
2010-07-04 04:43:39 +00:00
|
|
|
if (!empty($plugin)) {
|
2011-01-10 02:28:05 +00:00
|
|
|
ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-10 02:28:05 +00:00
|
|
|
} elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
|
2010-07-04 04:43:39 +00:00
|
|
|
if ($config !== null && $config !== false) {
|
2011-01-10 02:28:05 +00:00
|
|
|
$config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
|
2010-07-04 04:43:39 +00:00
|
|
|
} else {
|
|
|
|
$config = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($config)) {
|
|
|
|
$config = array();
|
|
|
|
}
|
2011-01-10 02:28:05 +00:00
|
|
|
$this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
|
2010-07-04 04:43:39 +00:00
|
|
|
|
2011-01-10 02:28:05 +00:00
|
|
|
foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
|
|
|
|
$this->_mappedMethods[$method] = array($alias, $methodAlias);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
2011-01-10 02:28:05 +00:00
|
|
|
$methods = get_class_methods($this->_loaded[$alias]);
|
2010-07-04 04:43:39 +00:00
|
|
|
$parentMethods = array_flip(get_class_methods('ModelBehavior'));
|
|
|
|
$callbacks = array(
|
|
|
|
'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
|
|
|
|
'beforeDelete', 'afterDelete', 'afterError'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($methods as $m) {
|
|
|
|
if (!isset($parentMethods[$m])) {
|
|
|
|
$methodAllowed = (
|
2010-12-26 22:10:16 +00:00
|
|
|
$m[0] != '_' && !array_key_exists($m, $this->_methods) &&
|
2010-07-04 04:43:39 +00:00
|
|
|
!in_array($m, $callbacks)
|
|
|
|
);
|
|
|
|
if ($methodAllowed) {
|
2011-01-10 02:28:05 +00:00
|
|
|
$this->_methods[$m] = array($alias, $m);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-07 04:07:27 +00:00
|
|
|
$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
|
2011-01-10 02:28:05 +00:00
|
|
|
if (!in_array($alias, $this->_enabled) && !$configDisabled) {
|
|
|
|
$this->enable($alias);
|
2010-07-04 05:09:39 +00:00
|
|
|
} elseif ($configDisabled) {
|
2011-01-10 02:28:05 +00:00
|
|
|
$this->disable($alias);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detaches a behavior from a model
|
|
|
|
*
|
|
|
|
* @param string $name CamelCased name of the behavior to unload
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unload($name) {
|
|
|
|
list($plugin, $name) = pluginSplit($name);
|
|
|
|
if (isset($this->_loaded[$name])) {
|
|
|
|
$this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
|
|
|
|
unset($this->_loaded[$name]);
|
|
|
|
}
|
2010-12-26 22:10:16 +00:00
|
|
|
foreach ($this->_methods as $m => $callback) {
|
2010-12-26 22:09:20 +00:00
|
|
|
if (is_array($callback) && $callback[0] == $name) {
|
2010-12-26 22:10:16 +00:00
|
|
|
unset($this->_methods[$m]);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-04 05:09:39 +00:00
|
|
|
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backwards compatible alias for unload()
|
|
|
|
*
|
|
|
|
* @param string $name Name of behavior
|
|
|
|
* @return void
|
|
|
|
* @deprecated Use unload instead.
|
|
|
|
*/
|
|
|
|
public function detach($name) {
|
|
|
|
return $this->unload($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-26 22:21:49 +00:00
|
|
|
* Dispatches a behavior method. Will call either normal methods or mapped methods.
|
2010-07-04 04:43:39 +00:00
|
|
|
*
|
2010-12-26 22:21:49 +00:00
|
|
|
* If a method is not handeled by the BehaviorCollection, and $strict is false, a
|
|
|
|
* special return of `array('unhandled')` will be returned to signal the method was not found.
|
|
|
|
*
|
|
|
|
* @param Model $model The model the method was originally called on.
|
|
|
|
* @param string $method The method called.
|
|
|
|
* @param array $params Parameters for the called method.
|
|
|
|
* @param boolean $strict If methods are not found, trigger an error.
|
2010-07-04 04:43:39 +00:00
|
|
|
* @return array All methods for all behaviors attached to this object
|
|
|
|
*/
|
2010-12-26 22:09:20 +00:00
|
|
|
public function dispatchMethod($model, $method, $params = array(), $strict = false) {
|
|
|
|
$method = $this->hasMethod($method, true);
|
|
|
|
|
|
|
|
if ($strict && empty($method)) {
|
2010-12-05 01:37:13 +00:00
|
|
|
trigger_error(__("BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $method), E_USER_WARNING);
|
2010-07-04 04:43:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2010-12-26 22:09:20 +00:00
|
|
|
if (empty($method)) {
|
|
|
|
return array('unhandled');
|
|
|
|
}
|
|
|
|
if (count($method) === 3) {
|
|
|
|
array_unshift($params, $method[2]);
|
|
|
|
unset($method[2]);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
2010-12-26 22:09:20 +00:00
|
|
|
return call_user_func_array(
|
|
|
|
array($this->_loaded[$method[0]], $method[1]),
|
|
|
|
array_merge(array(&$model), $params)
|
|
|
|
);
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-26 22:09:20 +00:00
|
|
|
* Gets the method list for attached behaviors, i.e. all public, non-callback methods.
|
|
|
|
* This does not include mappedMethods.
|
2010-07-04 04:43:39 +00:00
|
|
|
*
|
|
|
|
* @return array All public methods for all behaviors attached to this collection
|
|
|
|
*/
|
|
|
|
public function methods() {
|
2010-12-26 22:10:16 +00:00
|
|
|
return $this->_methods;
|
2010-07-04 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
2010-12-26 19:25:57 +00:00
|
|
|
/**
|
|
|
|
* Check to see if a behavior in this collection implements the provided method. Will
|
|
|
|
* also check mappedMethods.
|
|
|
|
*
|
|
|
|
* @param string $method The method to find.
|
2010-12-26 22:09:20 +00:00
|
|
|
* @param boolean $callback Return the callback for the method.
|
|
|
|
* @return mixed If $callback is false, a boolean will be returnned, if its true, an array
|
|
|
|
* containing callback information will be returnned. For mapped methods the array will have 3 elements.
|
2010-12-26 19:25:57 +00:00
|
|
|
*/
|
2010-12-26 22:09:20 +00:00
|
|
|
public function hasMethod($method, $callback = false) {
|
2010-12-26 22:10:16 +00:00
|
|
|
if (isset($this->_methods[$method])) {
|
|
|
|
return $callback ? $this->_methods[$method] : true;
|
2010-12-26 19:25:57 +00:00
|
|
|
}
|
2010-12-26 22:10:16 +00:00
|
|
|
foreach ($this->_mappedMethods as $pattern => $target) {
|
2010-12-26 19:25:57 +00:00
|
|
|
if (preg_match($pattern . 'i', $method)) {
|
2010-12-26 22:09:20 +00:00
|
|
|
if ($callback) {
|
|
|
|
$target[] = $method;
|
|
|
|
return $target;
|
|
|
|
}
|
2010-12-26 19:25:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-12 20:03:23 +00:00
|
|
|
}
|