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