ObjectCollection.php 7.9 KB

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