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