1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\EventDispatcher\Factory; |
6
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\EventDispatcher; |
8
|
|
|
use Arp\EventDispatcher\Factory\Listener\ListenerProviderFactory; |
9
|
|
|
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface; |
10
|
|
|
use Arp\Factory\Exception\FactoryException; |
11
|
|
|
use Arp\Factory\FactoryInterface; |
12
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Psr\EventDispatcher\ListenerProviderInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Alex Patterson <[email protected]> |
17
|
|
|
* @package Arp\EventDispatcher\Factory |
18
|
|
|
*/ |
19
|
|
|
class EventDispatcherFactory implements FactoryInterface |
20
|
|
|
{ |
21
|
|
|
use ListenerRegistrationTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FactoryInterface|null |
25
|
|
|
*/ |
26
|
|
|
private $listenerProviderFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param FactoryInterface|null $listenerProviderFactory |
30
|
|
|
*/ |
31
|
|
|
public function __construct(?FactoryInterface $listenerProviderFactory = null) |
32
|
|
|
{ |
33
|
|
|
$this->listenerProviderFactory = $listenerProviderFactory; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new event dispatcher. |
38
|
|
|
* |
39
|
|
|
* @param array $config |
40
|
|
|
* |
41
|
|
|
* @return EventDispatcherInterface |
42
|
|
|
* |
43
|
|
|
* @throws FactoryException |
44
|
|
|
*/ |
45
|
|
|
public function create(array $config = []): EventDispatcherInterface |
46
|
|
|
{ |
47
|
|
|
$eventDispatcher = new EventDispatcher( |
48
|
|
|
$this->createListenerProvider($config['listener_provider'] ?? []) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$listeners = $config['listeners'] ?? []; |
52
|
|
|
if (!empty($listeners) && $eventDispatcher instanceof AddableListenerProviderInterface) { |
|
|
|
|
53
|
|
|
$this->registerEventListeners($eventDispatcher, $listeners); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $eventDispatcher; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new listener provider with the provided configuration options. |
61
|
|
|
* |
62
|
|
|
* @param ListenerProviderInterface|array $config |
63
|
|
|
* |
64
|
|
|
* @return AddableListenerProviderInterface |
65
|
|
|
* |
66
|
|
|
* @throws FactoryException |
67
|
|
|
*/ |
68
|
|
|
private function createListenerProvider($config): AddableListenerProviderInterface |
69
|
|
|
{ |
70
|
|
|
if ($config instanceof AddableListenerProviderInterface) { |
71
|
|
|
return $config; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!is_array($config)) { |
75
|
|
|
throw new FactoryException( |
76
|
|
|
sprintf( |
77
|
|
|
'The listener provider configuration must be of type \'array\'; \'%s\' provided in \'%s\'', |
78
|
|
|
gettype($config), |
79
|
|
|
__METHOD__ |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (null === $this->listenerProviderFactory) { |
85
|
|
|
$this->listenerProviderFactory = new ListenerProviderFactory(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var AddableListenerProviderInterface $listenerProvider */ |
89
|
|
|
return $this->listenerProviderFactory->create($config); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|