BehaviorCollection.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.libs.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.libs.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. * @access public
  54. * @return void
  55. */
  56. public function init($modelName, $behaviors = array()) {
  57. $this->modelName = $modelName;
  58. if (!empty($behaviors)) {
  59. foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
  60. $this->load($config['class'], $config['settings']);
  61. }
  62. }
  63. }
  64. /**
  65. * Backwards compatible alias for load()
  66. *
  67. * @return void
  68. * @deprecated Replaced with load()
  69. */
  70. public function attach($behavior, $config = array()) {
  71. return $this->load($behavior, $config);
  72. }
  73. /**
  74. * Loads a behavior into the collection. You can use use `$config['enabled'] = false`
  75. * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
  76. * can still be used as normal.
  77. *
  78. * You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
  79. * {{{
  80. * public $actsAs = array(
  81. * 'Tree' => array(
  82. * 'className' => 'AliasedTree'
  83. * );
  84. * );
  85. * }}}
  86. * All calls to the `Tree` behavior would use `AliasedTree` instead.
  87. *
  88. * @param string $behavior CamelCased name of the behavior to load
  89. * @param array $config Behavior configuration parameters
  90. * @return boolean True on success, false on failure
  91. * @throws MissingBehaviorClassException when a behavior could not be found.
  92. */
  93. public function load($behavior, $config = array()) {
  94. if (is_array($config) && isset($config['className'])) {
  95. $alias = $behavior;
  96. $behavior = $config['className'];
  97. }
  98. list($plugin, $name) = pluginSplit($behavior, true);
  99. if (!isset($alias)) {
  100. $alias = $name;
  101. }
  102. $class = $name . 'Behavior';
  103. App::uses($class, $plugin . 'Model/Behavior');
  104. if (!class_exists($class)) {
  105. throw new MissingBehaviorClassException(array(
  106. 'file' => Inflector::underscore($behavior) . '.php',
  107. 'class' => $class
  108. ));
  109. }
  110. if (!isset($this->{$alias})) {
  111. if (ClassRegistry::isKeySet($class)) {
  112. $this->_loaded[$alias] = ClassRegistry::getObject($class);
  113. } else {
  114. $this->_loaded[$alias] = new $class();
  115. ClassRegistry::addObject($class, $this->_loaded[$alias]);
  116. if (!empty($plugin)) {
  117. ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
  118. }
  119. }
  120. } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
  121. if ($config !== null && $config !== false) {
  122. $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
  123. } else {
  124. $config = array();
  125. }
  126. }
  127. if (empty($config)) {
  128. $config = array();
  129. }
  130. $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
  131. foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
  132. $this->_mappedMethods[$method] = array($alias, $methodAlias);
  133. }
  134. $methods = get_class_methods($this->_loaded[$alias]);
  135. $parentMethods = array_flip(get_class_methods('ModelBehavior'));
  136. $callbacks = array(
  137. 'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
  138. 'beforeDelete', 'afterDelete', 'onError'
  139. );
  140. foreach ($methods as $m) {
  141. if (!isset($parentMethods[$m])) {
  142. $methodAllowed = (
  143. $m[0] != '_' && !array_key_exists($m, $this->_methods) &&
  144. !in_array($m, $callbacks)
  145. );
  146. if ($methodAllowed) {
  147. $this->_methods[$m] = array($alias, $m);
  148. }
  149. }
  150. }
  151. $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
  152. if (!in_array($alias, $this->_enabled) && !$configDisabled) {
  153. $this->enable($alias);
  154. } elseif ($configDisabled) {
  155. $this->disable($alias);
  156. }
  157. return true;
  158. }
  159. /**
  160. * Detaches a behavior from a model
  161. *
  162. * @param string $name CamelCased name of the behavior to unload
  163. * @return void
  164. */
  165. public function unload($name) {
  166. list($plugin, $name) = pluginSplit($name);
  167. if (isset($this->_loaded[$name])) {
  168. $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
  169. unset($this->_loaded[$name]);
  170. }
  171. foreach ($this->_methods as $m => $callback) {
  172. if (is_array($callback) && $callback[0] == $name) {
  173. unset($this->_methods[$m]);
  174. }
  175. }
  176. $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
  177. }
  178. /**
  179. * Backwards compatible alias for unload()
  180. *
  181. * @param string $name Name of behavior
  182. * @return void
  183. * @deprecated Use unload instead.
  184. */
  185. public function detach($name) {
  186. return $this->unload($name);
  187. }
  188. /**
  189. * Dispatches a behavior method. Will call either normal methods or mapped methods.
  190. *
  191. * If a method is not handeled by the BehaviorCollection, and $strict is false, a
  192. * special return of `array('unhandled')` will be returned to signal the method was not found.
  193. *
  194. * @param Model $model The model the method was originally called on.
  195. * @param string $method The method called.
  196. * @param array $params Parameters for the called method.
  197. * @param boolean $strict If methods are not found, trigger an error.
  198. * @return array All methods for all behaviors attached to this object
  199. */
  200. public function dispatchMethod($model, $method, $params = array(), $strict = false) {
  201. $method = $this->hasMethod($method, true);
  202. if ($strict && empty($method)) {
  203. trigger_error(__d('cake_dev', "BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $method), E_USER_WARNING);
  204. return null;
  205. }
  206. if (empty($method)) {
  207. return array('unhandled');
  208. }
  209. if (count($method) === 3) {
  210. array_unshift($params, $method[2]);
  211. unset($method[2]);
  212. }
  213. return call_user_func_array(
  214. array($this->_loaded[$method[0]], $method[1]),
  215. array_merge(array(&$model), $params)
  216. );
  217. }
  218. /**
  219. * Gets the method list for attached behaviors, i.e. all public, non-callback methods.
  220. * This does not include mappedMethods.
  221. *
  222. * @return array All public methods for all behaviors attached to this collection
  223. */
  224. public function methods() {
  225. return $this->_methods;
  226. }
  227. /**
  228. * Check to see if a behavior in this collection implements the provided method. Will
  229. * also check mappedMethods.
  230. *
  231. * @param string $method The method to find.
  232. * @param boolean $callback Return the callback for the method.
  233. * @return mixed If $callback is false, a boolean will be returnned, if its true, an array
  234. * containing callback information will be returnned. For mapped methods the array will have 3 elements.
  235. */
  236. public function hasMethod($method, $callback = false) {
  237. if (isset($this->_methods[$method])) {
  238. return $callback ? $this->_methods[$method] : true;
  239. }
  240. foreach ($this->_mappedMethods as $pattern => $target) {
  241. if (preg_match($pattern . 'i', $method)) {
  242. if ($callback) {
  243. $target[] = $method;
  244. return $target;
  245. }
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. }