PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Model/BehaviorCollection.php

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