1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfig; |
7
|
|
|
use WShafer\PSR11MonoLog\Formatter\FormatterMapper; |
8
|
|
|
use WShafer\PSR11MonoLog\Handler\HandlerMapper; |
9
|
|
|
use WShafer\PSR11MonoLog\Processor\ProcessorMapper; |
10
|
|
|
use WShafer\PSR11MonoLog\Service\FormatterManager; |
11
|
|
|
use WShafer\PSR11MonoLog\Service\HandlerManager; |
12
|
|
|
use WShafer\PSR11MonoLog\Service\ProcessorManager; |
13
|
|
|
|
14
|
|
|
class ChannelChangerFactory |
15
|
|
|
{ |
16
|
|
|
protected $config = null; |
17
|
|
|
|
18
|
|
|
protected $handlerManager = null; |
19
|
|
|
|
20
|
|
|
protected $processManager = null; |
21
|
|
|
|
22
|
|
|
protected $formatterManager = null; |
23
|
|
|
|
24
|
1 |
|
public function __invoke(ContainerInterface $container) |
25
|
|
|
{ |
26
|
1 |
|
$config = $this->getMainConfig($container); |
27
|
1 |
|
$handlerManager = $this->getHandlerManager($container); |
28
|
1 |
|
$processorManager = $this->getProcessorManager($container); |
29
|
|
|
|
30
|
1 |
|
return new ChannelChanger( |
31
|
1 |
|
$config, |
32
|
1 |
|
$handlerManager, |
33
|
1 |
|
$processorManager |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
public function getMainConfig(ContainerInterface $container) |
38
|
|
|
{ |
39
|
7 |
|
$config = $this->getConfigArray($container); |
40
|
7 |
|
return new MainConfig($config); |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
protected function getConfigArray(ContainerInterface $container) |
44
|
|
|
{ |
45
|
|
|
// Symfony config is parameters. // |
46
|
|
|
if ( |
47
|
7 |
|
method_exists($container, 'getParameter') |
48
|
7 |
|
&& method_exists($container, 'hasParameter') |
49
|
7 |
|
&& $container->hasParameter('monolog') |
|
|
|
|
50
|
|
|
) { |
51
|
1 |
|
return ['monolog' => $container->getParameter('monolog')]; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Zend uses config key |
55
|
6 |
|
if ($container->has('config')) { |
56
|
5 |
|
return $container->get('config'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Slim Config comes from "settings" |
60
|
1 |
|
if ($container->has('settings')) { |
61
|
1 |
|
return ['monolog' => $container->get('settings')['monolog']]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function getHandlerManager(ContainerInterface $container) |
68
|
|
|
{ |
69
|
2 |
|
$config = $this->getMainConfig($container); |
70
|
2 |
|
$this->handlerManager = new HandlerManager( |
71
|
2 |
|
$config, |
72
|
2 |
|
new HandlerMapper(), |
73
|
2 |
|
$container |
74
|
|
|
); |
75
|
|
|
|
76
|
2 |
|
$this->handlerManager->setFormatterManager($this->getFormatterManager($container)); |
77
|
2 |
|
$this->handlerManager->setProcessorManager($this->getProcessorManager($container)); |
78
|
2 |
|
return $this->handlerManager; |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function getFormatterManager(ContainerInterface $container) |
82
|
|
|
{ |
83
|
3 |
|
$config = $this->getMainConfig($container); |
84
|
3 |
|
$this->formatterManager = new FormatterManager( |
85
|
3 |
|
$config, |
86
|
3 |
|
new FormatterMapper(), |
87
|
3 |
|
$container |
88
|
|
|
); |
89
|
|
|
|
90
|
3 |
|
return $this->formatterManager; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
public function getProcessorManager(ContainerInterface $container) |
94
|
|
|
{ |
95
|
3 |
|
$config = $this->getMainConfig($container); |
96
|
3 |
|
$this->processManager = new ProcessorManager( |
97
|
3 |
|
$config, |
98
|
3 |
|
new ProcessorMapper(), |
99
|
3 |
|
$container |
100
|
|
|
); |
101
|
|
|
|
102
|
3 |
|
return $this->processManager; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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: