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