1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog; |
4
|
|
|
|
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfig; |
9
|
|
|
use WShafer\PSR11MonoLog\Exception\MissingConfigException; |
10
|
|
|
use WShafer\PSR11MonoLog\Exception\UnknownServiceException; |
11
|
|
|
|
12
|
|
|
class ChannelChanger implements ContainerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var LoggerInterface[] */ |
15
|
|
|
protected $channels = []; |
16
|
|
|
|
17
|
|
|
/** @var MainConfig */ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** @var ContainerInterface */ |
21
|
|
|
protected $handlerManager; |
22
|
|
|
|
23
|
|
|
/** @var ContainerInterface */ |
24
|
|
|
protected $processorManager; |
25
|
|
|
|
26
|
8 |
|
public function __construct( |
27
|
|
|
MainConfig $config, |
28
|
|
|
ContainerInterface $handlerManager, |
29
|
|
|
ContainerInterface $processorManager |
30
|
|
|
) { |
31
|
8 |
|
$this->config = $config; |
32
|
8 |
|
$this->handlerManager = $handlerManager; |
33
|
8 |
|
$this->processorManager = $processorManager; |
34
|
8 |
|
} |
35
|
|
|
|
36
|
6 |
|
public function get($id) |
37
|
|
|
{ |
38
|
|
|
if ( |
39
|
6 |
|
!empty($this->channels[$id]) |
40
|
6 |
|
&& $this->channels[$id] instanceof LoggerInterface |
41
|
|
|
) { |
42
|
1 |
|
return $this->channels[$id]; |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
if (!$this->has($id)) { |
46
|
1 |
|
throw new MissingConfigException( |
47
|
1 |
|
'Unable to locate channel ' . $id |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
$config = $this->config->getChannelConfig($id); |
52
|
|
|
|
53
|
5 |
|
$name = $config->getName() ?? $id; |
54
|
|
|
|
55
|
5 |
|
$channel = new Logger($name); |
56
|
|
|
|
57
|
5 |
|
$handlersToUse = $config->getHandlers(); |
58
|
|
|
|
59
|
5 |
|
foreach ($handlersToUse as $handlerToUse) { |
60
|
5 |
|
$handler = $this->getHandler($handlerToUse); |
61
|
4 |
|
$channel->pushHandler($handler); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
$processorsToUse = $config->getProcessors(); |
65
|
|
|
|
66
|
4 |
|
foreach ($processorsToUse as $processorToUse) { |
67
|
4 |
|
$processor = $this->getProcessor($processorToUse); |
68
|
3 |
|
$channel->pushProcessor($processor); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
$this->channels[$id] = $channel; |
72
|
3 |
|
return $this->channels[$id]; |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
public function has($id) |
76
|
|
|
{ |
77
|
7 |
|
return $this->config->hasChannelConfig($id); |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
protected function getHandler($id) |
81
|
|
|
{ |
82
|
5 |
|
if (!$this->handlerManager->has($id)) { |
83
|
1 |
|
throw new UnknownServiceException( |
84
|
1 |
|
'Unable to locate processor ' . $id |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return $this->handlerManager->get($id); |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
protected function getProcessor($id) |
92
|
|
|
{ |
93
|
4 |
|
if (!$this->processorManager->has($id)) { |
94
|
1 |
|
throw new UnknownServiceException( |
95
|
1 |
|
'Unable to locate processor ' . $id |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return $this->processorManager->get($id); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: