1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Ricardo Fiorani |
5
|
|
|
* Date: 31/08/2015 |
6
|
|
|
* Time: 21:06. |
7
|
|
|
*/ |
8
|
|
|
namespace RicardoFiorani\Container; |
9
|
|
|
|
10
|
|
|
use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface; |
11
|
|
|
use RicardoFiorani\Container\Exception\DuplicatedServiceNameException; |
12
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
13
|
|
|
|
14
|
|
|
class ServicesContainer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $services = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $patterns = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $factories = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $instantiatedFactories = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EmbedRendererInterface |
38
|
|
|
*/ |
39
|
|
|
private $renderer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $rendererName; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ServicesContainer constructor. |
48
|
|
|
* |
49
|
|
|
* @param array $config |
50
|
|
|
* @throws DuplicatedServiceNameException |
51
|
|
|
*/ |
52
|
|
|
public function __construct(array $config = array()) |
53
|
|
|
{ |
54
|
|
|
if (false == empty($config)) { |
|
|
|
|
55
|
|
|
$this->registerFromConfig($config); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Loads de default config file. |
61
|
|
|
* |
62
|
|
|
* @param array $config |
63
|
|
|
* |
64
|
|
|
* @throws DuplicatedServiceNameException |
65
|
|
|
*/ |
66
|
|
|
private function registerFromConfig(array $config) |
67
|
|
|
{ |
68
|
|
|
foreach ($config['services'] as $serviceName => $serviceConfig) { |
69
|
|
|
$this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']); |
70
|
|
|
} |
71
|
|
|
$this->setRenderer($config['renderer']['name'], $config['renderer']['factory']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Register a Service. |
76
|
|
|
* |
77
|
|
|
* @param string $serviceName |
78
|
|
|
* @param array $regex |
79
|
|
|
* @param string|callable $factory |
80
|
|
|
* |
81
|
|
|
* @throws DuplicatedServiceNameException |
82
|
|
|
*/ |
83
|
|
|
public function registerService($serviceName, array $regex, $factory) |
84
|
|
|
{ |
85
|
|
|
if ($this->hasService($serviceName)) { |
86
|
|
|
throw new DuplicatedServiceNameException( |
87
|
|
|
'The service "%s" is already registered in the container.', $serviceName |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->services[] = $serviceName; |
92
|
|
|
$this->patterns[$serviceName] = $regex; |
93
|
|
|
$this->factories[$serviceName] = $factory; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $rendererName |
98
|
|
|
* @param string $rendererFactory |
99
|
|
|
*/ |
100
|
|
|
public function setRenderer($rendererName, $rendererFactory) |
101
|
|
|
{ |
102
|
|
|
$this->rendererName = $rendererName; |
103
|
|
|
$factory = new $rendererFactory(); |
104
|
|
|
$this->renderer = $factory(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return EmbedRendererInterface |
109
|
|
|
*/ |
110
|
|
|
public function getRenderer() |
111
|
|
|
{ |
112
|
|
|
return $this->renderer; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getServiceNameList() |
119
|
|
|
{ |
120
|
|
|
return $this->services; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $serviceName |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function hasService($serviceName) |
129
|
|
|
{ |
130
|
|
|
return in_array($serviceName, $this->services); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getServices() |
137
|
|
|
{ |
138
|
|
|
return $this->services; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function getPatterns() |
145
|
|
|
{ |
146
|
|
|
return $this->patterns; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $serviceName |
151
|
|
|
* |
152
|
|
|
* @return CallableServiceAdapterFactoryInterface |
153
|
|
|
*/ |
154
|
|
|
public function getFactory($serviceName) |
155
|
|
|
{ |
156
|
|
|
$factory = new $this->factories[$serviceName](); |
157
|
|
|
if (isset($this->instantiatedFactories[$serviceName])) { |
158
|
|
|
return $this->instantiatedFactories[$serviceName]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->instantiatedFactories[$serviceName] = $factory; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.