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\Interfaces\EventEmitter; |
12
|
|
|
use xobotyi\emittr\Interfaces\EventEmitterGlobal; |
13
|
|
|
|
14
|
|
|
trait EventEmitterStatic |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EventEmitter; |
18
|
|
|
*/ |
19
|
|
|
private static $eventEmitter; |
20
|
|
|
|
21
|
|
|
public static function getEventEmitter() :EventEmitter { |
22
|
|
|
return self::$eventEmitter ?: self::setEventEmitter(new \xobotyi\emittr\EventEmitter()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function setEventEmitter(EventEmitter $eventEmitter) :EventEmitter { |
26
|
|
|
return self::$eventEmitter = $eventEmitter; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function emit(string $eventName, $payload = null) :void { |
30
|
|
|
self::getEventEmitter()->emit($eventName, $payload); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function on(string $eventName, $callback) :void { |
34
|
|
|
self::getEventEmitter()->on($eventName, $callback); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function once(string $eventName, $callback) :void { |
38
|
|
|
self::getEventEmitter()->once($eventName, $callback); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function prependListener(string $eventName, $callback) :void { |
42
|
|
|
self::getEventEmitter()->prependListener($eventName, $callback); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function prependOnceListener(string $eventName, $callback) :void { |
46
|
|
|
self::getEventEmitter()->prependOnceListener($eventName, $callback); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function off(string $eventName, $callback) { |
50
|
|
|
self::getEventEmitter()->off($eventName, $callback); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function removeAllListeners(?string $eventName = null) :void { |
54
|
|
|
self::getEventEmitter()->removeAllListeners($eventName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function getListeners(?string $eventName = null) :array { |
58
|
|
|
return self::getEventEmitter()->getListeners($eventName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function getMaxListenersCount() :int { |
62
|
|
|
return self::getEventEmitter()->getMaxListenersCount(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function setMaxListenersCount(int $maxListenersCount) :void { |
66
|
|
|
self::getEventEmitter()->setMaxListenersCount($maxListenersCount); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function getGlobalEmitter() :EventEmitterGlobal { |
70
|
|
|
return self::getEventEmitter()->getGlobalEmitter(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function setGlobalEmitter(EventEmitterGlobal $emitterGlobal) :void { |
74
|
|
|
self::getEventEmitter()->setGlobalEmitter($emitterGlobal); |
75
|
|
|
} |
76
|
|
|
} |