PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Model/BehaviorCollection.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 275 lines | 136 code | 22 blank | 117 comment | 32 complexity | 2f333a72f3438fd1cbc797a195016b58 MD5 | raw file
  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-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. */
  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. * @todo Make this method a constructor instead..
  52. * @param string $modelName
  53. * @param array $behaviors
  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. * @param string $behavior
  68. * @param array $config
  69. * @return void
  70. * @deprecated Replaced with load()
  71. */
  72. public function attach($behavior, $config = array()) {
  73. return $this->load($behavior, $config);
  74. }
  75. /**
  76. * Loads a behavior into the collection. You can use use `$config['enabled'] = false`
  77. * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
  78. * can still be used as normal.
  79. *
  80. * You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
  81. * {{{
  82. * public $actsAs = array(
  83. * 'Tree' => array(
  84. * 'className' => 'AliasedTree'
  85. * );
  86. * );
  87. * }}}
  88. * All calls to the `Tree` behavior would use `AliasedTree` instead.
  89. *
  90. * @param string $behavior CamelCased name of the behavior to load
  91. * @param array $config Behavior configuration parameters
  92. * @return boolean True on success, false on failure
  93. * @throws MissingBehaviorException when a behavior could not be found.
  94. */
  95. public function load($behavior, $config = array()) {
  96. if (is_array($config) && isset($config['className'])) {
  97. $alias = $behavior;
  98. $behavior = $config['className'];
  99. }
  100. list($plugin, $name) = pluginSplit($behavior, true);
  101. if (!isset($alias)) {
  102. $alias = $name;
  103. }
  104. $class = $name . 'Behavior';
  105. App::uses($class, $plugin . 'Model/Behavior');
  106. if (!class_exists($class)) {
  107. throw new MissingBehaviorException(array(
  108. 'class' => $class,
  109. 'plugin' => substr($plugin, 0, -1)
  110. ));
  111. }
  112. if (!isset($this->{$alias})) {
  113. if (ClassRegistry::isKeySet($class)) {
  114. $this->_loaded[$alias] = ClassRegistry::getObject($class);
  115. } else {
  116. $this->_loaded[$alias] = new $class();
  117. ClassRegistry::addObject($class, $this->_loaded[$alias]);
  118. if (!empty($plugin)) {
  119. ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
  120. }
  121. }
  122. } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
  123. if ($config !== null && $config !== false) {
  124. $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
  125. } else {
  126. $config = array();
  127. }
  128. }
  129. if (empty($config)) {
  130. $config = array();
  131. }
  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. $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
  154. if (!in_array($alias, $this->_enabled) && !$configDisabled) {
  155. $this->enable($alias);
  156. } elseif ($configDisabled) {
  157. $this->disable($alias);
  158. }
  159. return true;
  160. }
  161. /**
  162. * Detaches a behavior from a model
  163. *
  164. * @param string $name CamelCased name of the behavior to unload
  165. * @return void
  166. */
  167. public function unload($name) {
  168. list($plugin, $name) = pluginSplit($name);
  169. if (isset($this->_loaded[$name])) {
  170. $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
  171. unset($this->_loaded[$name]);
  172. }
  173. foreach ($this->_methods as $m => $callback) {
  174. if (is_array($callback) && $callback[0] == $name) {
  175. unset($this->_methods[$m]);
  176. }
  177. }
  178. $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
  179. }
  180. /**
  181. * Backwards compatible alias for unload()
  182. *
  183. * @param string $name Name of behavior
  184. * @return void
  185. * @deprecated 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 boolean $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', "BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $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 boolean $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. }