1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\EventDispatcher; |
6
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface; |
8
|
|
|
use Arp\EventDispatcher\Listener\AddListenerAwareInterface; |
9
|
|
|
use Arp\EventDispatcher\Listener\Exception\EventListenerException; |
10
|
|
|
use Psr\EventDispatcher\ListenerProviderInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Alex Patterson <[email protected]> |
14
|
|
|
* @package Arp\EventDispatcher |
15
|
|
|
*/ |
16
|
|
|
final class EventDispatcher extends AbstractEventDispatcher implements AddListenerAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var AddableListenerProviderInterface |
20
|
|
|
*/ |
21
|
|
|
private AddableListenerProviderInterface $listenerProvider; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param AddableListenerProviderInterface $listenerProvider |
25
|
|
|
*/ |
26
|
12 |
|
public function __construct(AddableListenerProviderInterface $listenerProvider) |
27
|
|
|
{ |
28
|
12 |
|
$this->listenerProvider = $listenerProvider; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add a new event listener to the collection. |
33
|
|
|
* |
34
|
|
|
* @param object|string $event The event that should be attached to. |
35
|
|
|
* @param callable $listener The event listener to attach. |
36
|
|
|
* @param int $priority The event priority. |
37
|
|
|
* |
38
|
|
|
* @throws EventListenerException If the event listener cannot be added. |
39
|
|
|
*/ |
40
|
1 |
|
public function addListenerForEvent($event, callable $listener, int $priority = 1): void |
41
|
|
|
{ |
42
|
1 |
|
$this->listenerProvider->addListenerForEvent($event, $listener, $priority); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a collection of event listeners for a single event. |
47
|
|
|
* |
48
|
|
|
* @param object|string $event The event name or instance to attach to. |
49
|
|
|
* @param iterable|callable[] $listeners Collection of listeners to attach. |
50
|
|
|
* @param int $priority Event priority to use for all $listeners. This will default to 1. |
51
|
|
|
* |
52
|
|
|
* @throws EventListenerException If the event listeners cannot be added. |
53
|
|
|
*/ |
54
|
1 |
|
public function addListenersForEvent($event, iterable $listeners, int $priority = 1): void |
55
|
|
|
{ |
56
|
1 |
|
$this->listenerProvider->addListenersForEvent($event, $listeners, $priority); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ListenerProviderInterface |
61
|
|
|
*/ |
62
|
7 |
|
protected function getListenerProvider(): ListenerProviderInterface |
63
|
|
|
{ |
64
|
7 |
|
return $this->listenerProvider; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|