/libraries/vendor/joomla/event/src/ListenersPriorityQueue.php

https://gitlab.com/vitaliylukin91/idea-rating · PHP · 206 lines · 77 code · 24 blank · 105 comment · 4 complexity · cacdc77c93627e68dae01204d5b5a97f MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Joomla Framework Event Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Event;
  9. use SplPriorityQueue;
  10. use SplObjectStorage;
  11. use IteratorAggregate;
  12. use Countable;
  13. /**
  14. * A class containing an inner listeners priority queue that can be iterated multiple times.
  15. * One instance of ListenersPriorityQueue is used per Event in the Dispatcher.
  16. *
  17. * @since 1.0
  18. */
  19. class ListenersPriorityQueue implements IteratorAggregate, Countable
  20. {
  21. /**
  22. * The inner priority queue.
  23. *
  24. * @var SplPriorityQueue
  25. *
  26. * @since 1.0
  27. */
  28. protected $queue;
  29. /**
  30. * A copy of the listeners contained in the queue
  31. * that is used when detaching them to
  32. * recreate the queue or to see if the queue contains
  33. * a given listener.
  34. *
  35. * @var SplObjectStorage
  36. *
  37. * @since 1.0
  38. */
  39. protected $storage;
  40. /**
  41. * A decreasing counter used to compute
  42. * the internal priority as an array because
  43. * SplPriorityQueue dequeues elements with the same priority.
  44. *
  45. * @var integer
  46. *
  47. * @since 1.0
  48. */
  49. private $counter = PHP_INT_MAX;
  50. /**
  51. * Constructor.
  52. *
  53. * @since 1.0
  54. */
  55. public function __construct()
  56. {
  57. $this->queue = new SplPriorityQueue;
  58. $this->storage = new SplObjectStorage;
  59. }
  60. /**
  61. * Add a listener with the given priority only if not already present.
  62. *
  63. * @param \Closure|object $listener The listener.
  64. * @param integer $priority The listener priority.
  65. *
  66. * @return ListenersPriorityQueue This method is chainable.
  67. *
  68. * @since 1.0
  69. */
  70. public function add($listener, $priority)
  71. {
  72. if (!$this->storage->contains($listener))
  73. {
  74. // Compute the internal priority as an array.
  75. $priority = array($priority, $this->counter--);
  76. $this->storage->attach($listener, $priority);
  77. $this->queue->insert($listener, $priority);
  78. }
  79. return $this;
  80. }
  81. /**
  82. * Remove a listener from the queue.
  83. *
  84. * @param \Closure|object $listener The listener.
  85. *
  86. * @return ListenersPriorityQueue This method is chainable.
  87. *
  88. * @since 1.0
  89. */
  90. public function remove($listener)
  91. {
  92. if ($this->storage->contains($listener))
  93. {
  94. $this->storage->detach($listener);
  95. $this->storage->rewind();
  96. $this->queue = new SplPriorityQueue;
  97. foreach ($this->storage as $listener)
  98. {
  99. $priority = $this->storage->getInfo();
  100. $this->queue->insert($listener, $priority);
  101. }
  102. }
  103. return $this;
  104. }
  105. /**
  106. * Tell if the listener exists in the queue.
  107. *
  108. * @param \Closure|object $listener The listener.
  109. *
  110. * @return boolean True if it exists, false otherwise.
  111. *
  112. * @since 1.0
  113. */
  114. public function has($listener)
  115. {
  116. return $this->storage->contains($listener);
  117. }
  118. /**
  119. * Get the priority of the given listener.
  120. *
  121. * @param \Closure|object $listener The listener.
  122. * @param mixed $default The default value to return if the listener doesn't exist.
  123. *
  124. * @return mixed The listener priority if it exists, null otherwise.
  125. *
  126. * @since 1.0
  127. */
  128. public function getPriority($listener, $default = null)
  129. {
  130. if ($this->storage->contains($listener))
  131. {
  132. return $this->storage[$listener][0];
  133. }
  134. return $default;
  135. }
  136. /**
  137. * Get all listeners contained in this queue, sorted according to their priority.
  138. *
  139. * @return object[] An array of listeners.
  140. *
  141. * @since 1.0
  142. */
  143. public function getAll()
  144. {
  145. $listeners = array();
  146. // Get a clone of the queue.
  147. $queue = $this->getIterator();
  148. foreach ($queue as $listener)
  149. {
  150. $listeners[] = $listener;
  151. }
  152. return $listeners;
  153. }
  154. /**
  155. * Get the inner queue with its cursor on top of the heap.
  156. *
  157. * @return SplPriorityQueue The inner queue.
  158. *
  159. * @since 1.0
  160. */
  161. public function getIterator()
  162. {
  163. // SplPriorityQueue queue is a heap.
  164. $queue = clone $this->queue;
  165. if (!$queue->isEmpty())
  166. {
  167. $queue->top();
  168. }
  169. return $queue;
  170. }
  171. /**
  172. * Count the number of listeners in the queue.
  173. *
  174. * @return integer The number of listeners in the queue.
  175. *
  176. * @since 1.0
  177. */
  178. public function count()
  179. {
  180. return count($this->queue);
  181. }
  182. }