Base for a static organization website

ObjectCollection.php 11KB

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