ObjectCollection.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. /**
  14. * Deals with Collections of objects. Keeping registries of those objects,
  15. * loading and constructing new objects and triggering callbacks. Each subclass needs
  16. * to implement its own load() functionality.
  17. *
  18. * All core subclasses of ObjectCollection by convention loaded objects are stored
  19. * in `$this->_loaded`. Enabled objects are stored in `$this->_enabled`. In addition
  20. * the all support an `enabled` option that controls the enabled/disabled state of the object
  21. * when loaded.
  22. *
  23. * @package Cake.Utility
  24. * @since CakePHP(tm) v 2.0
  25. */
  26. abstract class ObjectCollection {
  27. /**
  28. * List of the currently-enabled objects
  29. *
  30. * @var array
  31. */
  32. protected $_enabled = array();
  33. /**
  34. * A hash of loaded objects, indexed by name
  35. *
  36. * @var array
  37. */
  38. protected $_loaded = array();
  39. /**
  40. * Default object priority. A non zero integer.
  41. *
  42. * @var int
  43. */
  44. public $defaultPriority = 10;
  45. /**
  46. * Loads a new object onto the collection. Can throw a variety of exceptions
  47. *
  48. * Implementations of this class support a `$options['enabled']` flag which enables/disables
  49. * a loaded object.
  50. *
  51. * @param string $name Name of object to load.
  52. * @param array $options Array of configuration options for the object to be constructed.
  53. * @return object the constructed object
  54. */
  55. abstract public function load($name, $options = array());
  56. /**
  57. * Trigger a callback method on every object in the collection.
  58. * Used to trigger methods on objects in the collection. Will fire the methods in the
  59. * order they were attached.
  60. *
  61. * ### Options
  62. *
  63. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  64. * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
  65. *
  66. * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
  67. * will be returned. If used in combination with `collectReturn` the collected results will be returned.
  68. * Defaults to `false`.
  69. *
  70. * - `collectReturn` Set to true to collect the return of each object into an array.
  71. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  72. *
  73. * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
  74. * Setting modParams to an integer value will allow you to modify the parameter with that index.
  75. * Any non-null value will modify the parameter index indicated.
  76. * Defaults to false.
  77. *
  78. *
  79. * @param string $callback Method to fire on all the objects. Its assumed all the objects implement
  80. * the method you are calling.
  81. * @param array $params Array of parameters for the triggered callback.
  82. * @param array $options Array of options.
  83. * @return mixed Either the last result or all results if collectReturn is on.
  84. * @throws CakeException when modParams is used with an index that does not exist.
  85. */
  86. public function trigger($callback, $params = array(), $options = array()) {
  87. if (empty($this->_enabled)) {
  88. return true;
  89. }
  90. $options = array_merge(
  91. array(
  92. 'break' => false,
  93. 'breakOn' => false,
  94. 'collectReturn' => false,
  95. 'modParams' => false
  96. ),
  97. $options
  98. );
  99. $collected = array();
  100. $list = array_keys($this->_enabled);
  101. if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
  102. throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
  103. }
  104. foreach ($list as $name) {
  105. $result = call_user_func_array(array($this->_loaded[$name], $callback), $params);
  106. if ($options['collectReturn'] === true) {
  107. $collected[] = $result;
  108. }
  109. if (
  110. $options['break'] && ($result === $options['breakOn'] ||
  111. (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
  112. ) {
  113. return $result;
  114. } elseif ($options['modParams'] !== false && !in_array($result, array(true, false, null), true)) {
  115. $params[$options['modParams']] = $result;
  116. }
  117. }
  118. if ($options['modParams'] !== false) {
  119. return $params[$options['modParams']];
  120. }
  121. return $options['collectReturn'] ? $collected : $result;
  122. }
  123. /**
  124. * Provide public read access to the loaded objects
  125. *
  126. * @param string $name Name of property to read
  127. * @return mixed
  128. */
  129. public function __get($name) {
  130. if (isset($this->_loaded[$name])) {
  131. return $this->_loaded[$name];
  132. }
  133. return null;
  134. }
  135. /**
  136. * Provide isset access to _loaded
  137. *
  138. * @param string $name Name of object being checked.
  139. * @return boolean
  140. */
  141. public function __isset($name) {
  142. return isset($this->_loaded[$name]);
  143. }
  144. /**
  145. * Enables callbacks on an object or array of objects
  146. *
  147. * @param string|array $name CamelCased name of the object(s) to enable (string or array)
  148. * @param boolean Prioritize enabled list after enabling object(s)
  149. * @return void
  150. */
  151. public function enable($name, $prioritize = true) {
  152. $enabled = false;
  153. foreach ((array)$name as $object) {
  154. if (isset($this->_loaded[$object]) && !isset($this->_enabled[$object])) {
  155. $priority = isset($this->_loaded[$object]->settings['priority']) ? $this->_loaded[$object]->settings['priority'] : $this->defaultPriority;
  156. $this->_enabled[$object] = array($priority);
  157. $enabled = true;
  158. }
  159. }
  160. if ($prioritize && $enabled) {
  161. $this->prioritize();
  162. }
  163. }
  164. /**
  165. * Prioritize list of enabled object
  166. *
  167. * @return array Prioritized list of object
  168. */
  169. public function prioritize() {
  170. $i = 1;
  171. foreach ($this->_enabled as $name => $priority) {
  172. $priority[1] = $i++;
  173. $this->_enabled[$name] = $priority;
  174. }
  175. asort($this->_enabled);
  176. return $this->_enabled;
  177. }
  178. /**
  179. * Set priority for an object or array of objects
  180. *
  181. * @param string|array $name CamelCased name of the object(s) to enable (string or array)
  182. * If string the second param $priority is used else it should be an associative array
  183. * with keys as object names and values as priorities to set.
  184. * @param int|null Integer priority to set or null for default
  185. * @return void
  186. */
  187. public function setPriority($name, $priority = null) {
  188. if (is_string($name)) {
  189. $name = array($name => $priority);
  190. }
  191. foreach ($name as $obj => $prio) {
  192. if (isset($this->_loaded[$obj])) {
  193. if (is_null($prio)) {
  194. $prio = $this->defaultPriority;
  195. }
  196. $this->_loaded[$obj]->settings['priority'] = $prio;
  197. if (isset($this->_enabled[$obj])) {
  198. $this->_enabled[$obj] = array($prio);
  199. }
  200. }
  201. }
  202. $this->prioritize();
  203. }
  204. /**
  205. * Disables callbacks on a object or array of objects. Public object methods are still
  206. * callable as normal.
  207. *
  208. * @param string|array $name CamelCased name of the objects(s) to disable (string or array)
  209. * @return void
  210. */
  211. public function disable($name) {
  212. foreach ((array)$name as $object) {
  213. unset($this->_enabled[$object]);
  214. }
  215. }
  216. /**
  217. * Gets the list of currently-enabled objects, or, the current status of a single objects
  218. *
  219. * @param string $name Optional. The name of the object to check the status of. If omitted,
  220. * returns an array of currently-enabled object
  221. * @return mixed If $name is specified, returns the boolean status of the corresponding object.
  222. * Otherwise, returns an array of all enabled objects.
  223. */
  224. public function enabled($name = null) {
  225. if (!empty($name)) {
  226. return isset($this->_enabled[$name]);
  227. }
  228. return array_keys($this->_enabled);
  229. }
  230. /**
  231. * Gets the list of attached behaviors, or, whether the given behavior is attached
  232. *
  233. * @param string $name Optional. The name of the behavior to check the status of. If omitted,
  234. * returns an array of currently-attached behaviors
  235. * @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
  236. * Otherwise, returns an array of all attached behaviors.
  237. */
  238. public function attached($name = null) {
  239. if (!empty($name)) {
  240. return isset($this->_loaded[$name]);
  241. }
  242. return array_keys($this->_loaded);
  243. }
  244. /**
  245. * Name of the object to remove from the collection
  246. *
  247. * @param string $name Name of the object to delete.
  248. * @return void
  249. */
  250. public function unload($name) {
  251. list($plugin, $name) = pluginSplit($name);
  252. unset($this->_loaded[$name]);
  253. unset($this->_enabled[$name]);
  254. }
  255. /**
  256. * Adds or overwrites an instantiated object to the collection
  257. *
  258. * @param string $name Name of the object
  259. * @param Object $object The object to use
  260. * @return array Loaded objects
  261. */
  262. public function set($name = null, $object = null) {
  263. if (!empty($name) && !empty($object)) {
  264. list($plugin, $name) = pluginSplit($name);
  265. $this->_loaded[$name] = $object;
  266. }
  267. return $this->_loaded;
  268. }
  269. /**
  270. * Normalizes an object array, creates an array that makes lazy loading
  271. * easier
  272. *
  273. * @param array $objects Array of child objects to normalize.
  274. * @return array Array of normalized objects.
  275. */
  276. public static function normalizeObjectArray($objects) {
  277. $normal = array();
  278. foreach ($objects as $i => $objectName) {
  279. $options = array();
  280. if (!is_int($i)) {
  281. $options = (array)$objectName;
  282. $objectName = $i;
  283. }
  284. list($plugin, $name) = pluginSplit($objectName);
  285. $normal[$name] = array('class' => $objectName, 'settings' => $options);
  286. }
  287. return $normal;
  288. }
  289. }