PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bk/lib/Cake/Utility/ObjectCollection.php

https://gitlab.com/digaotinfo/agendaLegislativa
PHP | 326 lines | 150 code | 18 blank | 158 comment | 27 complexity | 2cac19d6958166f119ab806b3f2c678e MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, 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-2012, 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. * Default object priority. A non zero integer.
  41. *
  42. * @var int
  43. */
  44. public $defaultPriority = 10;
  45. /**
  46. * Loads a new object onto the collection. Can throw a variety of exceptions
  47. *
  48. * Implementations of this class support a `$options['enabled']` flag which enables/disables
  49. * a loaded object.
  50. *
  51. * @param string $name Name of object to load.
  52. * @param array $options Array of configuration options for the object to be constructed.
  53. * @return object the constructed object
  54. */
  55. abstract public function load($name, $options = array());
  56. /**
  57. * Trigger a callback method on every object in the collection.
  58. * Used to trigger methods on objects in the collection. Will fire the methods in the
  59. * order they were attached.
  60. *
  61. * ### Options
  62. *
  63. * - `breakOn` Set to the value or values you want the callback propagation to stop on.
  64. * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
  65. *
  66. * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
  67. * will be returned. If used in combination with `collectReturn` the collected results will be returned.
  68. * Defaults to `false`.
  69. *
  70. * - `collectReturn` Set to true to collect the return of each object into an array.
  71. * This array of return values will be returned from the trigger() call. Defaults to `false`.
  72. *
  73. * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
  74. * Setting modParams to an integer value will allow you to modify the parameter with that index.
  75. * Any non-null value will modify the parameter index indicated.
  76. * Defaults to false.
  77. *
  78. *
  79. * @param string $callback|CakeEvent 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. //TODO: Temporary BC check, while we move all the triggers system into the CakeEventManager
  101. foreach (array('break', 'breakOn', 'collectReturn', 'modParams') as $opt) {
  102. if (isset($event->{$opt})) {
  103. $options[$opt] = $event->{$opt};
  104. }
  105. }
  106. $parts = explode('.', $event->name());
  107. $callback = array_pop($parts);
  108. }
  109. $options = array_merge(
  110. array(
  111. 'break' => false,
  112. 'breakOn' => false,
  113. 'collectReturn' => false,
  114. 'modParams' => false
  115. ),
  116. $options
  117. );
  118. $collected = array();
  119. $list = array_keys($this->_enabled);
  120. if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
  121. throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
  122. }
  123. foreach ($list as $name) {
  124. $result = call_user_func_array(array($this->_loaded[$name], $callback), compact('subject') + $params);
  125. if ($options['collectReturn'] === true) {
  126. $collected[] = $result;
  127. }
  128. if (
  129. $options['break'] && ($result === $options['breakOn'] ||
  130. (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
  131. ) {
  132. return $result;
  133. } elseif ($options['modParams'] !== false && !in_array($result, array(true, false, null), true)) {
  134. $params[$options['modParams']] = $result;
  135. }
  136. }
  137. if ($options['modParams'] !== false) {
  138. return $params[$options['modParams']];
  139. }
  140. return $options['collectReturn'] ? $collected : $result;
  141. }
  142. /**
  143. * Provide public read access to the loaded objects
  144. *
  145. * @param string $name Name of property to read
  146. * @return mixed
  147. */
  148. public function __get($name) {
  149. if (isset($this->_loaded[$name])) {
  150. return $this->_loaded[$name];
  151. }
  152. return null;
  153. }
  154. /**
  155. * Provide isset access to _loaded
  156. *
  157. * @param string $name Name of object being checked.
  158. * @return boolean
  159. */
  160. public function __isset($name) {
  161. return isset($this->_loaded[$name]);
  162. }
  163. /**
  164. * Enables callbacks on an object or array of objects
  165. *
  166. * @param string|array $name CamelCased name of the object(s) to enable (string or array)
  167. * @param boolean Prioritize enabled list after enabling object(s)
  168. * @return void
  169. */
  170. public function enable($name, $prioritize = true) {
  171. $enabled = false;
  172. foreach ((array)$name as $object) {
  173. if (isset($this->_loaded[$object]) && !isset($this->_enabled[$object])) {
  174. $priority = isset($this->_loaded[$object]->settings['priority']) ? $this->_loaded[$object]->settings['priority'] : $this->defaultPriority;
  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 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 $obj => $prio) {
  211. if (isset($this->_loaded[$obj])) {
  212. if (is_null($prio)) {
  213. $prio = $this->defaultPriority;
  214. }
  215. $this->_loaded[$obj]->settings['priority'] = $prio;
  216. if (isset($this->_enabled[$obj])) {
  217. $this->_enabled[$obj] = array($prio);
  218. }
  219. }
  220. }
  221. $this->prioritize();
  222. }
  223. /**
  224. * Disables callbacks on a object or array of objects. Public object methods are still
  225. * callable as normal.
  226. *
  227. * @param string|array $name CamelCased name of the objects(s) to disable (string or array)
  228. * @return void
  229. */
  230. public function disable($name) {
  231. foreach ((array)$name as $object) {
  232. unset($this->_enabled[$object]);
  233. }
  234. }
  235. /**
  236. * Gets the list of currently-enabled objects, or, the current status of a single objects
  237. *
  238. * @param string $name Optional. The name of the object to check the status of. If omitted,
  239. * returns an array of currently-enabled object
  240. * @return mixed If $name is specified, returns the boolean status of the corresponding object.
  241. * Otherwise, returns an array of all enabled objects.
  242. */
  243. public function enabled($name = null) {
  244. if (!empty($name)) {
  245. return isset($this->_enabled[$name]);
  246. }
  247. return array_keys($this->_enabled);
  248. }
  249. /**
  250. * Gets the list of attached objects, or, whether the given object is attached
  251. *
  252. * @param string $name Optional. The name of the behavior to check the status of. If omitted,
  253. * returns an array of currently-attached behaviors
  254. * @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
  255. * Otherwise, returns an array of all attached behaviors.
  256. */
  257. public function attached($name = null) {
  258. if (!empty($name)) {
  259. return isset($this->_loaded[$name]);
  260. }
  261. return array_keys($this->_loaded);
  262. }
  263. /**
  264. * Name of the object to remove from the collection
  265. *
  266. * @param string $name Name of the object to delete.
  267. * @return void
  268. */
  269. public function unload($name) {
  270. list($plugin, $name) = pluginSplit($name);
  271. unset($this->_loaded[$name]);
  272. unset($this->_enabled[$name]);
  273. }
  274. /**
  275. * Adds or overwrites an instantiated object to the collection
  276. *
  277. * @param string $name Name of the object
  278. * @param Object $object The object to use
  279. * @return array Loaded objects
  280. */
  281. public function set($name = null, $object = null) {
  282. if (!empty($name) && !empty($object)) {
  283. list($plugin, $name) = pluginSplit($name);
  284. $this->_loaded[$name] = $object;
  285. }
  286. return $this->_loaded;
  287. }
  288. /**
  289. * Normalizes an object array, creates an array that makes lazy loading
  290. * easier
  291. *
  292. * @param array $objects Array of child objects to normalize.
  293. * @return array Array of normalized objects.
  294. */
  295. public static function normalizeObjectArray($objects) {
  296. $normal = array();
  297. foreach ($objects as $i => $objectName) {
  298. $options = array();
  299. if (!is_int($i)) {
  300. $options = (array)$objectName;
  301. $objectName = $i;
  302. }
  303. list($plugin, $name) = pluginSplit($objectName);
  304. $normal[$name] = array('class' => $objectName, 'settings' => $options);
  305. }
  306. return $normal;
  307. }
  308. }