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