1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog\Service; |
4
|
|
|
|
5
|
|
|
use Monolog\Handler\HandlerInterface; |
6
|
|
|
use WShafer\PSR11MonoLog\ConfigInterface; |
7
|
|
|
use WShafer\PSR11MonoLog\Exception\MissingServiceException; |
8
|
|
|
use WShafer\PSR11MonoLog\Exception\UnknownServiceException; |
9
|
|
|
|
10
|
|
|
class HandlerManager extends AbstractServiceManager |
11
|
|
|
{ |
12
|
|
|
/** @var FormatterManager */ |
13
|
|
|
protected $formatterManager; |
14
|
|
|
|
15
|
1 |
|
public function getServiceConfig($id): ConfigInterface |
16
|
|
|
{ |
17
|
1 |
|
return $this->config->getHandlerConfig($id); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function hasServiceConfig($id): bool |
21
|
|
|
{ |
22
|
|
|
return $this->config->hasHandlerConfig($id); |
23
|
|
|
} |
24
|
|
|
|
25
|
5 |
|
public function get($id) |
26
|
|
|
{ |
27
|
5 |
|
if (key_exists($id, $this->services)) { |
28
|
1 |
|
return $this->services[$id]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @var HandlerInterface $handler */ |
32
|
5 |
|
$handler = parent::get($id); |
33
|
|
|
|
34
|
5 |
|
$config = $this->config->getHandlerConfig($id); |
35
|
|
|
|
36
|
5 |
|
if (!$config) { |
37
|
2 |
|
return $handler; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$formatter= $config->getFormatter(); |
41
|
|
|
|
42
|
3 |
|
if ($formatter) { |
43
|
3 |
|
$handler->setFormatter($this->getFormatter($formatter)); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $handler; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
public function setFormatterManager(FormatterManager $manager) |
50
|
|
|
{ |
51
|
5 |
|
$this->formatterManager = $manager; |
52
|
5 |
|
} |
53
|
|
|
|
54
|
4 |
|
public function getFormatterManager() : FormatterManager |
55
|
|
|
{ |
56
|
4 |
|
if (!$this->formatterManager) { |
57
|
1 |
|
throw new MissingServiceException( |
58
|
1 |
|
'Unable to get FormatterManager.' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $this->formatterManager; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
protected function getFormatter($id) |
66
|
|
|
{ |
67
|
3 |
|
if (!$this->getFormatterManager()->has($id)) { |
68
|
1 |
|
throw new UnknownServiceException( |
69
|
1 |
|
'Unable to locate formatter '.$id |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $this->getFormatterManager()->get($id); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|