ObjectCollection.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Deals with Collections of objects. Keeping registries of those objects,
  4. * loading and constructing new objects and triggering callbacks.
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake
  17. * @subpackage cake.cake.libs.view
  18. * @since CakePHP(tm) v 0.10.0.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. abstract class ObjectCollection {
  22. /**
  23. * List of the currently-enabled objects
  24. *
  25. * @var array
  26. * @access protected
  27. */
  28. protected $_enabled = array();
  29. /**
  30. * A hash of loaded objects, indexed by name
  31. *
  32. * @var array
  33. */
  34. protected $_loaded = array();
  35. /**
  36. * Loads a new object onto the collection. Can throw a variety of exceptions
  37. *
  38. * Implementations of this class support a `$options['callbacks']` flag which enables/disables
  39. * a loaded object.
  40. *
  41. * @param string $name Name of object to load.
  42. * @param array $options Array of configuration options for the object to be constructed.
  43. * @return object the constructed object
  44. */
  45. abstract public function load($name, $options = array());
  46. /**
  47. * Trigger a callback method on every object in the collection.
  48. * Used to trigger methods on objects in the collection. Will fire the methods in the
  49. * order they were attached.
  50. *
  51. * ### Options
  52. *
  53. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  54. * Defaults to `false`
  55. * - `break` Set to true to enabled breaking. Defaults to `false`.
  56. * - `collectReturn` Set to true to collect the return of each object into an array.
  57. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  58. * - `triggerDisabled` Will trigger the callback on all objects in the collection even the non-enabled
  59. * objects. Defaults to false.
  60. *
  61. * @param string $callback Method to fire on all the objects. Its assumed all the objects implement
  62. * the method you are calling.
  63. * @param array $params Array of parameters for the triggered callback.
  64. * @param array $options Array of options.
  65. * @return mixed true.
  66. */
  67. public function trigger($callback, $params = array(), $options = array()) {
  68. if (empty($this->_enabled)) {
  69. return true;
  70. }
  71. $options = array_merge(
  72. array('break' => false, 'breakOn' => false, 'collectReturn' => false, 'triggerDisabled' => false),
  73. $options
  74. );
  75. $collected = array();
  76. $list = $this->_enabled;
  77. if ($options['triggerDisabled'] === true) {
  78. $list = array_keys($this->_loaded);
  79. }
  80. foreach ($list as $name) {
  81. $result = call_user_func_array(array(&$this->_loaded[$name], $callback), $params);
  82. if ($options['collectReturn'] === true) {
  83. $collected[] = $result;
  84. }
  85. if (
  86. $options['break'] && ($result === $options['breakOn'] ||
  87. (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
  88. ) {
  89. return ($options['collectReturn'] === true) ? $collected : $result;
  90. }
  91. }
  92. return $options['collectReturn'] ? $collected : true;
  93. }
  94. /**
  95. * Provide public read access to the loaded objects
  96. *
  97. * @param string $name Name of property to read
  98. * @return mixed
  99. */
  100. public function __get($name) {
  101. if (isset($this->_loaded[$name])) {
  102. return $this->_loaded[$name];
  103. }
  104. return null;
  105. }
  106. /**
  107. * Provide isset access to _loaded
  108. *
  109. * @param sting $name Name of object being checked.
  110. * @return boolean
  111. */
  112. public function __isset($name) {
  113. return isset($this->_loaded[$name]);
  114. }
  115. /**
  116. * Enables callbacks on an object or array of objects
  117. *
  118. * @param mixed $name CamelCased name of the object(s) to enable (string or array)
  119. * @return void
  120. */
  121. public function enable($name) {
  122. foreach ((array)$name as $object) {
  123. if (isset($this->_loaded[$object]) && array_search($object, $this->_enabled) === false) {
  124. $this->_enabled[] = $object;
  125. }
  126. }
  127. }
  128. /**
  129. * Disables callbacks on a object or array of objects. Public object methods are still
  130. * callable as normal.
  131. *
  132. * @param mixed $name CamelCased name of the objects(s) to disable (string or array)
  133. * @return void
  134. */
  135. public function disable($name) {
  136. foreach ((array)$name as $object) {
  137. $index = array_search($object, $this->_enabled);
  138. unset($this->_enabled[$index]);
  139. }
  140. $this->_enabled = array_values($this->_enabled);
  141. }
  142. /**
  143. * Gets the list of currently-enabled objects, or, the current status of a single objects
  144. *
  145. * @param string $name Optional. The name of the object to check the status of. If omitted,
  146. * returns an array of currently-enabled object
  147. * @return mixed If $name is specified, returns the boolean status of the corresponding object.
  148. * Otherwise, returns an array of all enabled objects.
  149. */
  150. public function enabled($name = null) {
  151. if (!empty($name)) {
  152. return in_array($name, $this->_enabled);
  153. }
  154. return $this->_enabled;
  155. }
  156. /**
  157. * Gets the list of attached behaviors, or, whether the given behavior is attached
  158. *
  159. * @param string $name Optional. The name of the behavior to check the status of. If omitted,
  160. * returns an array of currently-attached behaviors
  161. * @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
  162. * Otherwise, returns an array of all attached behaviors.
  163. */
  164. public function attached($name = null) {
  165. if (!empty($name)) {
  166. return isset($this->_loaded[$name]);
  167. }
  168. return array_keys($this->_loaded);
  169. }
  170. /**
  171. * Name of the object to remove from the collection
  172. *
  173. * @param string $name Name of the object to delete.
  174. * @return void
  175. */
  176. public function unload($name) {
  177. list($plugin, $name) = pluginSplit($name);
  178. unset($this->_loaded[$name]);
  179. $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
  180. }
  181. /**
  182. * Normalizes an object array, creates an array that makes lazy loading
  183. * easier
  184. *
  185. * @param array $objects Array of child objects to normalize.
  186. * @return array Array of normalized objects.
  187. */
  188. public static function normalizeObjectArray($objects) {
  189. $normal = array();
  190. foreach ($objects as $i => $objectName) {
  191. $options = array();
  192. if (!is_int($i)) {
  193. list($options, $objectName) = array($objectName, $i);
  194. }
  195. list($plugin, $name) = pluginSplit($objectName);
  196. $normal[$name] = array('class' => $objectName, 'settings' => $options);
  197. }
  198. return $normal;
  199. }
  200. }