ObjectCollection.php 8.2 KB

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