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\UnknownServiceException; |
9
|
|
|
use WShafer\PSR11MonoLog\FactoryInterface; |
10
|
|
|
use WShafer\PSR11MonoLog\MapperInterface; |
11
|
|
|
|
12
|
|
|
abstract class AbstractServiceManager implements ContainerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var MainConfig */ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
/** @var MapperInterface */ |
18
|
|
|
protected $mapper; |
19
|
|
|
|
20
|
|
|
/** @var ContainerInterface */ |
21
|
|
|
protected $container; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $services; |
25
|
|
|
|
26
|
|
|
const INTERFACE = ''; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
MainConfig $config, |
30
|
|
|
MapperInterface $mapper, |
31
|
|
|
ContainerInterface $container |
32
|
|
|
) { |
33
|
|
|
$this->config = $config; |
34
|
|
|
$this->mapper = $mapper; |
35
|
|
|
$this->container = $container; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
abstract protected function getServiceConfig($id) : ConfigInterface; |
39
|
|
|
|
40
|
|
|
public function get($id) |
41
|
|
|
{ |
42
|
|
|
$interface = self::INTERFACE; |
43
|
|
|
|
44
|
|
View Code Duplication |
if (key_exists($id, $this->services) |
|
|
|
|
45
|
|
|
&& $this->services[$id] instanceof $interface |
46
|
|
|
) { |
47
|
|
|
return $this->services[$id]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Make sure we have one of these |
51
|
|
|
if (!$this->has($id)) { |
52
|
|
|
throw new UnknownServiceException( |
53
|
|
|
'Unable to locate service '.$id.'. Please check your configuration.' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Check the main container for this service id. |
58
|
|
|
if ($this->container->has($id)) { |
59
|
|
|
$this->services[$id] = $this->container->get($id); |
60
|
|
|
return $this->services[$id]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Check for class and class implements of Monolog Interface |
64
|
|
|
if (class_exists($id) |
65
|
|
|
&& in_array($interface, class_implements($id)) |
66
|
|
|
) { |
67
|
|
|
$this->services[$id] = new $id; |
68
|
|
|
return $this->services[$id]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->services[$id] = $this->getInstanceFromFactory($id); |
72
|
|
|
return $this->services[$id]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function has($id) |
76
|
|
|
{ |
77
|
|
|
$interface = self::INTERFACE; |
78
|
|
|
|
79
|
|
View Code Duplication |
if (key_exists($id, $this->services) |
|
|
|
|
80
|
|
|
&& $this->services[$id] instanceof $interface |
81
|
|
|
) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($this->container->has($id)) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (class_exists($id) |
90
|
|
|
&& in_array($interface, class_implements($id)) |
91
|
|
|
) { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->config->hasHandlerConfig($id); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getInstanceFromFactory($id) |
99
|
|
|
{ |
100
|
|
|
$class = null; |
|
|
|
|
101
|
|
|
|
102
|
|
|
$config = $this->getServiceConfig($id); |
103
|
|
|
$type = $config->getType(); |
104
|
|
|
$options = $config->getOptions(); |
105
|
|
|
|
106
|
|
|
$class = $type; |
107
|
|
|
|
108
|
|
|
// Check for class and class implements of Monolog Formatter Interface |
109
|
|
|
if (!class_exists($class) |
110
|
|
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
111
|
|
|
) { |
112
|
|
|
$class = $this->mapper->map($type); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!class_exists($class) |
116
|
|
|
|| !in_array(FactoryInterface::class, class_implements($class)) |
117
|
|
|
) { |
118
|
|
|
throw new UnknownServiceException( |
119
|
|
|
'Unable to locate service '.$id.'. Please check your configuration.' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @var FactoryInterface $factory */ |
124
|
|
|
$factory = new $class; |
125
|
|
|
return $factory($options); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.