BehaviorCollection.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * BehaviorCollection
  4. *
  5. * Provides managment and interface for interacting with collections of behaviors.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Model
  18. * @since CakePHP(tm) v 1.2.0.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('ObjectCollection', 'Utility');
  22. /**
  23. * Model behavior collection class.
  24. *
  25. * Defines the Behavior interface, and contains common model interaction functionality.
  26. *
  27. * @package Cake.Model
  28. */
  29. class BehaviorCollection extends ObjectCollection {
  30. /**
  31. * Stores a reference to the attached name
  32. *
  33. * @var string
  34. * @access public
  35. */
  36. public $modelName = null;
  37. /**
  38. * Keeps a list of all methods of attached behaviors
  39. *
  40. * @var array
  41. */
  42. protected $_methods = array();
  43. /**
  44. * Keeps a list of all methods which have been mapped with regular expressions
  45. *
  46. * @var array
  47. */
  48. protected $_mappedMethods = array();
  49. /**
  50. * Attaches a model object and loads a list of behaviors
  51. *
  52. * @todo Make this method a constructor instead..
  53. * @param string $modelName
  54. * @param array $behaviors
  55. * @return void
  56. */
  57. public function init($modelName, $behaviors = array()) {
  58. $this->modelName = $modelName;
  59. if (!empty($behaviors)) {
  60. foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
  61. $this->load($config['class'], $config['settings']);
  62. }
  63. }
  64. }
  65. /**
  66. * Backwards compatible alias for load()
  67. *
  68. * @param string $behavior
  69. * @param array $config
  70. * @return void
  71. * @deprecated Replaced with load()
  72. */
  73. public function attach($behavior, $config = array()) {
  74. return $this->load($behavior, $config);
  75. }
  76. /**
  77. * Loads a behavior into the collection. You can use use `$config['enabled'] = false`
  78. * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
  79. * can still be used as normal.
  80. *
  81. * You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
  82. * {{{
  83. * public $actsAs = array(
  84. * 'Tree' => array(
  85. * 'className' => 'AliasedTree'
  86. * );
  87. * );
  88. * }}}
  89. * All calls to the `Tree` behavior would use `AliasedTree` instead.
  90. *
  91. * @param string $behavior CamelCased name of the behavior to load
  92. * @param array $config Behavior configuration parameters
  93. * @return boolean True on success, false on failure
  94. * @throws MissingBehaviorClassException when a behavior could not be found.
  95. */
  96. public function load($behavior, $config = array()) {
  97. if (is_array($config) && isset($config['className'])) {
  98. $alias = $behavior;
  99. $behavior = $config['className'];
  100. }
  101. list($plugin, $name) = pluginSplit($behavior, true);
  102. if (!isset($alias)) {
  103. $alias = $name;
  104. }
  105. $class = $name . 'Behavior';
  106. App::uses($class, $plugin . 'Model/Behavior');
  107. if (!class_exists($class)) {
  108. throw new MissingBehaviorClassException(array(
  109. 'file' => Inflector::underscore($behavior) . '.php',
  110. 'class' => $class
  111. ));
  112. }
  113. if (!isset($this->{$alias})) {
  114. if (ClassRegistry::isKeySet($class)) {
  115. $this->_loaded[$alias] = ClassRegistry::getObject($class);
  116. } else {
  117. $this->_loaded[$alias] = new $class();
  118. ClassRegistry::addObject($class, $this->_loaded[$alias]);
  119. if (!empty($plugin)) {
  120. ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
  121. }
  122. }
  123. } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
  124. if ($config !== null && $config !== false) {
  125. $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
  126. } else {
  127. $config = array();
  128. }
  129. }
  130. if (empty($config)) {
  131. $config = array();
  132. }
  133. $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
  134. foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
  135. $this->_mappedMethods[$method] = array($alias, $methodAlias);
  136. }
  137. $methods = get_class_methods($this->_loaded[$alias]);
  138. $parentMethods = array_flip(get_class_methods('ModelBehavior'));
  139. $callbacks = array(
  140. 'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
  141. 'beforeDelete', 'afterDelete', 'onError'
  142. );
  143. foreach ($methods as $m) {
  144. if (!isset($parentMethods[$m])) {
  145. $methodAllowed = (
  146. $m[0] != '_' && !array_key_exists($m, $this->_methods) &&
  147. !in_array($m, $callbacks)
  148. );
  149. if ($methodAllowed) {
  150. $this->_methods[$m] = array($alias, $m);
  151. }
  152. }
  153. }
  154. $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
  155. if (!in_array($alias, $this->_enabled) && !$configDisabled) {
  156. $this->enable($alias);
  157. } elseif ($configDisabled) {
  158. $this->disable($alias);
  159. }
  160. return true;
  161. }
  162. /**
  163. * Detaches a behavior from a model
  164. *
  165. * @param string $name CamelCased name of the behavior to unload
  166. * @return void
  167. */
  168. public function unload($name) {
  169. list($plugin, $name) = pluginSplit($name);
  170. if (isset($this->_loaded[$name])) {
  171. $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
  172. unset($this->_loaded[$name]);
  173. }
  174. foreach ($this->_methods as $m => $callback) {
  175. if (is_array($callback) && $callback[0] == $name) {
  176. unset($this->_methods[$m]);
  177. }
  178. }
  179. $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
  180. }
  181. /**
  182. * Backwards compatible alias for unload()
  183. *
  184. * @param string $name Name of behavior
  185. * @return void
  186. * @deprecated Use unload instead.
  187. */
  188. public function detach($name) {
  189. return $this->unload($name);
  190. }
  191. /**
  192. * Dispatches a behavior method. Will call either normal methods or mapped methods.
  193. *
  194. * If a method is not handeled by the BehaviorCollection, and $strict is false, a
  195. * special return of `array('unhandled')` will be returned to signal the method was not found.
  196. *
  197. * @param Model $model The model the method was originally called on.
  198. * @param string $method The method called.
  199. * @param array $params Parameters for the called method.
  200. * @param boolean $strict If methods are not found, trigger an error.
  201. * @return array All methods for all behaviors attached to this object
  202. */
  203. public function dispatchMethod($model, $method, $params = array(), $strict = false) {
  204. $method = $this->hasMethod($method, true);
  205. if ($strict && empty($method)) {
  206. trigger_error(__d('cake_dev', "BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $method), E_USER_WARNING);
  207. return null;
  208. }
  209. if (empty($method)) {
  210. return array('unhandled');
  211. }
  212. if (count($method) === 3) {
  213. array_unshift($params, $method[2]);
  214. unset($method[2]);
  215. }
  216. return call_user_func_array(
  217. array($this->_loaded[$method[0]], $method[1]),
  218. array_merge(array(&$model), $params)
  219. );
  220. }
  221. /**
  222. * Gets the method list for attached behaviors, i.e. all public, non-callback methods.
  223. * This does not include mappedMethods.
  224. *
  225. * @return array All public methods for all behaviors attached to this collection
  226. */
  227. public function methods() {
  228. return $this->_methods;
  229. }
  230. /**
  231. * Check to see if a behavior in this collection implements the provided method. Will
  232. * also check mappedMethods.
  233. *
  234. * @param string $method The method to find.
  235. * @param boolean $callback Return the callback for the method.
  236. * @return mixed If $callback is false, a boolean will be returnned, if its true, an array
  237. * containing callback information will be returnned. For mapped methods the array will have 3 elements.
  238. */
  239. public function hasMethod($method, $callback = false) {
  240. if (isset($this->_methods[$method])) {
  241. return $callback ? $this->_methods[$method] : true;
  242. }
  243. foreach ($this->_mappedMethods as $pattern => $target) {
  244. if (preg_match($pattern . 'i', $method)) {
  245. if ($callback) {
  246. $target[] = $method;
  247. return $target;
  248. }
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. }