[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Event Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Event; 10 11 /** 12 * A class containing an inner listeners priority queue that can be iterated multiple times. 13 * 14 * @since 1.0 15 * @internal 16 */ 17 final class ListenersPriorityQueue implements \IteratorAggregate, \Countable 18 { 19 /** 20 * The listeners for an event. 21 * 22 * @var array 23 * @since 2.0.0 24 */ 25 private $listeners = []; 26 27 /** 28 * Add a listener with the given priority only if not already present. 29 * 30 * @param callable $callback A callable function acting as an event listener. 31 * @param integer $priority The listener priority. 32 * 33 * @return $this 34 * 35 * @since 1.0 36 */ 37 public function add(callable $callback, int $priority): self 38 { 39 $this->listeners[$priority][] = $callback; 40 41 return $this; 42 } 43 44 /** 45 * Remove a listener from the queue. 46 * 47 * @param callable $callback A callable function acting as an event listener. 48 * 49 * @return $this 50 * 51 * @since 1.0 52 */ 53 public function remove(callable $callback): self 54 { 55 foreach ($this->listeners as $priority => $listeners) 56 { 57 if (($key = array_search($callback, $listeners, true)) !== false) 58 { 59 unset($this->listeners[$priority][$key]); 60 } 61 } 62 63 return $this; 64 } 65 66 /** 67 * Tell if the listener exists in the queue. 68 * 69 * @param callable $callback A callable function acting as an event listener. 70 * 71 * @return boolean True if it exists, false otherwise. 72 * 73 * @since 1.0 74 */ 75 public function has(callable $callback): bool 76 { 77 foreach ($this->listeners as $priority => $listeners) 78 { 79 if (($key = array_search($callback, $listeners, true)) !== false) 80 { 81 return true; 82 } 83 } 84 85 return false; 86 } 87 88 /** 89 * Get the priority of the given listener. 90 * 91 * @param callable $callback A callable function acting as an event listener. 92 * @param mixed $default The default value to return if the listener doesn't exist. 93 * 94 * @return mixed The listener priority if it exists or the specified default value 95 * 96 * @since 1.0 97 */ 98 public function getPriority(callable $callback, $default = null) 99 { 100 foreach ($this->listeners as $priority => $listeners) 101 { 102 if (($key = array_search($callback, $listeners, true)) !== false) 103 { 104 return $priority; 105 } 106 } 107 108 return $default; 109 } 110 111 /** 112 * Get all listeners contained in this queue, sorted according to their priority. 113 * 114 * @return callable[] An array of listeners. 115 * 116 * @since 1.0 117 */ 118 public function getAll(): array 119 { 120 if (empty($this->listeners)) 121 { 122 return []; 123 } 124 125 krsort($this->listeners); 126 127 return \call_user_func_array('array_merge', $this->listeners); 128 } 129 130 /** 131 * Get the priority queue. 132 * 133 * @return \ArrayIterator 134 * 135 * @since 1.0 136 */ 137 #[\ReturnTypeWillChange] 138 public function getIterator() 139 { 140 return new \ArrayIterator($this->getAll()); 141 } 142 143 /** 144 * Count the number of listeners in the queue. 145 * 146 * @return integer The number of listeners in the queue. 147 * 148 * @since 1.0 149 */ 150 #[\ReturnTypeWillChange] 151 public function count() 152 { 153 $count = 0; 154 155 foreach ($this->listeners as $priority) 156 { 157 $count += \count($priority); 158 } 159 160 return $count; 161 } 162 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |