PocketMine-MP 5.37.4 git-07e225b0bd0d389de8a3d035fbd0ae8730a06053
Loading...
Searching...
No Matches
HandlerListManager.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\event;
25
28
30
31 private static ?self $globalInstance = null;
32
33 public static function global() : self{
34 return self::$globalInstance ?? (self::$globalInstance = new self());
35 }
36
41 private array $allLists = [];
46 private array $handlerCaches = [];
47
53 public function unregisterAll(RegisteredListener|Plugin|Listener|null $object = null) : void{
54 if($object instanceof Listener || $object instanceof Plugin || $object instanceof RegisteredListener){
55 foreach($this->allLists as $h){
56 $h->unregister($object);
57 }
58 }else{
59 foreach($this->allLists as $h){
60 $h->clear();
61 }
62 }
63 }
64
68 private static function isValidClass(\ReflectionClass $class) : bool{
69 $tags = Utils::parseDocComment((string) $class->getDocComment());
70 return !$class->isAbstract() || isset($tags["allowHandle"]);
71 }
72
79 private static function resolveNearestHandleableParent(\ReflectionClass $class) : ?\ReflectionClass{
80 for($parent = $class->getParentClass(); $parent !== false; $parent = $parent->getParentClass()){
81 if(self::isValidClass($parent)){
82 return $parent;
83 }
84 //NOOP
85 }
86 return null;
87 }
88
102 public function getListFor(string $event) : HandlerList{
103 if(isset($this->allLists[$event])){
105 $list = $this->allLists[$event];
106 return $list;
107 }
108
109 $class = new \ReflectionClass($event);
110 if(!self::isValidClass($class)){
111 throw new \InvalidArgumentException("Event must be non-abstract or have the @allowHandle annotation");
112 }
113
114 $parent = self::resolveNearestHandleableParent($class);
115 $cache = new RegisteredListenerCache();
116 $this->handlerCaches[$event] = $cache;
117
118 $parentList = $parent !== null ? $this->getListFor($parent->getName()) : null;
119
120 //TODO: this @var is needed because we can't stop parentList from screwing up the inference for HandlerList<TEvent>
122 $list = new HandlerList(
123 $event,
124 parentList: $parentList,
125 handlerCache: $cache
126 );
127 $this->allLists[$event] = $list;
128 return $list;
129 }
130
138 public function getHandlersFor(string $event) : array{
140 $cache = $this->handlerCaches[$event] ?? null;
141 //getListFor() will populate the cache for the next call
142 return $cache->list ?? $this->getListFor($event)->getListenerList();
143 }
144
149 public function getAll() : array{
150 return $this->allLists;
151 }
152}
unregisterAll(RegisteredListener|Plugin|Listener|null $object=null)