37 private array $handlerSlots = [];
40 private array $affectedHandlerCaches = [];
46 private string $class,
50 for($list = $this; $list !==
null; $list = $list->parentList){
51 $list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache;
59 if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){
60 throw new \InvalidArgumentException(
"This listener is already registered to priority {$listener->getPriority()} of event {$this->class}");
62 $this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener;
63 $this->invalidateAffectedCaches();
70 foreach($listeners as $listener){
71 $this->
register($listener);
73 $this->invalidateAffectedCaches();
76 public function unregister(RegisteredListener|Plugin|Listener $object) : void{
77 if($object instanceof Plugin || $object instanceof Listener){
78 foreach($this->handlerSlots as $priority => $list){
79 foreach($list as $hash => $listener){
80 if(($object instanceof Plugin && $listener->getPlugin() === $object)
81 || ($object instanceof Listener && (
new \ReflectionFunction($listener->getHandler()))->getClosureThis() === $object)
83 unset($this->handlerSlots[$priority][$hash]);
88 unset($this->handlerSlots[$object->getPriority()][spl_object_id($object)]);
90 $this->invalidateAffectedCaches();
93 public function clear() : void{
94 $this->handlerSlots = [];
95 $this->invalidateAffectedCaches();
102 return $this->handlerSlots[$priority] ?? [];
106 return $this->parentList;
112 private function invalidateAffectedCaches() : void{
113 foreach($this->affectedHandlerCaches as $cache){
123 if($this->handlerCache->list !== null){
124 return $this->handlerCache->list;
128 for($currentList = $this; $currentList !==
null; $currentList = $currentList->parentList){
129 $handlerLists[] = $currentList;
132 $listenersByPriority = [];
133 foreach($handlerLists as $currentList){
134 foreach($currentList->handlerSlots as $priority => $listeners){
135 $listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $listeners);
140 krsort($listenersByPriority, SORT_NUMERIC);
142 return $this->handlerCache->list = array_merge(...$listenersByPriority);