|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Test\Support\EventDispatcher; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Psr\EventDispatcher\StoppableEventInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function get_class; |
|
12
|
|
|
|
|
13
|
|
|
final class SimpleEventDispatcher implements EventDispatcherInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array<int, Closure> */ |
|
16
|
|
|
private array $listeners; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var object[] |
|
20
|
|
|
* @psalm-var list<object> |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $events = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Closure ...$listeners Functions that will handle each event. |
|
26
|
|
|
*/ |
|
27
|
18 |
|
public function __construct(Closure ...$listeners) |
|
28
|
|
|
{ |
|
29
|
18 |
|
$this->listeners = $listeners; |
|
30
|
18 |
|
} |
|
31
|
|
|
|
|
32
|
16 |
|
public function dispatch(object $event): object |
|
33
|
|
|
{ |
|
34
|
16 |
|
$this->events[] = $event; |
|
35
|
16 |
|
foreach ($this->listeners as $listener) { |
|
36
|
4 |
|
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { |
|
37
|
2 |
|
return $event; |
|
38
|
|
|
} |
|
39
|
3 |
|
$listener($event); |
|
40
|
|
|
} |
|
41
|
13 |
|
return $event; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return object[] |
|
46
|
|
|
* @psalm-return list<object> |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function getEvents(): array |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->events; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @psalm-return list<class-string> |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function getEventClasses(): array |
|
57
|
|
|
{ |
|
58
|
1 |
|
return array_map('\get_class', $this->events); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function clearEvents(): void |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->events = []; |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
public function isObjectTriggered(object $event, int $times = null): bool |
|
67
|
|
|
{ |
|
68
|
3 |
|
return $this->processBoolResult(static fn (object $e): bool => $e === $event, $times); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
public function isClassTriggered(string $class, int $times = null): bool |
|
72
|
|
|
{ |
|
73
|
4 |
|
return $this->processBoolResult(static fn (object $event): bool => get_class($event) === $class, $times); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function isInstanceOfTriggered(string $class, int $times = null): bool |
|
77
|
|
|
{ |
|
78
|
3 |
|
return $this->processBoolResult(static fn (object $event): bool => $event instanceof $class, $times); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
10 |
|
private function processBoolResult(Closure $closure, ?int $times): bool |
|
82
|
|
|
{ |
|
83
|
10 |
|
if ($times < 0) { |
|
84
|
1 |
|
throw new \InvalidArgumentException('The $times argument cannot be less than zero.'); |
|
85
|
|
|
} |
|
86
|
9 |
|
if ($times === null) { |
|
87
|
3 |
|
return $this->hasEvent($closure); |
|
88
|
|
|
} |
|
89
|
6 |
|
return $times === $this->calcEvent($closure); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
private function hasEvent(Closure $closure): bool |
|
93
|
|
|
{ |
|
94
|
3 |
|
foreach ($this->events as $event) { |
|
95
|
3 |
|
if ($closure($event)) { |
|
96
|
3 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
3 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
6 |
|
private function calcEvent(Closure $closure): int |
|
103
|
|
|
{ |
|
104
|
6 |
|
$count = 0; |
|
105
|
6 |
|
foreach ($this->events as $event) { |
|
106
|
6 |
|
if ($closure($event)) { |
|
107
|
6 |
|
++$count; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
6 |
|
return $count; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|