34 private array $handlerSlots = [];
37 private array $affectedHandlerCaches = [];
43 private string $class,
47 for($list = $this; $list !==
null; $list = $list->parentList){
48 $list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache;
56 if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){
57 throw new \InvalidArgumentException(
"This listener is already registered to priority {$listener->getPriority()} of event {$this->class}");
59 $this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener;
60 $this->invalidateAffectedCaches();
67 foreach($listeners as $listener){
68 $this->
register($listener);
70 $this->invalidateAffectedCaches();
73 public function unregister(RegisteredListener|Plugin|Listener $object) : void{
74 if($object instanceof Plugin || $object instanceof Listener){
75 foreach($this->handlerSlots as $priority => $list){
76 foreach($list as $hash => $listener){
77 if(($object instanceof Plugin && $listener->getPlugin() === $object)
78 || ($object instanceof Listener && (
new \ReflectionFunction($listener->getHandler()))->getClosureThis() === $object)
80 unset($this->handlerSlots[$priority][$hash]);
85 unset($this->handlerSlots[$object->getPriority()][spl_object_id($object)]);
87 $this->invalidateAffectedCaches();
90 public function clear() : void{
91 $this->handlerSlots = [];
92 $this->invalidateAffectedCaches();
99 return $this->handlerSlots[$priority] ?? [];
103 return $this->parentList;
109 private function invalidateAffectedCaches() : void{
110 foreach($this->affectedHandlerCaches as $cache){
120 if($this->handlerCache->list !== null){
121 return $this->handlerCache->list;
125 for($currentList = $this; $currentList !==
null; $currentList = $currentList->parentList){
126 $handlerLists[] = $currentList;
129 $listenersByPriority = [];
130 foreach($handlerLists as $currentList){
131 foreach($currentList->handlerSlots as $priority => $listeners){
132 $listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $listeners);
137 krsort($listenersByPriority, SORT_NUMERIC);
139 return $this->handlerCache->list = array_merge(...$listenersByPriority);