1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\EventDispatcher\Listener; |
6
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\Listener\Exception\EventListenerException; |
8
|
|
|
use Arp\EventDispatcher\Resolver\EventNameResolver; |
9
|
|
|
use Arp\EventDispatcher\Resolver\EventNameResolverInterface; |
10
|
|
|
use Arp\EventDispatcher\Resolver\Exception\EventNameResolverException; |
11
|
|
|
|
12
|
|
|
class ListenerProvider implements AddableListenerProviderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array<ListenerCollectionInterface> |
16
|
|
|
*/ |
17
|
|
|
protected array $collections = []; |
18
|
|
|
|
19
|
|
|
protected EventNameResolverInterface $eventNameResolver; |
20
|
|
|
|
21
|
|
|
public function __construct(?EventNameResolverInterface $eventNameResolver = null) |
22
|
|
|
{ |
23
|
|
|
$this->eventNameResolver = $eventNameResolver ?? new EventNameResolver(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return iterable<callable>&ListenerCollectionInterface |
28
|
|
|
* |
29
|
|
|
* @throws EventListenerException |
30
|
|
|
*/ |
31
|
|
|
public function getListenersForEvent(object $event): iterable |
32
|
|
|
{ |
33
|
|
|
return $this->getOrCreateListenerCollection($event); |
34
|
|
|
} |
35
|
10 |
|
|
36
|
|
|
/** |
37
|
10 |
|
* @throws EventListenerException |
38
|
|
|
*/ |
39
|
|
|
public function addListenerForEvent(object|string $event, callable $listener, int $priority = 1): void |
40
|
|
|
{ |
41
|
|
|
$this->getOrCreateListenerCollection($event)->addListener($listener, $priority); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws EventListenerException If the $event name cannot be resolved. |
46
|
|
|
*/ |
47
|
|
|
public function addListenersForEvent(object|string $event, iterable $listeners, int $priority = 1): void |
48
|
|
|
{ |
49
|
5 |
|
$this->getOrCreateListenerCollection($event)->addListeners($listeners, $priority); |
50
|
|
|
} |
51
|
5 |
|
|
52
|
|
|
/** |
53
|
|
|
* @param array<callable> $listeners |
54
|
|
|
* |
55
|
|
|
* @return ListenerCollectionInterface<callable> |
56
|
|
|
*/ |
57
|
|
|
protected function createListenerCollection(array $listeners = []): ListenerCollectionInterface |
58
|
|
|
{ |
59
|
|
|
return new ListenerCollection($listeners); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws EventListenerException |
64
|
8 |
|
*/ |
65
|
|
|
private function getOrCreateListenerCollection(string|object $event): ListenerCollectionInterface |
66
|
|
|
{ |
67
|
8 |
|
try { |
68
|
1 |
|
$eventName = $this->eventNameResolver->resolveEventName($event); |
69
|
1 |
|
} catch (EventNameResolverException $e) { |
70
|
1 |
|
throw new EventListenerException( |
71
|
1 |
|
sprintf('Failed to resolve the event name : %s', $e->getMessage()), |
72
|
|
|
$e->getCode(), |
73
|
|
|
$e |
74
|
|
|
); |
75
|
|
|
} |
76
|
7 |
|
|
77
|
7 |
|
if (!isset($this->collections[$eventName])) { |
78
|
|
|
$this->collections[$eventName] = $this->createListenerCollection(); |
79
|
|
|
} |
80
|
7 |
|
|
81
|
|
|
return $this->collections[$eventName]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|