1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfig; |
7
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfigFactory; |
8
|
|
|
use WShafer\PSR11MonoLog\Formatter\FormatterMapper; |
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
|
8 |
|
public function getMainConfig(ContainerInterface $container) |
38
|
|
|
{ |
39
|
8 |
|
$config = $this->getConfigArray($container); |
40
|
8 |
|
return new MainConfig($config); |
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
|
5 |
|
return $container->get('config'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Slim Config comes from "settings" |
59
|
2 |
|
if ($container->has('settings')) { |
60
|
1 |
|
return ['monolog' => $container->get('settings')['monolog']]; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function getHandlerManager(ContainerInterface $container) |
67
|
|
|
{ |
68
|
2 |
|
$config = $this->getMainConfig($container); |
69
|
2 |
|
$this->handlerManager = new HandlerManager( |
70
|
2 |
|
$config, |
71
|
2 |
|
new FormatterMapper(), |
72
|
2 |
|
$container |
73
|
|
|
); |
74
|
|
|
|
75
|
2 |
|
$this->handlerManager->setFormatterManager($this->getFormatterManager($container)); |
76
|
2 |
|
return $this->handlerManager; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function getFormatterManager(ContainerInterface $container) |
80
|
|
|
{ |
81
|
3 |
|
$config = $this->getMainConfig($container); |
82
|
3 |
|
$this->formatterManager = new FormatterManager( |
83
|
3 |
|
$config, |
84
|
3 |
|
new FormatterMapper(), |
85
|
3 |
|
$container |
86
|
|
|
); |
87
|
|
|
|
88
|
3 |
|
return $this->formatterManager; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function getProcessorManager(ContainerInterface $container) |
92
|
|
|
{ |
93
|
2 |
|
$config = $this->getMainConfig($container); |
94
|
2 |
|
$this->processManager = new ProcessorManager( |
95
|
2 |
|
$config, |
96
|
2 |
|
new ProcessorMapper(), |
97
|
2 |
|
$container |
98
|
|
|
); |
99
|
|
|
|
100
|
2 |
|
return $this->processManager; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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: