|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
8
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
9
|
|
|
use Spiral\Config\Patch\Append; |
|
10
|
|
|
use Spiral\Core\Attribute\Proxy; |
|
11
|
|
|
use Spiral\Core\Attribute\Singleton; |
|
12
|
|
|
use Spiral\Core\Container\Autowire; |
|
13
|
|
|
use Spiral\Core\FactoryInterface; |
|
14
|
|
|
use Spiral\Core\InvokerInterface; |
|
15
|
|
|
use Spiral\Debug\Config\DebugConfig; |
|
16
|
|
|
use Spiral\Debug\Exception\StateException; |
|
17
|
|
|
use Spiral\Debug\State; |
|
18
|
|
|
use Spiral\Debug\StateCollector\EnvironmentCollector; |
|
19
|
|
|
use Spiral\Debug\StateCollectorInterface; |
|
20
|
|
|
use Spiral\Debug\StateInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @psalm-import-type TCollector from DebugConfig |
|
24
|
|
|
* @psalm-import-type TTag from DebugConfig |
|
25
|
|
|
*/ |
|
26
|
|
|
#[Singleton] |
|
27
|
|
|
final class DebugBootloader extends Bootloader |
|
28
|
|
|
{ |
|
29
|
|
|
protected const SINGLETONS = [ |
|
30
|
|
|
EnvironmentCollector::class => EnvironmentCollector::class, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
protected const BINDINGS = [ |
|
34
|
|
|
StateInterface::class => [self::class, 'state'], |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
357 |
|
public function __construct( |
|
38
|
|
|
private readonly InvokerInterface $invoker, |
|
39
|
|
|
private readonly ConfiguratorInterface $config, |
|
40
|
|
|
) { |
|
41
|
357 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Boot default state collector. |
|
45
|
|
|
*/ |
|
46
|
357 |
|
public function init(): void |
|
47
|
|
|
{ |
|
48
|
357 |
|
$this->initDefaultConfig(); |
|
49
|
357 |
|
$this->addStateCollector(EnvironmentCollector::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param non-empty-string $key |
|
|
|
|
|
|
54
|
|
|
* @param TTag $value |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function addTag(string $key, string|\Stringable|\Closure $value): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->config->modify(DebugConfig::CONFIG, new Append('tags', $key, $value)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @psalm-param TCollector $collector |
|
63
|
|
|
*/ |
|
64
|
357 |
|
public function addStateCollector(string|StateCollectorInterface|Autowire $collector): void |
|
65
|
|
|
{ |
|
66
|
357 |
|
$this->config->modify(DebugConfig::CONFIG, new Append('collectors', null, $collector)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create state and populate it with collectors. |
|
71
|
|
|
*/ |
|
72
|
6 |
|
private function state( |
|
73
|
|
|
#[Proxy] FactoryInterface $factory, |
|
74
|
|
|
DebugConfig $config, |
|
75
|
|
|
): StateInterface { |
|
76
|
6 |
|
$state = new State(); |
|
77
|
|
|
|
|
78
|
6 |
|
foreach ($config->getTags() as $key => $value) { |
|
79
|
3 |
|
if ($value instanceof \Closure) { |
|
80
|
1 |
|
$value = $this->invoker->invoke($value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
if (!\is_string($value) && !$value instanceof \Stringable) { |
|
84
|
1 |
|
throw new StateException( |
|
85
|
1 |
|
\sprintf( |
|
86
|
1 |
|
'Invalid tag value, `string` expected got `%s`', |
|
87
|
1 |
|
\is_object($value) ? $value::class : \gettype($value), |
|
88
|
1 |
|
), |
|
89
|
1 |
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
$state->setTag((string)$key, (string)$value); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
5 |
|
$errors = []; |
|
96
|
|
|
|
|
97
|
5 |
|
foreach ($config->getCollectors() as $collector) { |
|
98
|
|
|
try { |
|
99
|
4 |
|
$collector = match (true) { |
|
100
|
4 |
|
\is_string($collector) => $factory->make($collector), |
|
101
|
2 |
|
$collector instanceof Autowire => $collector->resolve($factory), |
|
102
|
2 |
|
default => $collector, |
|
103
|
4 |
|
}; |
|
104
|
1 |
|
} catch (\Throwable) { |
|
105
|
1 |
|
$errors[] = \is_string($collector) || $collector instanceof \Stringable |
|
106
|
1 |
|
? (string) $collector |
|
107
|
|
|
: \get_debug_type($collector); |
|
108
|
1 |
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
if (!$collector instanceof StateCollectorInterface) { |
|
112
|
|
|
throw new StateException( |
|
113
|
|
|
\sprintf( |
|
114
|
|
|
'Unable to populate state, invalid state collector %s', |
|
115
|
|
|
\is_object($collector) ? $collector::class : \gettype($collector), |
|
116
|
|
|
), |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
$collector->populate($state); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
5 |
|
$errors === [] or $state->setTags(['unresolved-collectors' => \implode(', ', $errors)]); |
|
124
|
|
|
|
|
125
|
5 |
|
return $state; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
357 |
|
private function initDefaultConfig(): void |
|
129
|
|
|
{ |
|
130
|
357 |
|
$this->config->setDefaults(DebugConfig::CONFIG, [ |
|
131
|
357 |
|
'collectors' => [], |
|
132
|
357 |
|
'tags' => [], |
|
133
|
357 |
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|