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
|
|
|
* Gets all global parameters |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getParameters() |
202
|
|
|
{ |
203
|
|
|
return $this->parameters->toArray(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sets array of parameters |
208
|
|
|
* @param array $parameterStore |
209
|
|
|
*/ |
210
|
|
|
public function setParameters(array $parameterStore) |
211
|
|
|
{ |
212
|
|
|
$this->parameters->setParameters($parameterStore); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Add some parameters |
217
|
|
|
* @param array $parameters |
218
|
|
|
*/ |
219
|
|
|
public function addParameters(array $parameters) |
220
|
|
|
{ |
221
|
|
|
$this->parameters->addParameters($parameters); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Sets a parameter with its name and value |
226
|
|
|
* |
227
|
|
|
* @param $name |
228
|
|
|
* @param mixed $value |
229
|
|
|
*/ |
230
|
|
|
public function setParameter($name, $value) |
231
|
|
|
{ |
232
|
|
|
$this->parameters->setParameter($name, $value); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Gets a parameter by given name |
237
|
|
|
* @param $name |
238
|
|
|
* @param mixed $default |
239
|
|
|
* @return mixed |
240
|
|
|
*/ |
241
|
|
|
public function getParameter($name, $default = null) |
242
|
|
|
{ |
243
|
|
|
return $this->parameters->getParameter($name, $default); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Gets a default option of the container. |
248
|
|
|
* |
249
|
|
|
* @param string $option |
250
|
|
|
* @return mixed|null|boolean |
251
|
|
|
*/ |
252
|
|
|
public function getDefault($option) |
253
|
|
|
{ |
254
|
|
|
return isset($this->defaults[$option]) ? $this->defaults[$option] : null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Configure defaults. |
259
|
|
|
* |
260
|
|
|
* @param array $defaults |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function setDefaults(array $defaults) |
264
|
|
|
{ |
265
|
|
|
return $this->defaults = array_merge($this->defaults, $defaults); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
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: