11
11
12
12
namespace PhpBench \DependencyInjection ;
13
13
14
+ use Closure ;
14
15
use Psr \Container \ContainerInterface ;
16
+ use Symfony \Component \OptionsResolver \Exception \ExceptionInterface ;
17
+ use Symfony \Component \OptionsResolver \OptionsResolver ;
15
18
16
19
/**
17
20
* PHPBench Container.
20
23
*/
21
24
class Container implements ContainerInterface
22
25
{
26
+ /**
27
+ * @var array<string,Callable>
28
+ */
23
29
private $ instantiators = [];
30
+
31
+ /**
32
+ * @var array<string, mixed>
33
+ */
24
34
private $ services = [];
35
+
36
+ /**
37
+ * @var array<string,array<string,mixed>>
38
+ */
25
39
private $ tags = [];
40
+
41
+ /**
42
+ * @var array<string,mixed>
43
+ */
26
44
private $ config = [];
27
- private $ userConfig = [];
28
45
46
+ /**
47
+ * @var array<string>
48
+ */
29
49
private $ extensionClasses = [];
30
50
31
- public function __construct (array $ extensionClasses = [], array $ userConfig = [])
51
+ /**
52
+ * @param array<string,mixed> $config
53
+ * @param array<string> $extensionClasses
54
+ */
55
+ public function __construct (array $ extensionClasses = [], array $ config = [])
32
56
{
33
57
$ this ->extensionClasses = $ extensionClasses ;
34
- $ this ->userConfig = $ userConfig ;
58
+ $ this ->config = $ config ;
35
59
}
36
60
37
61
/**
@@ -41,11 +65,13 @@ public function __construct(array $extensionClasses = [], array $userConfig = []
41
65
*
42
66
* This method must be called before `build()`.
43
67
*/
44
- public function init ()
68
+ public function init (): void
45
69
{
70
+ $ resolver = new OptionsResolver ();
46
71
$ extensions = [];
72
+ $ config = [];
47
73
48
- if (empty ($ this ->extensionClasses ) && empty ($ this ->userConfig )) {
74
+ if (empty ($ this ->extensionClasses ) && empty ($ this ->config )) {
49
75
return ;
50
76
}
51
77
@@ -68,26 +94,27 @@ public function init()
68
94
}
69
95
70
96
$ extensions [] = $ extension ;
71
-
72
- $ this ->config = array_merge (
73
- $ this ->config ,
74
- $ extension ->getDefaultConfig ()
75
- );
97
+ $ extension ->configure ($ resolver );
76
98
}
77
99
78
- $ diff = array_diff (array_keys ($ this ->userConfig ), array_keys ($ this ->config ));
100
+ $ diff = array_diff (array_keys ($ this ->config ), array_keys ($ this ->config ));
79
101
80
102
if ($ diff ) {
81
103
throw new \InvalidArgumentException (sprintf (
82
104
'Unknown configuration keys: "%s". Permitted keys: "%s" ' ,
83
- implode ('", " ' , $ diff ), implode ('", " ' , array_keys ($ this ->config ))
105
+ implode ('", " ' , $ diff ),
106
+ implode ('", " ' , array_keys ($ this ->config ))
84
107
));
85
108
}
86
109
87
- $ this ->config = array_merge (
88
- $ this ->config ,
89
- $ this ->userConfig
90
- );
110
+ try {
111
+ $ this ->config = $ resolver ->resolve ($ this ->config );
112
+ } catch (ExceptionInterface $ resolverException ) {
113
+ throw new InvalidConfigurationException (sprintf (
114
+ 'Invalid user configuration: %s ' ,
115
+ $ resolverException ->getMessage ()
116
+ ), 0 , $ resolverException );
117
+ }
91
118
92
119
foreach ($ extensions as $ extension ) {
93
120
$ extension ->load ($ this );
@@ -99,8 +126,6 @@ public function init()
99
126
* Note that this method will return the same instance on subsequent calls.
100
127
*
101
128
* @param string $serviceId
102
- *
103
- * @return mixed
104
129
*/
105
130
public function get ($ serviceId )
106
131
{
@@ -120,30 +145,30 @@ public function get($serviceId)
120
145
return $ this ->services [$ serviceId ];
121
146
}
122
147
123
- public function has ($ serviceId )
148
+ /**
149
+ * @param string $serviceId
150
+ */
151
+ public function has ($ serviceId ): bool
124
152
{
125
153
return isset ($ this ->instantiators [$ serviceId ]);
126
154
}
127
155
128
156
/**
129
157
* Set a service instance.
130
158
*
131
- * @param string $serviceId
132
159
* @param mixed $instance
133
160
*/
134
- public function set ($ serviceId , $ instance )
161
+ public function set (string $ serviceId , $ instance ): void
135
162
{
136
163
$ this ->services [$ serviceId ] = $ instance ;
137
164
}
138
165
139
166
/**
140
167
* Return services IDs for the given tag.
141
168
*
142
- * @param string $tag
143
- *
144
- * @return string[][]
169
+ * @return array<string, array<string, mixed>>
145
170
*/
146
- public function getServiceIdsForTag ($ tag )
171
+ public function getServiceIdsForTag (string $ tag ): array
147
172
{
148
173
$ serviceIds = [];
149
174
foreach ($ this ->tags as $ serviceId => $ tags ) {
@@ -161,15 +186,15 @@ public function getServiceIdsForTag($tag)
161
186
* The instantiator is a closure which accepts an instance of this container and
162
187
* returns a new instance of the service class.
163
188
*
164
- * @param string $serviceId
165
- * @param \Closure $instantiator
166
- * @param string[][] $tags
189
+ * @param array<string, array<string, mixed>> $tags
167
190
*/
168
- public function register ($ serviceId , \ Closure $ instantiator , array $ tags = [])
191
+ public function register (string $ serviceId , Closure $ instantiator , array $ tags = []): void
169
192
{
170
193
if (isset ($ this ->instantiators [$ serviceId ])) {
171
194
throw new \InvalidArgumentException (sprintf (
172
- 'Service with ID "%s" has already been registered ' , $ serviceId ));
195
+ 'Service with ID "%s" has already been registered ' ,
196
+ $ serviceId
197
+ ));
173
198
}
174
199
175
200
$ this ->instantiators [$ serviceId ] = $ instantiator ;
@@ -179,15 +204,17 @@ public function register($serviceId, \Closure $instantiator, array $tags = [])
179
204
/**
180
205
* Set the value of the parameter with the given name.
181
206
*
182
- * @param string $name
183
207
* @param mixed $value
184
208
*/
185
- public function setParameter ($ name , $ value )
209
+ public function setParameter (string $ name , $ value ): void
186
210
{
187
211
$ this ->config [$ name ] = $ value ;
188
212
}
189
213
190
- public function mergeParameter ($ name , array $ values )
214
+ /**
215
+ * @param array<mixed> $values
216
+ */
217
+ public function mergeParameter (string $ name , array $ values ): void
191
218
{
192
219
$ actual = $ this ->getParameter ($ name );
193
220
@@ -225,7 +252,10 @@ public function getParameter($name)
225
252
return $ this ->config [$ name ];
226
253
}
227
254
228
- public function getParameters ()
255
+ /**
256
+ * @return array<mixed,mixed>
257
+ */
258
+ public function getParameters (): array
229
259
{
230
260
return $ this ->config ;
231
261
}
0 commit comments