1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog\Service; |
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\UnknownServiceException; |
10
|
|
|
|
11
|
|
|
class ChannelChanger implements ContainerInterface |
12
|
|
|
{ |
13
|
|
|
/** @var LoggerInterface[] */ |
14
|
|
|
protected $channels = []; |
15
|
|
|
|
16
|
|
|
/** @var MainConfig */ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** @var ContainerInterface */ |
20
|
|
|
protected $handlerManager; |
21
|
|
|
|
22
|
|
|
/** @var ContainerInterface */ |
23
|
|
|
protected $processorManager; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
MainConfig $config, |
27
|
|
|
ContainerInterface $handlerManager, |
28
|
|
|
ContainerInterface $processorManager |
29
|
|
|
) { |
30
|
|
|
$this->config = $config; |
31
|
|
|
$this->handlerManager = $handlerManager; |
32
|
|
|
$this->processorManager = $processorManager; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function get($id) |
36
|
|
|
{ |
37
|
|
|
if (!empty($this->channels[$id]) |
38
|
|
|
&& $this->channels[$id] instanceof LoggerInterface |
39
|
|
|
) { |
40
|
|
|
return $this->channels[$id]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$this->has($id)) { |
44
|
|
|
throw new UnknownServiceException( |
45
|
|
|
'Unable to locate channel '.$id |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$config = $this->config->getChannelConfig($id); |
50
|
|
|
|
51
|
|
|
$channel = new Logger($id); |
52
|
|
|
|
53
|
|
|
$handlersToUse = $config->getHandlers(); |
54
|
|
|
|
55
|
|
|
foreach ($handlersToUse as $handlerToUse) { |
56
|
|
|
$handler = $this->getHandler($handlerToUse); |
57
|
|
|
$channel->pushHandler($handler); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$processorsToUse = $config->getProcessors(); |
61
|
|
|
|
62
|
|
|
foreach ($processorsToUse as $processorToUse) { |
63
|
|
|
$processor = $this->getProcessor($processorToUse); |
64
|
|
|
$channel->pushProcessor($processor); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->channels[$id]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function has($id) |
71
|
|
|
{ |
72
|
|
|
return $this->config->hasChannelConfig($id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getHandler($id) |
76
|
|
|
{ |
77
|
|
|
if (!$this->handlerManager->has($id)) { |
78
|
|
|
throw new UnknownServiceException( |
79
|
|
|
'Unable to locate processor '.$id |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->handlerManager->get($id); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getProcessor($id) |
87
|
|
|
{ |
88
|
|
|
if (!$this->processorManager->has($id)) { |
89
|
|
|
throw new UnknownServiceException( |
90
|
|
|
'Unable to locate processor '.$id |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->processorManager->get($id); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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: