PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
HandlerList.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 
22 namespace pocketmine\event;
23 
26 
28 
32  private $handlers = null;
33 
37  private $handlerSlots = [];
38 
42  private static $allLists = [];
43 
44  public static function bakeAll(){
45  foreach(self::$allLists as $h){
46  $h->bake();
47  }
48  }
49 
56  public static function unregisterAll($object = null){
57  if($object instanceof Listener or $object instanceof Plugin){
58  foreach(self::$allLists as $h){
59  $h->unregister($object);
60  }
61  }else{
62  foreach(self::$allLists as $h){
63  foreach($h->handlerSlots as $key => $list){
64  $h->handlerSlots[$key] = [];
65  }
66  $h->handlers = null;
67  }
68  }
69  }
70 
71  public function __construct(){
72  $this->handlerSlots = [
74  EventPriority::LOW => [],
76  EventPriority::HIGH => [],
79  ];
80  self::$allLists[] = $this;
81  }
82 
88  public function register(RegisteredListener $listener){
89  if($listener->getPriority() < EventPriority::MONITOR or $listener->getPriority() > EventPriority::LOWEST){
90  return;
91  }
92  if(isset($this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)])){
93  throw new \InvalidStateException("This listener is already registered to priority " . $listener->getPriority());
94  }
95  $this->handlers = null;
96  $this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)] = $listener;
97  }
98 
102  public function registerAll(array $listeners){
103  foreach($listeners as $listener){
104  $this->register($listener);
105  }
106  }
107 
111  public function unregister($object){
112  if($object instanceof Plugin or $object instanceof Listener){
113  $changed = false;
114  foreach($this->handlerSlots as $priority => $list){
115  foreach($list as $hash => $listener){
116  if(($object instanceof Plugin and $listener->getPlugin() === $object)
117  or ($object instanceof Listener and $listener->getListener() === $object)
118  ){
119  unset($this->handlerSlots[$priority][$hash]);
120  $changed = true;
121  }
122  }
123  }
124  if($changed === true){
125  $this->handlers = null;
126  }
127  }elseif($object instanceof RegisteredListener){
128  if(isset($this->handlerSlots[$object->getPriority()][spl_object_hash($object)])){
129  unset($this->handlerSlots[$object->getPriority()][spl_object_hash($object)]);
130  $this->handlers = null;
131  }
132  }
133  }
134 
135  public function bake(){
136  if($this->handlers !== null){
137  return;
138  }
139  $entries = [];
140  foreach($this->handlerSlots as $list){
141  foreach($list as $hash => $listener){
142  $entries[$hash] = $listener;
143  }
144  }
145  $this->handlers = $entries;
146  }
147 
153  public function getRegisteredListeners($plugin = null){
154  if($plugin instanceof Plugin){
155  $listeners = [];
156  foreach($this->getRegisteredListeners(null) as $hash => $listener){
157  if($listener->getPlugin() === $plugin){
158  $listeners[$hash] = $plugin;
159  }
160  }
161 
162  return $listeners;
163  }else{
164  while(($handlers = $this->handlers) === null){
165  $this->bake();
166  }
167 
168  return $handlers;
169  }
170  }
171 
175  public static function getHandlerLists(){
176  return self::$allLists;
177  }
178 
179 }
getRegisteredListeners($plugin=null)
registerAll(array $listeners)
static unregisterAll($object=null)
Definition: HandlerList.php:56