PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/cakephp/lib/Cake/Model/BehaviorCollection.php

http://github.com/eryx/php-framework-benchmark
PHP | 295 lines | 149 code | 24 blank | 122 comment | 33 complexity | 300bd8b58f16006bc54ce98e71955d36 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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. * @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. $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
  101. unset($config['enabled'], $config['className']);
  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. if (!empty($plugin)) {
  121. ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
  122. }
  123. }
  124. } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
  125. if ($config !== null && $config !== false) {
  126. $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
  127. } else {
  128. $config = array();
  129. }
  130. }
  131. if (empty($config)) {
  132. $config = array();
  133. }
  134. $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
  135. foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
  136. $this->_mappedMethods[$method] = array($alias, $methodAlias);
  137. }
  138. $methods = get_class_methods($this->_loaded[$alias]);
  139. $parentMethods = array_flip(get_class_methods('ModelBehavior'));
  140. $callbacks = array(
  141. 'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
  142. 'beforeDelete', 'afterDelete', 'onError'
  143. );
  144. foreach ($methods as $m) {
  145. if (!isset($parentMethods[$m])) {
  146. $methodAllowed = (
  147. $m[0] != '_' && !array_key_exists($m, $this->_methods) &&
  148. !in_array($m, $callbacks)
  149. );
  150. if ($methodAllowed) {
  151. $this->_methods[$m] = array($alias, $m);
  152. }
  153. }
  154. }
  155. if (!in_array($alias, $this->_enabled) && !$configDisabled) {
  156. $this->enable($alias);
  157. } else {
  158. $this->disable($alias);
  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($plugin, $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 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. /**
  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. }