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\MapperInterface; |
13
|
|
|
|
14
|
|
|
abstract class AbstractServiceManager implements ContainerInterface |
15
|
|
|
{ |
16
|
|
|
/** @var MainConfig */ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** @var MapperInterface */ |
20
|
|
|
protected $mapper; |
21
|
|
|
|
22
|
|
|
/** @var ContainerInterface */ |
23
|
|
|
protected $container; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $services = []; |
27
|
|
|
|
28
|
9 |
|
public function __construct( |
29
|
|
|
MainConfig $config, |
30
|
|
|
MapperInterface $mapper, |
31
|
|
|
ContainerInterface $container |
32
|
|
|
) { |
33
|
9 |
|
$this->config = $config; |
34
|
9 |
|
$this->mapper = $mapper; |
35
|
9 |
|
$this->container = $container; |
36
|
9 |
|
} |
37
|
|
|
|
38
|
|
|
abstract protected function getServiceConfig($id) : ConfigInterface; |
39
|
|
|
abstract protected function hasServiceConfig($id) : bool; |
40
|
|
|
|
41
|
6 |
|
public function get($id) |
42
|
|
|
{ |
43
|
6 |
|
if (key_exists($id, $this->services)) { |
44
|
1 |
|
return $this->services[$id]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Make sure we have one of these |
48
|
6 |
|
if (!$this->has($id)) { |
49
|
1 |
|
throw new UnknownServiceException( |
50
|
1 |
|
'Unable to locate service '.$id.'. Please check your configuration.' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Check the main container for this service id. |
55
|
5 |
|
if ($this->container->has($id)) { |
56
|
2 |
|
$this->services[$id] = $this->container->get($id); |
57
|
2 |
|
return $this->services[$id]; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
$this->services[$id] = $this->getInstanceFromFactory($id); |
61
|
2 |
|
return $this->services[$id]; |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
public function has($id) |
65
|
|
|
{ |
66
|
8 |
|
if (key_exists($id, $this->services)) { |
67
|
1 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
if ($this->container->has($id)) { |
71
|
3 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
return $this->hasServiceConfig($id); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
protected function getInstanceFromFactory($id) |
78
|
|
|
{ |
79
|
3 |
|
$class = null; |
|
|
|
|
80
|
|
|
|
81
|
3 |
|
$config = $this->getServiceConfig($id); |
82
|
3 |
|
$type = $config->getType(); |
83
|
3 |
|
$options = $config->getOptions(); |
84
|
|
|
|
85
|
3 |
|
$class = $type; |
86
|
|
|
|
87
|
|
|
// Check for class and class implements of Monolog Formatter Interface |
88
|
3 |
|
if (!class_exists($class) |
89
|
3 |
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
90
|
|
|
) { |
91
|
2 |
|
$class = $this->mapper->map($type); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
if (!class_exists($class) |
95
|
3 |
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
96
|
|
|
) { |
97
|
1 |
|
throw new InvalidConfigException( |
98
|
1 |
|
$id.'. Is not a valid factory. Please check your configuration.' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @var FactoryInterface $factory */ |
103
|
2 |
|
$factory = new $class; |
104
|
|
|
|
105
|
2 |
|
if ($factory instanceof ContainerAwareInterface) { |
106
|
2 |
|
$factory->setContainer($this->container); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return $factory($options); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.