1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @Author : a.zinovyev |
4
|
|
|
* @Package: emittr |
5
|
|
|
* @License: http://www.opensource.org/licenses/mit-license.php |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace xobotyi\emittr\Traits; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use xobotyi\emittr\Event; |
12
|
|
|
use xobotyi\emittr\Interfaces\EventEmitter; |
13
|
|
|
use xobotyi\emittr\Interfaces\GlobalEventHandler; |
14
|
|
|
|
15
|
|
|
trait EventEmitterStatic |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EventEmitter; |
19
|
|
|
*/ |
20
|
|
|
private static $eventEmitter; |
21
|
|
|
|
22
|
|
|
public static function getEventEmitter() :EventEmitter { |
23
|
|
|
return self::$eventEmitter ?: self::setEventEmitter(new \xobotyi\emittr\EventEmitter()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function setEventEmitter(EventEmitter $eventEmitter) :EventEmitter { |
27
|
|
|
return self::$eventEmitter = $eventEmitter; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function emit(string $eventName, $payload = null) :void { |
31
|
|
|
self::getEventEmitter()->emit(new Event($eventName, $payload, get_called_class(), null)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function on(string $eventName, $callback) :void { |
35
|
|
|
self::getEventEmitter()->on($eventName, $callback); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function once(string $eventName, $callback) :void { |
39
|
|
|
self::getEventEmitter()->once($eventName, $callback); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function prependListener(string $eventName, $callback) :void { |
43
|
|
|
self::getEventEmitter()->prependListener($eventName, $callback); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function prependOnceListener(string $eventName, $callback) :void { |
47
|
|
|
self::getEventEmitter()->prependOnceListener($eventName, $callback); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function off(string $eventName, $callback) { |
51
|
|
|
self::getEventEmitter()->off($eventName, $callback); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function removeAllListeners(?string $eventName = null) :void { |
55
|
|
|
self::getEventEmitter()->removeAllListeners($eventName); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function getListeners(?string $eventName = null) :array { |
59
|
|
|
return self::getEventEmitter()->getListeners($eventName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getMaxListenersCount() :int { |
63
|
|
|
return self::getEventEmitter()->getMaxListenersCount(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function setMaxListenersCount(int $maxListenersCount) :void { |
67
|
|
|
self::getEventEmitter()->setMaxListenersCount($maxListenersCount); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function getGlobalEmitter() :GlobalEventHandler { |
71
|
|
|
return self::getEventEmitter()->getGlobalEmitter(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function setGlobalEmitter(GlobalEventHandler $emitterGlobal) :void { |
75
|
|
|
self::getEventEmitter()->setGlobalEmitter($emitterGlobal); |
76
|
|
|
} |
77
|
|
|
} |