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 setFormatterManager(FormatterManager $manager) |
45
|
|
|
{ |
46
|
5 |
|
$this->formatterManager = $manager; |
47
|
5 |
|
} |
48
|
|
|
|
49
|
4 |
|
public function getFormatterManager() : FormatterManager |
50
|
|
|
{ |
51
|
4 |
|
if (!$this->formatterManager) { |
52
|
1 |
|
throw new MissingServiceException( |
53
|
1 |
|
'Unable to get FormatterManager.' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
return $this->formatterManager; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
protected function getFormatter($id) |
61
|
|
|
{ |
62
|
3 |
|
if (!$this->getFormatterManager()->has($id)) { |
63
|
1 |
|
throw new UnknownServiceException( |
64
|
1 |
|
'Unable to locate formatter '.$id |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this->getFormatterManager()->get($id); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|