ObjectCollection.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * Loads a new object onto the collection. Can throw a variety of exceptions
  41. *
  42. * Implementations of this class support a `$options['callbacks']` flag which enables/disables
  43. * a loaded object.
  44. *
  45. * @param string $name Name of object to load.
  46. * @param array $options Array of configuration options for the object to be constructed.
  47. * @return object the constructed object
  48. */
  49. abstract public function load($name, $options = array());
  50. /**
  51. * Trigger a callback method on every object in the collection.
  52. * Used to trigger methods on objects in the collection. Will fire the methods in the
  53. * order they were attached.
  54. *
  55. * ### Options
  56. *
  57. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  58. * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
  59. *
  60. * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
  61. * will be returned. If used in combination with `collectReturn` the collected results will be returned.
  62. * Defaults to `false`.
  63. *
  64. * - `collectReturn` Set to true to collect the return of each object into an array.
  65. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  66. *
  67. * - `triggerDisabled` Will trigger the callback on all objects in the collection even the non-enabled
  68. * objects. Defaults to false.
  69. *
  70. * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
  71. * Setting modParams to an integer value will allow you to modify the parameter with that index.
  72. * Any non-null value will modify the parameter index indicated.
  73. * Defaults to false.
  74. *
  75. *
  76. * @param string $callback Method to fire on all the objects. Its assumed all the objects implement
  77. * the method you are calling.
  78. * @param array $params Array of parameters for the triggered callback.
  79. * @param array $options Array of options.
  80. * @return mixed Either the last result or all results if collectReturn is on.
  81. * @throws CakeException when modParams is used with an index that does not exist.
  82. */
  83. public function trigger($callback, $params = array(), $options = array()) {
  84. if (empty($this->_enabled)) {
  85. return true;
  86. }
  87. $options = array_merge(
  88. array(
  89. 'break' => false,
  90. 'breakOn' => false,
  91. 'collectReturn' => false,
  92. 'triggerDisabled' => false,
  93. 'modParams' => false
  94. ),
  95. $options
  96. );
  97. $collected = array();
  98. $list = $this->_enabled;
  99. if ($options['triggerDisabled'] === true) {
  100. $list = array_keys($this->_loaded);
  101. }
  102. if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
  103. throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
  104. }
  105. foreach ($list as $name) {
  106. $result = call_user_func_array(array($this->_loaded[$name], $callback), $params);
  107. if ($options['collectReturn'] === true) {
  108. $collected[] = $result;
  109. }
  110. if (
  111. $options['break'] && ($result === $options['breakOn'] ||
  112. (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
  113. ) {
  114. return $result;
  115. } elseif ($options['modParams'] !== false && is_array($result)) {
  116. $params[$options['modParams']] = $result;
  117. }
  118. }
  119. if ($options['modParams'] !== false) {
  120. return $params[$options['modParams']];
  121. }
  122. return $options['collectReturn'] ? $collected : $result;
  123. }
  124. /**
  125. * Provide public read access to the loaded objects
  126. *
  127. * @param string $name Name of property to read
  128. * @return mixed
  129. */
  130. public function __get($name) {
  131. if (isset($this->_loaded[$name])) {
  132. return $this->_loaded[$name];
  133. }
  134. return null;
  135. }
  136. /**
  137. * Provide isset access to _loaded
  138. *
  139. * @param string $name Name of object being checked.
  140. * @return boolean
  141. */
  142. public function __isset($name) {
  143. return isset($this->_loaded[$name]);
  144. }
  145. /**
  146. * Enables callbacks on an object or array of objects
  147. *
  148. * @param mixed $name CamelCased name of the object(s) to enable (string or array)
  149. * @return void
  150. */
  151. public function enable($name) {
  152. foreach ((array)$name as $object) {
  153. if (isset($this->_loaded[$object]) && array_search($object, $this->_enabled) === false) {
  154. $this->_enabled[] = $object;
  155. }
  156. }
  157. }
  158. /**
  159. * Disables callbacks on a object or array of objects. Public object methods are still
  160. * callable as normal.
  161. *
  162. * @param mixed $name CamelCased name of the objects(s) to disable (string or array)
  163. * @return void
  164. */
  165. public function disable($name) {
  166. foreach ((array)$name as $object) {
  167. $index = array_search($object, $this->_enabled);
  168. unset($this->_enabled[$index]);
  169. }
  170. $this->_enabled = array_values($this->_enabled);
  171. }
  172. /**
  173. * Gets the list of currently-enabled objects, or, the current status of a single objects
  174. *
  175. * @param string $name Optional. The name of the object to check the status of. If omitted,
  176. * returns an array of currently-enabled object
  177. * @return mixed If $name is specified, returns the boolean status of the corresponding object.
  178. * Otherwise, returns an array of all enabled objects.
  179. */
  180. public function enabled($name = null) {
  181. if (!empty($name)) {
  182. return in_array($name, $this->_enabled);
  183. }
  184. return $this->_enabled;
  185. }
  186. /**
  187. * Gets the list of attached behaviors, or, whether the given behavior is attached
  188. *
  189. * @param string $name Optional. The name of the behavior to check the status of. If omitted,
  190. * returns an array of currently-attached behaviors
  191. * @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
  192. * Otherwise, returns an array of all attached behaviors.
  193. */
  194. public function attached($name = null) {
  195. if (!empty($name)) {
  196. return isset($this->_loaded[$name]);
  197. }
  198. return array_keys($this->_loaded);
  199. }
  200. /**
  201. * Name of the object to remove from the collection
  202. *
  203. * @param string $name Name of the object to delete.
  204. * @return void
  205. */
  206. public function unload($name) {
  207. list($plugin, $name) = pluginSplit($name);
  208. unset($this->_loaded[$name]);
  209. $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
  210. }
  211. /**
  212. * Adds or overwrites an instantiated object to the collection
  213. *
  214. * @param string $name Name of the object
  215. * @param Object $object The object to use
  216. * @return array Loaded objects
  217. */
  218. public function set($name = null, $object = null) {
  219. if (!empty($name) && !empty($object)) {
  220. list($plugin, $name) = pluginSplit($name);
  221. $this->_loaded[$name] = $object;
  222. }
  223. return $this->_loaded;
  224. }
  225. /**
  226. * Normalizes an object array, creates an array that makes lazy loading
  227. * easier
  228. *
  229. * @param array $objects Array of child objects to normalize.
  230. * @return array Array of normalized objects.
  231. */
  232. public static function normalizeObjectArray($objects) {
  233. $normal = array();
  234. foreach ($objects as $i => $objectName) {
  235. $options = array();
  236. if (!is_int($i)) {
  237. $options = (array)$objectName;
  238. $objectName = $i;
  239. }
  240. list($plugin, $name) = pluginSplit($objectName);
  241. $normal[$name] = array('class' => $objectName, 'settings' => $options);
  242. }
  243. return $normal;
  244. }
  245. }