1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WShafer\PSR11MonoLog\Config; |
6
|
|
|
|
7
|
|
|
use Monolog\Logger; |
8
|
|
|
|
9
|
|
|
class MainConfig |
10
|
|
|
{ |
11
|
|
|
/** @var HandlerConfig[] */ |
12
|
|
|
protected $handlers = []; |
13
|
|
|
|
14
|
|
|
/** @var FormatterConfig[] */ |
15
|
|
|
protected $formatters = []; |
16
|
|
|
|
17
|
|
|
/** @var ChannelConfig[] */ |
18
|
|
|
protected $channels = []; |
19
|
|
|
|
20
|
|
|
/** @var ProcessorConfig[] */ |
21
|
|
|
protected $processors = []; |
22
|
|
|
|
23
|
17 |
|
public function __construct(array $config) |
24
|
|
|
{ |
25
|
17 |
|
$this->setDefaults($config); |
26
|
17 |
|
$this->buildFormatters($config); |
27
|
17 |
|
$this->buildHandlers($config); |
28
|
17 |
|
$this->buildChannels($config); |
29
|
17 |
|
$this->buildProcessors($config); |
30
|
17 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return HandlerConfig[] |
34
|
|
|
*/ |
35
|
1 |
|
public function getHandlers() |
36
|
|
|
{ |
37
|
1 |
|
return $this->handlers; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function hasHandlerConfig($handler): bool |
41
|
|
|
{ |
42
|
1 |
|
return key_exists($handler, $this->handlers); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $handler |
47
|
|
|
* @return null|HandlerConfig |
48
|
|
|
*/ |
49
|
2 |
|
public function getHandlerConfig($handler) |
50
|
|
|
{ |
51
|
2 |
|
return $this->handlers[$handler] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return FormatterConfig[] |
56
|
|
|
*/ |
57
|
2 |
|
public function getFormatters(): array |
58
|
|
|
{ |
59
|
2 |
|
return $this->formatters; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function hasFormatterConfig($formatter): bool |
63
|
|
|
{ |
64
|
1 |
|
return key_exists($formatter, $this->formatters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $formatter |
69
|
|
|
* @return null|FormatterConfig |
70
|
|
|
*/ |
71
|
1 |
|
public function getFormatterConfig($formatter) |
72
|
|
|
{ |
73
|
1 |
|
return $this->formatters[$formatter] ?? null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return FormatterConfig[] |
78
|
|
|
*/ |
79
|
2 |
|
public function getProcessors(): array |
80
|
|
|
{ |
81
|
2 |
|
return $this->processors; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function hasProcessorConfig($processor): bool |
85
|
|
|
{ |
86
|
1 |
|
return key_exists($processor, $this->processors); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $processor |
91
|
|
|
* @return null|ProcessorConfig |
92
|
|
|
*/ |
93
|
1 |
|
public function getProcessorConfig($processor) |
94
|
|
|
{ |
95
|
1 |
|
return $this->processors[$processor] ?? null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ChannelConfig[] |
100
|
|
|
*/ |
101
|
1 |
|
public function getChannels(): array |
102
|
|
|
{ |
103
|
1 |
|
return $this->channels; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $channel |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
|
public function hasChannelConfig($channel): bool |
111
|
|
|
{ |
112
|
1 |
|
return key_exists($channel, $this->channels); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function getChannelConfig($channel) |
116
|
|
|
{ |
117
|
2 |
|
return $this->channels[$channel] ?? null; |
118
|
|
|
} |
119
|
|
|
|
120
|
17 |
|
protected function setDefaults(array &$config) |
121
|
|
|
{ |
122
|
17 |
|
if (empty($config['monolog']['handlers']['default'])) { |
123
|
1 |
|
$config['monolog']['handlers']['default'] = [ |
124
|
1 |
|
'type' => 'noop', |
125
|
|
|
'options' => [ |
126
|
1 |
|
'level' => Logger::DEBUG |
127
|
|
|
] |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
17 |
|
if (empty($config['monolog']['channels']['default'])) { |
132
|
1 |
|
$config['monolog']['channels']['default']['handlers'][] = 'default'; |
133
|
|
|
} |
134
|
17 |
|
} |
135
|
|
|
|
136
|
17 |
|
protected function buildHandlers($config) |
137
|
|
|
{ |
138
|
17 |
|
foreach ($config['monolog']['handlers'] as $name => $handlerConfig) { |
139
|
17 |
|
$this->handlers[$name] = new HandlerConfig($handlerConfig); |
140
|
|
|
} |
141
|
17 |
|
} |
142
|
|
|
|
143
|
17 |
|
protected function buildChannels($config) |
144
|
|
|
{ |
145
|
17 |
|
foreach ($config['monolog']['channels'] as $name => $channelConfig) { |
146
|
17 |
|
$this->channels[$name] = new ChannelConfig($channelConfig); |
147
|
|
|
} |
148
|
17 |
|
} |
149
|
|
|
|
150
|
17 |
|
protected function buildFormatters($config) |
151
|
|
|
{ |
152
|
17 |
|
if (empty($config['monolog']['formatters'])) { |
153
|
1 |
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
17 |
|
foreach ($config['monolog']['formatters'] as $name => $formatterConfig) { |
157
|
17 |
|
$this->formatters[$name] = new FormatterConfig($formatterConfig); |
158
|
|
|
} |
159
|
17 |
|
} |
160
|
|
|
|
161
|
17 |
|
protected function buildProcessors($config) |
162
|
|
|
{ |
163
|
17 |
|
if (empty($config['monolog']['processors'])) { |
164
|
1 |
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
17 |
|
foreach ($config['monolog']['processors'] as $name => $processorConfig) { |
168
|
17 |
|
$this->processors[$name] = new ProcessorConfig($processorConfig); |
169
|
|
|
} |
170
|
17 |
|
} |
171
|
|
|
} |
172
|
|
|
|