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