|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Events\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Core\Container\Autowire; |
|
8
|
|
|
use Spiral\Core\CoreInterceptorInterface; |
|
9
|
|
|
use Spiral\Core\InjectableConfig; |
|
10
|
|
|
use Spiral\Events\Processor\ProcessorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @psalm-type TProcessor = ProcessorInterface|class-string<ProcessorInterface>|Autowire<ProcessorInterface> |
|
14
|
|
|
* @psalm-type TListener = class-string|EventListener |
|
15
|
|
|
* @psalm-type TInterceptor = class-string<CoreInterceptorInterface>|CoreInterceptorInterface|Autowire<CoreInterceptorInterface> |
|
16
|
|
|
* @property array{ |
|
17
|
|
|
* processors: TProcessor[], |
|
18
|
|
|
* listeners: array<class-string, TListener[]>, |
|
19
|
|
|
* interceptors: TInterceptor[] |
|
20
|
|
|
* } $config |
|
21
|
|
|
*/ |
|
22
|
|
|
final class EventsConfig extends InjectableConfig |
|
23
|
|
|
{ |
|
24
|
|
|
public const CONFIG = 'events'; |
|
25
|
|
|
|
|
26
|
|
|
protected array $config = [ |
|
27
|
|
|
'processors' => [], |
|
28
|
|
|
'listeners' => [], |
|
29
|
|
|
'interceptors' => [], |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get registered listeners. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array<class-string, EventListener[]> |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
5 |
|
public function getListeners(): array |
|
38
|
|
|
{ |
|
39
|
5 |
|
$listeners = []; |
|
40
|
5 |
|
foreach ($this->config['listeners'] as $event => $eventListeners) { |
|
41
|
4 |
|
$listeners[$event] = \array_map( |
|
42
|
4 |
|
self::normalizeListener(...), |
|
43
|
4 |
|
$eventListeners |
|
44
|
4 |
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
5 |
|
return $listeners; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return TProcessor[] |
|
52
|
|
|
*/ |
|
53
|
293 |
|
public function getProcessors(): array |
|
54
|
|
|
{ |
|
55
|
293 |
|
return $this->config['processors']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return TInterceptor[] |
|
60
|
|
|
*/ |
|
61
|
8 |
|
public function getInterceptors(): array |
|
62
|
|
|
{ |
|
63
|
8 |
|
return $this->config['interceptors']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param TListener $listener |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
4 |
|
private static function normalizeListener(string|EventListener $listener): EventListener |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
4 |
|
return \is_string($listener) ? new EventListener($listener) : $listener; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|