1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WShafer\PSR11MonoLog\Service; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use WShafer\PSR11MonoLog\Config\MainConfig; |
7
|
|
|
use WShafer\PSR11MonoLog\ConfigInterface; |
8
|
|
|
use WShafer\PSR11MonoLog\ContainerAwareInterface; |
9
|
|
|
use WShafer\PSR11MonoLog\Exception\InvalidConfigException; |
10
|
|
|
use WShafer\PSR11MonoLog\Exception\UnknownServiceException; |
11
|
|
|
use WShafer\PSR11MonoLog\FactoryInterface; |
12
|
|
|
use WShafer\PSR11MonoLog\HandlerManagerAwareInterface; |
13
|
|
|
use WShafer\PSR11MonoLog\MapperInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractServiceManager implements ContainerInterface |
16
|
|
|
{ |
17
|
|
|
/** @var MainConfig */ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** @var MapperInterface */ |
21
|
|
|
protected $mapper; |
22
|
|
|
|
23
|
|
|
/** @var ContainerInterface */ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected $services = []; |
28
|
|
|
|
29
|
9 |
|
public function __construct( |
30
|
|
|
MainConfig $config, |
31
|
|
|
MapperInterface $mapper, |
32
|
|
|
ContainerInterface $container |
33
|
|
|
) { |
34
|
9 |
|
$this->config = $config; |
35
|
9 |
|
$this->mapper = $mapper; |
36
|
9 |
|
$this->container = $container; |
37
|
9 |
|
} |
38
|
|
|
|
39
|
|
|
abstract protected function getServiceConfig($id) : ConfigInterface; |
40
|
|
|
abstract protected function hasServiceConfig($id) : bool; |
41
|
|
|
|
42
|
6 |
|
public function get($id) |
43
|
|
|
{ |
44
|
6 |
|
if (key_exists($id, $this->services)) { |
45
|
1 |
|
return $this->services[$id]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Make sure we have one of these |
49
|
6 |
|
if (!$this->has($id)) { |
50
|
1 |
|
throw new UnknownServiceException( |
51
|
1 |
|
'Unable to locate service '.$id.'. Please check your configuration.' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Check the main container for this service id. |
56
|
5 |
|
if ($this->container->has($id)) { |
57
|
2 |
|
$this->services[$id] = $this->container->get($id); |
58
|
2 |
|
return $this->services[$id]; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$this->services[$id] = $this->getInstanceFromFactory($id); |
62
|
2 |
|
return $this->services[$id]; |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
public function has($id) |
66
|
|
|
{ |
67
|
8 |
|
if (key_exists($id, $this->services)) { |
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
if ($this->container->has($id)) { |
72
|
3 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
return $this->hasServiceConfig($id); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
protected function getInstanceFromFactory($id) |
79
|
|
|
{ |
80
|
3 |
|
$class = null; |
|
|
|
|
81
|
|
|
|
82
|
3 |
|
$config = $this->getServiceConfig($id); |
83
|
3 |
|
$type = $config->getType(); |
84
|
3 |
|
$options = $config->getOptions(); |
85
|
|
|
|
86
|
3 |
|
$class = $type; |
87
|
|
|
|
88
|
|
|
// Check for class and class implements of Monolog Formatter Interface |
89
|
3 |
|
if (!class_exists($class) |
90
|
3 |
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
91
|
|
|
) { |
92
|
2 |
|
$class = $this->mapper->map($type); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
if (!class_exists($class) |
96
|
3 |
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
97
|
|
|
) { |
98
|
1 |
|
throw new InvalidConfigException( |
99
|
1 |
|
$id.'. Is not a valid factory. Please check your configuration.' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @var FactoryInterface $factory */ |
104
|
2 |
|
$factory = new $class; |
105
|
|
|
|
106
|
2 |
|
if ($factory instanceof ContainerAwareInterface) { |
107
|
2 |
|
$factory->setContainer($this->container); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
if ($factory instanceof HandlerManagerAwareInterface |
111
|
2 |
|
&& $this instanceof HandlerManager |
112
|
|
|
) { |
113
|
|
|
$factory->setHandlerManager($this); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return $factory($options); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.