BehaviorCollection.php 8.3 KB

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