PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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
38 private array $allLists = [];
43 private array $handlerCaches = [];
44
49 public function unregisterAll(RegisteredListener|Plugin|Listener|null $object = null) : void{
50 if($object instanceof Listener || $object instanceof Plugin || $object instanceof RegisteredListener){
51 foreach($this->allLists as $h){
52 $h->unregister($object);
53 }
54 }else{
55 foreach($this->allLists as $h){
56 $h->clear();
57 }
58 }
59 }
60
64 private static function isValidClass(\ReflectionClass $class) : bool{
65 $tags = Utils::parseDocComment((string) $class->getDocComment());
66 return !$class->isAbstract() || isset($tags["allowHandle"]);
67 }
68
74 private static function resolveNearestHandleableParent(\ReflectionClass $class) : ?\ReflectionClass{
75 for($parent = $class->getParentClass(); $parent !== false; $parent = $parent->getParentClass()){
76 if(self::isValidClass($parent)){
77 return $parent;
78 }
79 //NOOP
80 }
81 return null;
82 }
83
94 public function getListFor(string $event) : HandlerList{
95 if(isset($this->allLists[$event])){
96 return $this->allLists[$event];
97 }
98
99 $class = new \ReflectionClass($event);
100 if(!self::isValidClass($class)){
101 throw new \InvalidArgumentException("Event must be non-abstract or have the @allowHandle annotation");
102 }
103
104 $parent = self::resolveNearestHandleableParent($class);
105 $cache = new RegisteredListenerCache();
106 $this->handlerCaches[$event] = $cache;
107 return $this->allLists[$event] = new HandlerList(
108 $event,
109 parentList: $parent !== null ? $this->getListFor($parent->getName()) : null,
110 handlerCache: $cache
111 );
112 }
113
119 public function getHandlersFor(string $event) : array{
120 $cache = $this->handlerCaches[$event] ?? null;
121 //getListFor() will populate the cache for the next call
122 return $cache?->list ?? $this->getListFor($event)->getListenerList();
123 }
124
128 public function getAll() : array{
129 return $this->allLists;
130 }
131}
unregisterAll(RegisteredListener|Plugin|Listener|null $object=null)