1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Event; |
3
|
|
|
|
4
|
|
|
use Interop\Container\ContainerInterface; |
5
|
|
|
use Wandu\Q\Queue; |
6
|
|
|
|
7
|
|
|
class Dispatcher implements DispatcherInterface |
8
|
|
|
{ |
9
|
|
|
/** @var \Interop\Container\ContainerInterface */ |
10
|
|
|
protected $container; |
11
|
|
|
|
12
|
|
|
/** @var array|\Wandu\Event\ListenerInterface[][]|callable[][] */ |
13
|
|
|
protected $listeners = []; |
14
|
|
|
|
15
|
20 |
|
public function __construct(ContainerInterface $container) |
16
|
|
|
{ |
17
|
20 |
|
$this->container = $container; |
18
|
20 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
6 |
|
public function on(string $event, $listener) |
24
|
|
|
{ |
25
|
6 |
|
if (!array_key_exists($event, $this->listeners)) { |
26
|
6 |
|
$this->listeners[$event] = []; |
27
|
|
|
} |
28
|
6 |
|
$this->listeners[$event][] = $listener; |
29
|
6 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function off(string $event, $listener = null) |
35
|
|
|
{ |
36
|
|
|
if ($listener) { |
|
|
|
|
37
|
|
|
// @todo |
38
|
|
|
} else { |
39
|
|
|
$this->listeners[$event] = []; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
20 |
|
public function trigger(EventInterface $event) |
47
|
|
|
{ |
48
|
20 |
|
if (!count($this->listeners)) { |
49
|
18 |
|
return; |
50
|
|
|
} |
51
|
6 |
|
if ($event instanceof ViaQueue) { |
52
|
1 |
|
if (!$this->container->has(Queue::class)) { |
53
|
|
|
// @todo fix error message |
54
|
|
|
throw new \InvalidArgumentException('Cannot load queue.'); |
55
|
|
|
} |
56
|
1 |
|
$this->container->get(Queue::class)->enqueue([ |
57
|
1 |
|
'method' => 'event:execute', |
58
|
1 |
|
'event' => $event, |
59
|
|
|
]); |
60
|
|
|
} else { |
61
|
5 |
|
$this->executeListeners($event); |
62
|
|
|
} |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Wandu\Event\EventInterface $event |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
5 |
|
public function executeListeners(EventInterface $event) |
70
|
|
|
{ |
71
|
5 |
|
$executedListeners = []; |
72
|
5 |
|
$eventName = get_class($event); |
73
|
5 |
|
if (!isset($this->listeners[$eventName])) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
5 |
|
foreach ($this->listeners[$eventName] as $listener) { |
77
|
5 |
|
if (is_callable($listener)) { |
78
|
|
|
call_user_func($listener, $event); |
79
|
|
|
$executedListeners[] = 'callable'; |
80
|
5 |
|
} elseif (is_string($listener)) { |
81
|
1 |
|
$this->container->get($listener)->call($event); |
82
|
1 |
|
$executedListeners[] = $listener; |
83
|
|
|
} elseif ($listener instanceof ListenerInterface) { |
84
|
4 |
|
$listener->call($event); |
85
|
5 |
|
$executedListeners[] = get_class($listener); |
86
|
|
|
} |
87
|
|
|
} |
88
|
5 |
|
return $executedListeners; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.