1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/di package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Slince\Di; |
13
|
|
|
|
14
|
|
|
use Slince\Di\Exception\ConfigException; |
15
|
|
|
use Slince\Di\Exception\DependencyInjectionException; |
16
|
|
|
use Slince\Di\Exception\NotFoundException; |
17
|
|
|
use Interop\Container\ContainerInterface; |
18
|
|
|
|
19
|
|
|
class Container implements ContainerInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $aliases = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Array of singletons |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $services = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Array of Definitions |
35
|
|
|
* |
36
|
|
|
* @var Definition[] |
37
|
|
|
*/ |
38
|
|
|
protected $definitions = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Array of parameters |
42
|
|
|
* @var ParameterBag |
43
|
|
|
*/ |
44
|
|
|
protected $parameters; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DefinitionResolver |
48
|
|
|
*/ |
49
|
|
|
protected $definitionResolver; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Defaults for the container. |
53
|
|
|
* |
54
|
|
|
* [ |
55
|
|
|
* 'share' => true, |
56
|
|
|
* 'autowire' => true |
57
|
|
|
* ] |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $defaults = [ |
61
|
|
|
'share' => true, |
62
|
|
|
'autowire' => true |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
$this->parameters = new ParameterBag(); |
68
|
|
|
$this->definitionResolver = new DefinitionResolver($this); |
69
|
|
|
$this->register($this); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register a definition. |
74
|
|
|
* |
75
|
|
|
* @param string $id |
76
|
|
|
* @param mixed $concrete |
77
|
|
|
* @return Definition |
78
|
|
|
*/ |
79
|
|
|
public function register($id, $concrete = null) |
80
|
|
|
{ |
81
|
|
|
if (is_object($id)) { |
82
|
|
|
$concrete = $id; |
83
|
|
|
$id = get_class($id); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//Apply defaults. |
87
|
|
|
$definition = (new Definition()) |
88
|
|
|
->setShared($this->defaults['share']) |
89
|
|
|
->setAutowired($this->defaults['autowire']); |
90
|
|
|
|
91
|
|
|
if (null === $concrete) { |
92
|
|
|
$definition->setClass($id); |
93
|
|
|
} elseif (is_string($concrete)) { |
94
|
|
|
$definition->setClass($concrete); |
95
|
|
|
} elseif ($concrete instanceof \Closure || is_array($concrete)) { |
96
|
|
|
$definition->setFactory($concrete); |
97
|
|
|
} elseif (is_object($concrete)) { |
98
|
|
|
$definition->setFactory(function() use ($concrete){ |
99
|
|
|
return $concrete; |
100
|
|
|
}) |
101
|
|
|
->setClass(get_class($concrete)) |
102
|
|
|
->setShared(true); |
103
|
|
|
} else { |
104
|
|
|
throw new ConfigException('expects a valid concrete'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$definition = $this->setDefinition($id, $definition); |
108
|
|
|
return $definition; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set a definition. |
113
|
|
|
* |
114
|
|
|
* @param string $id |
115
|
|
|
* @param Definition $definition |
116
|
|
|
* |
117
|
|
|
* @return Definition |
118
|
|
|
*/ |
119
|
|
|
public function setDefinition($id, Definition $definition) |
120
|
|
|
{ |
121
|
|
|
$id = (string) $id; |
122
|
|
|
return $this->definitions[$id] = $definition; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets an alias for an existing service. |
127
|
|
|
* |
128
|
|
|
* @param string $alias |
129
|
|
|
* @param string $id |
130
|
|
|
*/ |
131
|
|
|
public function setAlias($alias, $id) |
132
|
|
|
{ |
133
|
|
|
$this->aliases[$alias] = $id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get id of the alias. |
138
|
|
|
* |
139
|
|
|
* @param string $alias |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
|
|
public function getAlias($alias) |
143
|
|
|
{ |
144
|
|
|
return isset($this->aliases[$alias]) ? $this->aliases[$alias] : null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get a service instance by specified ID |
149
|
|
|
* |
150
|
|
|
* @param string $id |
151
|
|
|
* @return object |
152
|
|
|
*/ |
153
|
|
|
public function get($id) |
154
|
|
|
{ |
155
|
|
|
if (isset($this->aliases[$id])) { |
156
|
|
|
$id = $this->aliases[$id]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//If service is singleton, return instance directly. |
160
|
|
|
if (isset($this->services[$id])) { |
161
|
|
|
return $this->services[$id]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
//If there is no matching definition, creates an definition automatically |
165
|
|
|
if (!isset($this->definitions[$id])) { |
166
|
|
|
if (class_exists($id)) { |
167
|
|
|
$this->register($id); |
168
|
|
|
} else { |
169
|
|
|
throw new NotFoundException(sprintf('There is no definition named "%s"', $id)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// resolve instance. |
174
|
|
|
$instance = $this->definitionResolver->resolve($this->definitions[$id]); |
175
|
|
|
|
176
|
|
|
//If the service be set as singleton mode, stores its instance |
177
|
|
|
if ($this->definitions[$id]->isShared()) { |
178
|
|
|
$this->services[$id] = $instance; |
179
|
|
|
} |
180
|
|
|
return $instance; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function has($id) |
187
|
|
|
{ |
188
|
|
|
if (isset($this->services[$id])) { |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
if (!isset($this->definitions[$id]) && class_exists($id)) { |
192
|
|
|
$this->register($id); |
193
|
|
|
} |
194
|
|
|
return isset($this->definitions[$id]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns service ids for a given tag. |
199
|
|
|
* |
200
|
|
|
* Example: |
201
|
|
|
* |
202
|
|
|
* $container->register('foo')->addTag('my.tag', array('hello' => 'world')); |
203
|
|
|
* |
204
|
|
|
* $serviceIds = $container->findTaggedServiceIds('my.tag'); |
205
|
|
|
* foreach ($serviceIds as $serviceId => $tags) { |
206
|
|
|
* foreach ($tags as $tag) { |
207
|
|
|
* echo $tag['hello']; |
208
|
|
|
* } |
209
|
|
|
* } |
210
|
|
|
* @param string $name |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function findTaggedServiceIds($name) |
214
|
|
|
{ |
215
|
|
|
$tags = array(); |
216
|
|
|
foreach ($this->definitions as $id => $definition) { |
217
|
|
|
if ($definition->hasTag($name)) { |
218
|
|
|
$tags[$id] = $definition->getTag($name); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
return $tags; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets all global parameters |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public function getParameters() |
229
|
|
|
{ |
230
|
|
|
return $this->parameters->toArray(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Sets array of parameters |
235
|
|
|
* @param array $parameterStore |
236
|
|
|
*/ |
237
|
|
|
public function setParameters(array $parameterStore) |
238
|
|
|
{ |
239
|
|
|
$this->parameters->setParameters($parameterStore); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add some parameters |
244
|
|
|
* @param array $parameters |
245
|
|
|
*/ |
246
|
|
|
public function addParameters(array $parameters) |
247
|
|
|
{ |
248
|
|
|
$this->parameters->addParameters($parameters); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Sets a parameter with its name and value |
253
|
|
|
* |
254
|
|
|
* @param $name |
255
|
|
|
* @param mixed $value |
256
|
|
|
*/ |
257
|
|
|
public function setParameter($name, $value) |
258
|
|
|
{ |
259
|
|
|
$this->parameters->setParameter($name, $value); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Gets a parameter by given name |
264
|
|
|
* @param $name |
265
|
|
|
* @param mixed $default |
266
|
|
|
* @return mixed |
267
|
|
|
*/ |
268
|
|
|
public function getParameter($name, $default = null) |
269
|
|
|
{ |
270
|
|
|
return $this->parameters->getParameter($name, $default); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Gets a default option of the container. |
275
|
|
|
* |
276
|
|
|
* @param string $option |
277
|
|
|
* @return mixed|null|boolean |
278
|
|
|
*/ |
279
|
|
|
public function getDefault($option) |
280
|
|
|
{ |
281
|
|
|
return isset($this->defaults[$option]) ? $this->defaults[$option] : null; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Configure defaults. |
286
|
|
|
* |
287
|
|
|
* @param array $defaults |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
|
|
public function setDefaults(array $defaults) |
291
|
|
|
{ |
292
|
|
|
return $this->defaults = array_merge($this->defaults, $defaults); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: