BehaviorCollection.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * BehaviorCollection
  4. *
  5. * Provides management 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-2012, 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-2012, 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. App::uses('CakeEventListener', 'Event');
  23. /**
  24. * Model behavior collection class.
  25. *
  26. * Defines the Behavior interface, and contains common model interaction functionality.
  27. *
  28. * @package Cake.Model
  29. */
  30. class BehaviorCollection extends ObjectCollection implements CakeEventListener {
  31. /**
  32. * Stores a reference to the attached name
  33. *
  34. * @var string
  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 MissingBehaviorException 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. $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
  102. unset($config['enabled'], $config['className']);
  103. list($plugin, $name) = pluginSplit($behavior, true);
  104. if (!isset($alias)) {
  105. $alias = $name;
  106. }
  107. $class = $name . 'Behavior';
  108. App::uses($class, $plugin . 'Model/Behavior');
  109. if (!class_exists($class)) {
  110. throw new MissingBehaviorException(array(
  111. 'class' => $class,
  112. 'plugin' => substr($plugin, 0, -1)
  113. ));
  114. }
  115. if (!isset($this->{$alias})) {
  116. if (ClassRegistry::isKeySet($class)) {
  117. $this->_loaded[$alias] = ClassRegistry::getObject($class);
  118. } else {
  119. $this->_loaded[$alias] = new $class();
  120. ClassRegistry::addObject($class, $this->_loaded[$alias]);
  121. if (!empty($plugin)) {
  122. ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
  123. }
  124. }
  125. } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
  126. if ($config !== null && $config !== false) {
  127. $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
  128. } else {
  129. $config = array();
  130. }
  131. }
  132. if (empty($config)) {
  133. $config = array();
  134. }
  135. $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
  136. foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
  137. $this->_mappedMethods[$method] = array($alias, $methodAlias);
  138. }
  139. $methods = get_class_methods($this->_loaded[$alias]);
  140. $parentMethods = array_flip(get_class_methods('ModelBehavior'));
  141. $callbacks = array(
  142. 'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
  143. 'beforeDelete', 'afterDelete', 'onError'
  144. );
  145. foreach ($methods as $m) {
  146. if (!isset($parentMethods[$m])) {
  147. $methodAllowed = (
  148. $m[0] != '_' && !array_key_exists($m, $this->_methods) &&
  149. !in_array($m, $callbacks)
  150. );
  151. if ($methodAllowed) {
  152. $this->_methods[$m] = array($alias, $m);
  153. }
  154. }
  155. }
  156. if (!in_array($alias, $this->_enabled) && !$configDisabled) {
  157. $this->enable($alias);
  158. } else {
  159. $this->disable($alias);
  160. }
  161. return true;
  162. }
  163. /**
  164. * Detaches a behavior from a model
  165. *
  166. * @param string $name CamelCased name of the behavior to unload
  167. * @return void
  168. */
  169. public function unload($name) {
  170. list($plugin, $name) = pluginSplit($name);
  171. if (isset($this->_loaded[$name])) {
  172. $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
  173. parent::unload($name);
  174. }
  175. foreach ($this->_methods as $m => $callback) {
  176. if (is_array($callback) && $callback[0] == $name) {
  177. unset($this->_methods[$m]);
  178. }
  179. }
  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 handled 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 returned, if its true, an array
  237. * containing callback information will be returned. 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. /**
  255. * Returns the implemented events that will get routed to the trigger function
  256. * in order to dispatch them separately on each behavior
  257. *
  258. * @return array
  259. */
  260. public function implementedEvents() {
  261. return array(
  262. 'Model.beforeFind' => 'trigger',
  263. 'Model.afterFind' => 'trigger',
  264. 'Model.beforeValidate' => 'trigger',
  265. 'Model.afterValidate' => 'trigger',
  266. 'Model.beforeSave' => 'trigger',
  267. 'Model.afterSave' => 'trigger',
  268. 'Model.beforeDelete' => 'trigger',
  269. 'Model.afterDelete' => 'trigger'
  270. );
  271. }
  272. }