1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\EventDispatcher; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
6
|
|
|
use BrainExe\Annotations\Annotations\Service; |
7
|
|
|
use BrainExe\Core\Websockets\WebSocketEvent; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; |
11
|
|
|
use Symfony\Component\EventDispatcher\Event; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Service("EventDispatcher", public=false) |
16
|
|
|
* @api |
17
|
|
|
*/ |
18
|
|
|
class EventDispatcher extends ContainerAwareEventDispatcher |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EventDispatcherInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $catchall = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @Inject({"@service_container"}) |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
*/ |
30
|
9 |
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
9 |
|
parent::__construct($container); |
33
|
9 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param EventDispatcherInterface $dispatcher |
37
|
|
|
*/ |
38
|
1 |
|
public function addCatchall(EventDispatcherInterface $dispatcher) |
39
|
|
|
{ |
40
|
1 |
|
$this->catchall[] = $dispatcher; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $eventName |
45
|
|
|
* @param Event $event |
46
|
|
|
* @return Event |
47
|
|
|
*/ |
48
|
1 |
|
public function dispatch($eventName, Event $event = null) |
49
|
|
|
{ |
50
|
1 |
|
if (empty($event)) { |
51
|
1 |
|
throw new RuntimeException('You have to pass an Event into EventDispatcher::dispatch'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($this->catchall as $dispatcher) { |
55
|
|
|
$dispatcher->dispatch($eventName, $event); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return parent::dispatch($eventName, $event); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param AbstractEvent $event |
63
|
|
|
*/ |
64
|
4 |
|
public function dispatchEvent(AbstractEvent $event) |
65
|
|
|
{ |
66
|
4 |
|
$this->dispatch($event->getEventName(), $event); |
67
|
4 |
|
if ($event instanceof PushViaWebsocket) { |
68
|
|
|
/** @var AbstractEvent $event */ |
69
|
1 |
|
$this->dispatchAsWebsocketEvent($event); |
70
|
|
|
} |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param AbstractEvent $event |
75
|
|
|
* @param int|null $timestamp |
76
|
|
|
*/ |
77
|
2 |
|
public function dispatchInBackground(AbstractEvent $event, int $timestamp = 0) |
78
|
|
|
{ |
79
|
2 |
|
if ($timestamp) { |
80
|
1 |
|
$wrapper = new DelayedEvent($event, $timestamp); |
81
|
|
|
} else { |
82
|
1 |
|
$wrapper = new BackgroundEvent($event); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$this->dispatchEvent($wrapper); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param AbstractEvent $event |
90
|
|
|
*/ |
91
|
1 |
|
private function dispatchAsWebsocketEvent(AbstractEvent $event) |
92
|
|
|
{ |
93
|
1 |
|
$wrappedEvent = new WebSocketEvent($event); |
94
|
|
|
|
95
|
1 |
|
$this->dispatch($wrappedEvent->getEventName(), $wrappedEvent); |
96
|
1 |
|
} |
97
|
|
|
} |
98
|
|
|
|