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
|
5 |
|
public function get($id) |
21
|
|
|
{ |
22
|
5 |
|
if (key_exists($id, $this->services)) { |
23
|
1 |
|
return $this->services[$id]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @var HandlerInterface $handler */ |
27
|
5 |
|
$handler = parent::get($id); |
28
|
|
|
|
29
|
5 |
|
$config = $this->config->getHandlerConfig($id); |
30
|
|
|
|
31
|
5 |
|
if (!$config) { |
32
|
2 |
|
return $handler; |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
$formatter= $config->getFormatter(); |
36
|
|
|
|
37
|
3 |
|
if ($formatter) { |
38
|
3 |
|
$handler->setFormatter($this->getFormatter($formatter)); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $handler; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
public function has($id) |
45
|
|
|
{ |
46
|
5 |
|
if (parent::has($id)) { |
47
|
5 |
|
return true; |
48
|
|
|
} |
49
|
4 |
|
|
50
|
|
|
return $this->config->hasHandlerConfig($id); |
51
|
4 |
|
} |
52
|
1 |
|
|
53
|
1 |
|
public function setFormatterManager(FormatterManager $manager) |
54
|
|
|
{ |
55
|
|
|
$this->formatterManager = $manager; |
56
|
|
|
} |
57
|
3 |
|
|
58
|
|
|
public function getFormatterManager() : FormatterManager |
59
|
|
|
{ |
60
|
3 |
|
if (!$this->formatterManager) { |
61
|
|
|
throw new MissingServiceException( |
62
|
3 |
|
'Unable to get FormatterManager.' |
63
|
1 |
|
); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
return $this->formatterManager; |
67
|
|
|
} |
68
|
1 |
|
|
69
|
|
|
protected function getFormatter($id) |
70
|
|
|
{ |
71
|
|
|
if (!$this->getFormatterManager()->has($id)) { |
72
|
|
|
throw new UnknownServiceException( |
73
|
|
|
'Unable to locate formatter '.$id |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->getFormatterManager()->get($id); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|