|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Events\Bootloader; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Spiral\Boot\AbstractKernel; |
|
10
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
11
|
|
|
use Spiral\Boot\FinalizerInterface; |
|
12
|
|
|
use Spiral\Bootloader\Attributes\AttributesBootloader; |
|
13
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
14
|
|
|
use Spiral\Config\Patch\Append; |
|
15
|
|
|
use Spiral\Core\Container; |
|
16
|
|
|
use Spiral\Core\Container\Autowire; |
|
17
|
|
|
use Spiral\Core\CoreInterceptorInterface; |
|
18
|
|
|
use Spiral\Core\FactoryInterface; |
|
19
|
|
|
use Spiral\Core\InterceptableCore; |
|
20
|
|
|
use Spiral\Events\AutowireListenerFactory; |
|
21
|
|
|
use Spiral\Events\Config\EventsConfig; |
|
|
|
|
|
|
22
|
|
|
use Spiral\Events\EventDispatcher; |
|
23
|
|
|
use Spiral\Events\EventDispatcherAwareInterface; |
|
24
|
|
|
use Spiral\Events\Interceptor\Core; |
|
25
|
|
|
use Spiral\Events\ListenerFactoryInterface; |
|
26
|
|
|
use Spiral\Events\ListenerProcessorRegistry; |
|
27
|
|
|
use Spiral\Events\Processor\AttributeProcessor; |
|
28
|
|
|
use Spiral\Events\Processor\ConfigProcessor; |
|
29
|
|
|
use Spiral\Events\Processor\ProcessorInterface; |
|
30
|
|
|
use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @psalm-import-type TInterceptor from EventsConfig |
|
34
|
|
|
*/ |
|
35
|
|
|
final class EventsBootloader extends Bootloader |
|
36
|
|
|
{ |
|
37
|
|
|
protected const DEPENDENCIES = [ |
|
38
|
|
|
TokenizerListenerBootloader::class, |
|
39
|
|
|
AttributesBootloader::class, |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
protected const SINGLETONS = [ |
|
43
|
|
|
ListenerFactoryInterface::class => AutowireListenerFactory::class, |
|
44
|
|
|
ListenerProcessorRegistry::class => ListenerProcessorRegistry::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
248 |
|
public function __construct( |
|
48
|
|
|
private readonly ConfiguratorInterface $configs |
|
49
|
|
|
) { |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
248 |
|
public function init(): void |
|
53
|
|
|
{ |
|
54
|
248 |
|
$this->configs->setDefaults(EventsConfig::CONFIG, [ |
|
55
|
248 |
|
'listeners' => [], |
|
56
|
|
|
'processors' => [ |
|
57
|
|
|
AttributeProcessor::class, |
|
58
|
|
|
ConfigProcessor::class, |
|
59
|
|
|
], |
|
60
|
|
|
'interceptors' => [], |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
248 |
|
public function boot( |
|
65
|
|
|
Container $container, |
|
66
|
|
|
FactoryInterface $factory, |
|
67
|
|
|
EventsConfig $config, |
|
68
|
|
|
AbstractKernel $kernel, |
|
69
|
|
|
ListenerProcessorRegistry $registry, |
|
70
|
|
|
FinalizerInterface $finalizer, |
|
71
|
|
|
?EventDispatcherInterface $eventDispatcher = null |
|
72
|
|
|
): void { |
|
73
|
248 |
|
if ($eventDispatcher !== null) { |
|
74
|
6 |
|
$this->initEventDispatcher(new Core($eventDispatcher), $config, $container, $factory); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
248 |
|
foreach ($config->getProcessors() as $processor) { |
|
78
|
248 |
|
$processor = $this->autowire($processor, $container, $factory); |
|
79
|
|
|
|
|
80
|
248 |
|
\assert($processor instanceof ProcessorInterface); |
|
81
|
248 |
|
$registry->addProcessor($processor); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
248 |
|
$kernel->bootstrapped(static function () use ($registry): void { |
|
85
|
248 |
|
$registry->process(); |
|
86
|
|
|
}); |
|
87
|
|
|
|
|
88
|
248 |
|
if ($finalizer instanceof EventDispatcherAwareInterface && $eventDispatcher !== null) { |
|
89
|
2 |
|
$finalizer->setEventDispatcher($eventDispatcher); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param TInterceptor $interceptor |
|
|
|
|
|
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function addInterceptor(string|CoreInterceptorInterface|Container\Autowire $interceptor): void |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->configs->modify(EventsConfig::CONFIG, new Append('interceptors', null, $interceptor)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
private function initEventDispatcher( |
|
102
|
|
|
Core $core, |
|
103
|
|
|
EventsConfig $config, |
|
104
|
|
|
Container $container, |
|
105
|
|
|
FactoryInterface $factory |
|
106
|
|
|
): void { |
|
107
|
6 |
|
$core = new InterceptableCore($core); |
|
108
|
|
|
|
|
109
|
6 |
|
foreach ($config->getInterceptors() as $interceptor) { |
|
110
|
|
|
$interceptor = $this->autowire($interceptor, $container, $factory); |
|
111
|
|
|
|
|
112
|
|
|
\assert($interceptor instanceof CoreInterceptorInterface); |
|
113
|
|
|
$core->addInterceptor($interceptor); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
$container->bindSingleton(EventDispatcherInterface::class, new EventDispatcher($core)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
248 |
|
private function autowire(string|object $id, ContainerInterface $container, FactoryInterface $factory): object |
|
120
|
|
|
{ |
|
121
|
248 |
|
if (\is_string($id)) { |
|
122
|
248 |
|
$id = $container->get($id); |
|
123
|
2 |
|
} elseif ($id instanceof Autowire) { |
|
124
|
1 |
|
$id = $id->resolve($factory); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
248 |
|
return $id; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths