1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfig; |
8
|
|
|
use WShafer\PSR11MonoLog\Formatter\FormatterMapper; |
9
|
|
|
use WShafer\PSR11MonoLog\Handler\HandlerMapper; |
10
|
|
|
use WShafer\PSR11MonoLog\Processor\ProcessorMapper; |
11
|
|
|
use WShafer\PSR11MonoLog\Service\FormatterManager; |
12
|
|
|
use WShafer\PSR11MonoLog\Service\HandlerManager; |
13
|
|
|
use WShafer\PSR11MonoLog\Service\ProcessorManager; |
14
|
|
|
|
15
|
|
|
class ChannelChangerFactory |
16
|
|
|
{ |
17
|
|
|
protected $config = null; |
18
|
|
|
|
19
|
|
|
protected $handlerManager = null; |
20
|
|
|
|
21
|
|
|
protected $processManager = null; |
22
|
|
|
|
23
|
|
|
protected $formatterManager = null; |
24
|
|
|
|
25
|
1 |
|
public function __invoke(ContainerInterface $container) |
26
|
|
|
{ |
27
|
1 |
|
$config = $this->getMainConfig($container); |
28
|
1 |
|
$handlerManager = $this->getHandlerManager($container); |
29
|
1 |
|
$processorManager = $this->getProcessorManager($container); |
30
|
|
|
|
31
|
1 |
|
return new ChannelChanger( |
32
|
1 |
|
$config, |
33
|
1 |
|
$handlerManager, |
34
|
1 |
|
$processorManager |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
public function getMainConfig(ContainerInterface $container) |
39
|
|
|
{ |
40
|
8 |
|
return new MainConfig($this->getConfigArray($container)); |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
protected function getConfigArray(ContainerInterface $container) |
44
|
|
|
{ |
45
|
|
|
// Symfony config is parameters. // |
46
|
8 |
|
if (method_exists($container, 'getParameter') |
47
|
8 |
|
&& method_exists($container, 'hasParameter') |
48
|
8 |
|
&& $container->hasParameter('monolog') |
|
|
|
|
49
|
|
|
) { |
50
|
1 |
|
return ['monolog' => $container->getParameter('monolog')]; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Zend uses config key |
54
|
7 |
|
if ($container->has('config')) { |
55
|
6 |
|
return $this->getZendConfigArray($container); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Slim Config comes from "settings" |
59
|
1 |
|
if ($container->has('settings')) { |
60
|
1 |
|
return ['monolog' => $container->get('settings')['monolog']]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
protected function getZendConfigArray(ContainerInterface $container) |
67
|
|
|
{ |
68
|
6 |
|
$config = $container->get('config'); |
69
|
6 |
|
if ($config instanceof ArrayObject) { |
70
|
1 |
|
$config = $config->getArrayCopy(); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
return $config; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public function getHandlerManager(ContainerInterface $container) |
77
|
|
|
{ |
78
|
2 |
|
$config = $this->getMainConfig($container); |
79
|
2 |
|
$this->handlerManager = new HandlerManager( |
80
|
2 |
|
$config, |
81
|
2 |
|
new HandlerMapper(), |
82
|
2 |
|
$container |
83
|
|
|
); |
84
|
|
|
|
85
|
2 |
|
$this->handlerManager->setFormatterManager($this->getFormatterManager($container)); |
86
|
2 |
|
$this->handlerManager->setProcessorManager($this->getProcessorManager($container)); |
87
|
2 |
|
return $this->handlerManager; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
public function getFormatterManager(ContainerInterface $container) |
91
|
|
|
{ |
92
|
3 |
|
$config = $this->getMainConfig($container); |
93
|
3 |
|
$this->formatterManager = new FormatterManager( |
94
|
3 |
|
$config, |
95
|
3 |
|
new FormatterMapper(), |
96
|
3 |
|
$container |
97
|
|
|
); |
98
|
|
|
|
99
|
3 |
|
return $this->formatterManager; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
public function getProcessorManager(ContainerInterface $container) |
103
|
|
|
{ |
104
|
3 |
|
$config = $this->getMainConfig($container); |
105
|
3 |
|
$this->processManager = new ProcessorManager( |
106
|
3 |
|
$config, |
107
|
3 |
|
new ProcessorMapper(), |
108
|
3 |
|
$container |
109
|
|
|
); |
110
|
|
|
|
111
|
3 |
|
return $this->processManager; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: