1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\SM\Event; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
6
|
|
|
use Symfony\Component\EventDispatcher\Event; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
|
10
|
|
|
class Dispatcher implements EventDispatcherInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
14
|
|
|
*/ |
15
|
|
|
protected $dispatcher; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $events = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
24
|
|
|
*/ |
25
|
|
|
public function __construct(DispatcherContract $dispatcher) |
26
|
|
|
{ |
27
|
|
|
$this->dispatcher = $dispatcher; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function dispatch($eventName, Event $event = null) |
34
|
|
|
{ |
35
|
|
|
if (is_null($event)) { |
36
|
|
|
$event = new Event(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->dispatcher->fire($eventName, $event); |
40
|
|
|
|
41
|
|
|
return $event; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
|
|
public function addListener($eventName, $listener, $priority = 0) |
48
|
|
|
{ |
49
|
|
|
$this->events[] = $eventName; |
50
|
|
|
|
51
|
|
|
$this->dispatcher->listen($eventName, $listener, $priority); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
58
|
|
|
{ |
59
|
|
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { |
60
|
|
|
if (is_string($params)) { |
61
|
|
|
$this->addListener($eventName, [$subscriber, $params]); |
62
|
|
|
} elseif (is_string($params[0])) { |
63
|
|
|
$this->addListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0); |
64
|
|
|
} else { |
65
|
|
|
foreach ($params as $listener) { |
66
|
|
|
$this->addListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function removeListener($eventName, $listener) |
76
|
|
|
{ |
77
|
|
|
$listeners = $this->getListeners($eventName); |
78
|
|
|
|
79
|
|
|
$this->dispatcher->forget($eventName); |
80
|
|
|
|
81
|
|
|
foreach ($listeners as $l) { |
82
|
|
|
if ($l !== $listener) { |
83
|
|
|
$this->addListener($eventName, $l); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber) |
92
|
|
|
{ |
93
|
|
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { |
94
|
|
|
if (is_array($params) && is_array($params[0])) { |
95
|
|
|
foreach ($params as $listener) { |
96
|
|
|
$this->removeListener($eventName, [$subscriber, $listener[0]]); |
97
|
|
|
} |
98
|
|
|
} else { |
99
|
|
|
$this->removeListener($eventName, [$subscriber, is_string($params) ? $params : $params[0]]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
|
|
public function getListeners($eventName = null) |
108
|
|
|
{ |
109
|
|
|
if (! is_null($eventName)) { |
110
|
|
|
return $this->dispatcher->getListeners($eventName); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$sorted = []; |
114
|
|
|
|
115
|
|
|
foreach ($this->events as $eventName) { |
116
|
|
|
$sorted = array_merge($sorted, $this->dispatcher->getListeners($eventName)); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return array_filter($sorted); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function getListenerPriority($eventName, $listener) |
126
|
|
|
{ |
127
|
|
|
return array_search($listener, $this->getListeners($eventName), true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
|
|
public function hasListeners($eventName = null) |
134
|
|
|
{ |
135
|
|
|
return (bool) count($this->getListeners($eventName)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.