|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace RicardoFiorani\Container; |
|
4
|
|
|
|
|
5
|
|
|
use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface; |
|
6
|
|
|
use RicardoFiorani\Container\Exception\DuplicatedServiceNameException; |
|
7
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ServicesContainer |
|
10
|
|
|
{ |
|
11
|
|
|
private $services = array(); |
|
|
|
|
|
|
12
|
|
|
private $patterns = array(); |
|
|
|
|
|
|
13
|
|
|
private $factories = array(); |
|
|
|
|
|
|
14
|
|
|
private $instantiatedFactories = array(); |
|
15
|
|
|
private $renderer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @throws DuplicatedServiceNameException |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function __construct(array $config = []) |
|
21
|
|
|
{ |
|
22
|
1 |
|
if (false == empty($config)) { |
|
|
|
|
|
|
23
|
|
|
$this->registerFromConfig($config); |
|
24
|
|
|
} |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws DuplicatedServiceNameException |
|
29
|
|
|
*/ |
|
30
|
|
|
private function registerFromConfig(array $config) |
|
31
|
|
|
{ |
|
32
|
|
|
foreach ($config['services'] as $serviceName => $serviceConfig) { |
|
33
|
|
|
$this->registerService($serviceName, $serviceConfig['patterns'], new $serviceConfig['factory']); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->setRenderer($config['renderer']['name'], new $config['renderer']['factory']); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws DuplicatedServiceNameException |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function registerService(string $serviceName, array $regex, callable $factory) |
|
43
|
|
|
{ |
|
44
|
1 |
|
if ($this->hasService($serviceName)) { |
|
45
|
1 |
|
throw new DuplicatedServiceNameException( |
|
46
|
1 |
|
sprintf( |
|
47
|
1 |
|
'The service "%s" is already registered in the container.', |
|
48
|
1 |
|
$serviceName |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$this->services[] = $serviceName; |
|
|
|
|
|
|
54
|
1 |
|
$this->patterns[$serviceName] = $regex; |
|
|
|
|
|
|
55
|
1 |
|
$this->factories[$serviceName] = $factory; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setRenderer(callable $rendererFactory) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->renderer = $rendererFactory(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getRenderer(): EmbedRendererInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->renderer; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function getServiceNameList(): array |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->services; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
public function hasService($serviceName): bool |
|
74
|
|
|
{ |
|
75
|
1 |
|
return in_array($serviceName, $this->services); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getServices(): array |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->services; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getPatterns(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->patterns; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getFactory($serviceName): CallableServiceAdapterFactoryInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$factory = new $this->factories[$serviceName](); |
|
91
|
|
|
if (isset($this->instantiatedFactories[$serviceName])) { |
|
92
|
|
|
return $this->instantiatedFactories[$serviceName]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->instantiatedFactories[$serviceName] = $factory; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.