1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Monolog\Config; |
6
|
|
|
|
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Spiral\Core\Container\Autowire; |
9
|
|
|
use Spiral\Core\InjectableConfig; |
10
|
|
|
use Spiral\Monolog\Exception\ConfigException; |
11
|
|
|
|
12
|
|
|
final class MonologConfig extends InjectableConfig |
13
|
|
|
{ |
14
|
|
|
public const CONFIG = 'monolog'; |
15
|
|
|
public const DEFAULT_CHANNEL = 'default'; |
16
|
|
|
|
17
|
|
|
protected array $config = [ |
18
|
|
|
'default' => self::DEFAULT_CHANNEL, |
19
|
|
|
'globalLevel' => Logger::DEBUG, |
20
|
|
|
'handlers' => [], |
21
|
|
|
]; |
22
|
|
|
|
23
|
21 |
|
public function getDefault(): string |
24
|
|
|
{ |
25
|
21 |
|
return $this->config['default'] ?? self::DEFAULT_CHANNEL; |
26
|
|
|
} |
27
|
|
|
|
28
|
21 |
|
public function getEventLevel(): int |
29
|
|
|
{ |
30
|
21 |
|
return $this->config['globalLevel'] ?? Logger::DEBUG; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \Generator<int, Autowire> |
35
|
|
|
*/ |
36
|
21 |
|
public function getHandlers(string $channel): \Generator |
37
|
|
|
{ |
38
|
21 |
|
if (empty($this->config['handlers'][$channel])) { |
39
|
14 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
foreach ($this->config['handlers'][$channel] as $handler) { |
43
|
7 |
|
if (\is_object($handler) && !$handler instanceof Autowire) { |
44
|
2 |
|
yield $handler; |
45
|
2 |
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
$wire = $this->wire($handler); |
49
|
5 |
|
if (\is_null($wire)) { |
50
|
1 |
|
throw new ConfigException(\sprintf('Invalid handler definition for channel `%s`.', $channel)); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
yield $wire; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
20 |
|
public function getProcessors(string $channel): \Generator |
58
|
|
|
{ |
59
|
20 |
|
if (empty($this->config['processors'][$channel])) { |
60
|
13 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
7 |
|
foreach ($this->config['processors'][$channel] as $processor) { |
64
|
7 |
|
if (\is_object($processor) && !$processor instanceof Autowire) { |
65
|
3 |
|
yield $processor; |
66
|
3 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$wire = $this->wire($processor); |
70
|
4 |
|
if (\is_null($wire)) { |
71
|
1 |
|
throw new ConfigException(\sprintf('Invalid processor definition for channel `%s`.', $channel)); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
yield $wire; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
9 |
|
private function wire(Autowire|string|array $definition): ?Autowire |
79
|
|
|
{ |
80
|
9 |
|
if ($definition instanceof Autowire) { |
|
|
|
|
81
|
1 |
|
return $definition; |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
if (\is_string($definition)) { |
|
|
|
|
85
|
2 |
|
return new Autowire($definition); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
if (isset($definition['class'])) { |
89
|
4 |
|
return new Autowire($definition['class'], $definition['options'] ?? []); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return null; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|