1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\di; |
9
|
|
|
|
10
|
|
|
use ReflectionClass; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Component; |
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Container implements a [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) container. |
18
|
|
|
* |
19
|
|
|
* A dependency injection (DI) container is an object that knows how to instantiate and configure objects and |
20
|
|
|
* all their dependent objects. For more information about DI, please refer to |
21
|
|
|
* [Martin Fowler's article](http://martinfowler.com/articles/injection.html). |
22
|
|
|
* |
23
|
|
|
* Container supports constructor injection as well as property injection. |
24
|
|
|
* |
25
|
|
|
* To use Container, you first need to set up the class dependencies by calling [[set()]]. |
26
|
|
|
* You then call [[get()]] to create a new class object. Container will automatically instantiate |
27
|
|
|
* dependent objects, inject them into the object being created, configure and finally return the newly created object. |
28
|
|
|
* |
29
|
|
|
* By default, [[\Yii::$container]] refers to a Container instance which is used by [[\Yii::createObject()]] |
30
|
|
|
* to create new object instances. You may use this method to replace the `new` operator |
31
|
|
|
* when creating a new object, which gives you the benefit of automatic dependency resolution and default |
32
|
|
|
* property configuration. |
33
|
|
|
* |
34
|
|
|
* Below is an example of using Container: |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* namespace app\models; |
38
|
|
|
* |
39
|
|
|
* use yii\base\Object; |
40
|
|
|
* use yii\db\Connection; |
41
|
|
|
* use yii\di\Container; |
42
|
|
|
* |
43
|
|
|
* interface UserFinderInterface |
44
|
|
|
* { |
45
|
|
|
* function findUser(); |
46
|
|
|
* } |
47
|
|
|
* |
48
|
|
|
* class UserFinder extends Object implements UserFinderInterface |
49
|
|
|
* { |
50
|
|
|
* public $db; |
51
|
|
|
* |
52
|
|
|
* public function __construct(Connection $db, $config = []) |
53
|
|
|
* { |
54
|
|
|
* $this->db = $db; |
55
|
|
|
* parent::__construct($config); |
56
|
|
|
* } |
57
|
|
|
* |
58
|
|
|
* public function findUser() |
59
|
|
|
* { |
60
|
|
|
* } |
61
|
|
|
* } |
62
|
|
|
* |
63
|
|
|
* class UserLister extends Object |
64
|
|
|
* { |
65
|
|
|
* public $finder; |
66
|
|
|
* |
67
|
|
|
* public function __construct(UserFinderInterface $finder, $config = []) |
68
|
|
|
* { |
69
|
|
|
* $this->finder = $finder; |
70
|
|
|
* parent::__construct($config); |
71
|
|
|
* } |
72
|
|
|
* } |
73
|
|
|
* |
74
|
|
|
* $container = new Container; |
75
|
|
|
* $container->set('yii\db\Connection', [ |
76
|
|
|
* 'dsn' => '...', |
77
|
|
|
* ]); |
78
|
|
|
* $container->set('app\models\UserFinderInterface', [ |
79
|
|
|
* 'class' => 'app\models\UserFinder', |
80
|
|
|
* ]); |
81
|
|
|
* $container->set('userLister', 'app\models\UserLister'); |
82
|
|
|
* |
83
|
|
|
* $lister = $container->get('userLister'); |
84
|
|
|
* |
85
|
|
|
* // which is equivalent to: |
86
|
|
|
* |
87
|
|
|
* $db = new \yii\db\Connection(['dsn' => '...']); |
88
|
|
|
* $finder = new UserFinder($db); |
89
|
|
|
* $lister = new UserLister($finder); |
90
|
|
|
* ``` |
91
|
|
|
* |
92
|
|
|
* @property array $definitions The list of the object definitions or the loaded shared objects (type or ID => |
93
|
|
|
* definition or instance). This property is read-only. |
94
|
|
|
* |
95
|
|
|
* @author Qiang Xue <[email protected]> |
96
|
|
|
* @since 2.0 |
97
|
|
|
*/ |
98
|
|
|
class Container extends Component |
99
|
|
|
{ |
100
|
|
|
/** |
101
|
|
|
* @var array singleton objects indexed by their types |
102
|
|
|
*/ |
103
|
|
|
private $_singletons = []; |
104
|
|
|
/** |
105
|
|
|
* @var array object definitions indexed by their types |
106
|
|
|
*/ |
107
|
|
|
private $_definitions = []; |
108
|
|
|
/** |
109
|
|
|
* @var array constructor parameters indexed by object types |
110
|
|
|
*/ |
111
|
|
|
private $_params = []; |
112
|
|
|
/** |
113
|
|
|
* @var array cached ReflectionClass objects indexed by class/interface names |
114
|
|
|
*/ |
115
|
|
|
private $_reflections = []; |
116
|
|
|
/** |
117
|
|
|
* @var array cached dependencies indexed by class/interface names. Each class name |
118
|
|
|
* is associated with a list of constructor parameter types or default values. |
119
|
|
|
*/ |
120
|
|
|
private $_dependencies = []; |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns an instance of the requested class. |
125
|
|
|
* |
126
|
|
|
* You may provide constructor parameters (`$params`) and object configurations (`$config`) |
127
|
|
|
* that will be used during the creation of the instance. |
128
|
|
|
* |
129
|
|
|
* If the class implements [[\yii\base\Configurable]], the `$config` parameter will be passed as the last |
130
|
|
|
* parameter to the class constructor; Otherwise, the configuration will be applied *after* the object is |
131
|
|
|
* instantiated. |
132
|
|
|
* |
133
|
|
|
* Note that if the class is declared to be singleton by calling [[setSingleton()]], |
134
|
|
|
* the same instance of the class will be returned each time this method is called. |
135
|
|
|
* In this case, the constructor parameters and object configurations will be used |
136
|
|
|
* only if the class is instantiated the first time. |
137
|
|
|
* |
138
|
|
|
* @param string $class the class name or an alias name (e.g. `foo`) that was previously registered via [[set()]] |
139
|
|
|
* or [[setSingleton()]]. |
140
|
|
|
* @param array $params a list of constructor parameter values. The parameters should be provided in the order |
141
|
|
|
* they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining |
142
|
|
|
* ones with the integers that represent their positions in the constructor parameter list. |
143
|
|
|
* @param array $config a list of name-value pairs that will be used to initialize the object properties. |
144
|
|
|
* @return object an instance of the requested class. |
145
|
|
|
* @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition |
146
|
|
|
*/ |
147
|
1535 |
|
public function get($class, $params = [], $config = []) |
148
|
|
|
{ |
149
|
1535 |
|
if (isset($this->_singletons[$class])) { |
150
|
|
|
// singleton |
151
|
1 |
|
return $this->_singletons[$class]; |
152
|
1535 |
|
} elseif (!isset($this->_definitions[$class])) { |
153
|
1535 |
|
return $this->build($class, $params, $config); |
154
|
|
|
} |
155
|
|
|
|
156
|
11 |
|
$definition = $this->_definitions[$class]; |
157
|
|
|
|
158
|
11 |
|
if (is_callable($definition, true)) { |
159
|
1 |
|
$params = $this->resolveDependencies($this->mergeParams($class, $params)); |
160
|
1 |
|
$object = call_user_func($definition, $this, $params, $config); |
161
|
11 |
|
} elseif (is_array($definition)) { |
162
|
11 |
|
$concrete = $definition['class']; |
163
|
11 |
|
unset($definition['class']); |
164
|
|
|
|
165
|
11 |
|
$config = array_merge($definition, $config); |
166
|
11 |
|
$params = $this->mergeParams($class, $params); |
167
|
|
|
|
168
|
11 |
|
if ($concrete === $class) { |
169
|
1 |
|
$object = $this->build($class, $params, $config); |
170
|
1 |
|
} else { |
171
|
11 |
|
$object = $this->get($concrete, $params, $config); |
172
|
|
|
} |
173
|
11 |
|
} elseif (is_object($definition)) { |
174
|
1 |
|
return $this->_singletons[$class] = $definition; |
175
|
|
|
} else { |
176
|
|
|
throw new InvalidConfigException('Unexpected object definition type: ' . gettype($definition)); |
177
|
|
|
} |
178
|
|
|
|
179
|
11 |
|
if (array_key_exists($class, $this->_singletons)) { |
180
|
|
|
// singleton |
181
|
|
|
$this->_singletons[$class] = $object; |
182
|
|
|
} |
183
|
|
|
|
184
|
11 |
|
return $object; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Registers a class definition with this container. |
189
|
|
|
* |
190
|
|
|
* For example, |
191
|
|
|
* |
192
|
|
|
* ```php |
193
|
|
|
* // register a class name as is. This can be skipped. |
194
|
|
|
* $container->set('yii\db\Connection'); |
195
|
|
|
* |
196
|
|
|
* // register an interface |
197
|
|
|
* // When a class depends on the interface, the corresponding class |
198
|
|
|
* // will be instantiated as the dependent object |
199
|
|
|
* $container->set('yii\mail\MailInterface', 'yii\swiftmailer\Mailer'); |
200
|
|
|
* |
201
|
|
|
* // register an alias name. You can use $container->get('foo') |
202
|
|
|
* // to create an instance of Connection |
203
|
|
|
* $container->set('foo', 'yii\db\Connection'); |
204
|
|
|
* |
205
|
|
|
* // register a class with configuration. The configuration |
206
|
|
|
* // will be applied when the class is instantiated by get() |
207
|
|
|
* $container->set('yii\db\Connection', [ |
208
|
|
|
* 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
209
|
|
|
* 'username' => 'root', |
210
|
|
|
* 'password' => '', |
211
|
|
|
* 'charset' => 'utf8', |
212
|
|
|
* ]); |
213
|
|
|
* |
214
|
|
|
* // register an alias name with class configuration |
215
|
|
|
* // In this case, a "class" element is required to specify the class |
216
|
|
|
* $container->set('db', [ |
217
|
|
|
* 'class' => 'yii\db\Connection', |
218
|
|
|
* 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
219
|
|
|
* 'username' => 'root', |
220
|
|
|
* 'password' => '', |
221
|
|
|
* 'charset' => 'utf8', |
222
|
|
|
* ]); |
223
|
|
|
* |
224
|
|
|
* // register a PHP callable |
225
|
|
|
* // The callable will be executed when $container->get('db') is called |
226
|
|
|
* $container->set('db', function ($container, $params, $config) { |
227
|
|
|
* return new \yii\db\Connection($config); |
228
|
|
|
* }); |
229
|
|
|
* ``` |
230
|
|
|
* |
231
|
|
|
* If a class definition with the same name already exists, it will be overwritten with the new one. |
232
|
|
|
* You may use [[has()]] to check if a class definition already exists. |
233
|
|
|
* |
234
|
|
|
* @param string $class class name, interface name or alias name |
235
|
|
|
* @param mixed $definition the definition associated with `$class`. It can be one of the following: |
236
|
|
|
* |
237
|
|
|
* - a PHP callable: The callable will be executed when [[get()]] is invoked. The signature of the callable |
238
|
|
|
* should be `function ($container, $params, $config)`, where `$params` stands for the list of constructor |
239
|
|
|
* parameters, `$config` the object configuration, and `$container` the container object. The return value |
240
|
|
|
* of the callable will be returned by [[get()]] as the object instance requested. |
241
|
|
|
* - a configuration array: the array contains name-value pairs that will be used to initialize the property |
242
|
|
|
* values of the newly created object when [[get()]] is called. The `class` element stands for the |
243
|
|
|
* the class of the object to be created. If `class` is not specified, `$class` will be used as the class name. |
244
|
|
|
* - a string: a class name, an interface name or an alias name. |
245
|
|
|
* @param array $params the list of constructor parameters. The parameters will be passed to the class |
246
|
|
|
* constructor when [[get()]] is called. |
247
|
|
|
* @return $this the container itself |
248
|
|
|
*/ |
249
|
326 |
|
public function set($class, $definition = [], array $params = []) |
250
|
|
|
{ |
251
|
326 |
|
$this->_definitions[$class] = $this->normalizeDefinition($class, $definition); |
|
|
|
|
252
|
326 |
|
$this->_params[$class] = $params; |
253
|
326 |
|
unset($this->_singletons[$class]); |
254
|
326 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Registers a class definition with this container and marks the class as a singleton class. |
259
|
|
|
* |
260
|
|
|
* This method is similar to [[set()]] except that classes registered via this method will only have one |
261
|
|
|
* instance. Each time [[get()]] is called, the same instance of the specified class will be returned. |
262
|
|
|
* |
263
|
|
|
* @param string $class class name, interface name or alias name |
264
|
|
|
* @param mixed $definition the definition associated with `$class`. See [[set()]] for more details. |
265
|
|
|
* @param array $params the list of constructor parameters. The parameters will be passed to the class |
266
|
|
|
* constructor when [[get()]] is called. |
267
|
|
|
* @return $this the container itself |
268
|
|
|
* @see set() |
269
|
|
|
*/ |
270
|
|
|
public function setSingleton($class, $definition = [], array $params = []) |
271
|
|
|
{ |
272
|
|
|
$this->_definitions[$class] = $this->normalizeDefinition($class, $definition); |
|
|
|
|
273
|
|
|
$this->_params[$class] = $params; |
274
|
|
|
$this->_singletons[$class] = null; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
280
|
|
|
* @param string $class class name, interface name or alias name |
281
|
|
|
* @return boolean whether the container has the definition of the specified name.. |
282
|
|
|
* @see set() |
283
|
|
|
*/ |
284
|
|
|
public function has($class) |
285
|
|
|
{ |
286
|
|
|
return isset($this->_definitions[$class]); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns a value indicating whether the given name corresponds to a registered singleton. |
291
|
|
|
* @param string $class class name, interface name or alias name |
292
|
|
|
* @param boolean $checkInstance whether to check if the singleton has been instantiated. |
293
|
|
|
* @return boolean whether the given name corresponds to a registered singleton. If `$checkInstance` is true, |
294
|
|
|
* the method should return a value indicating whether the singleton has been instantiated. |
295
|
|
|
*/ |
296
|
|
|
public function hasSingleton($class, $checkInstance = false) |
297
|
|
|
{ |
298
|
|
|
return $checkInstance ? isset($this->_singletons[$class]) : array_key_exists($class, $this->_singletons); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Removes the definition for the specified name. |
303
|
|
|
* @param string $class class name, interface name or alias name |
304
|
|
|
*/ |
305
|
|
|
public function clear($class) |
306
|
|
|
{ |
307
|
|
|
unset($this->_definitions[$class], $this->_singletons[$class]); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Normalizes the class definition. |
312
|
|
|
* @param string $class class name |
313
|
|
|
* @param string|array|callable $definition the class definition |
314
|
|
|
* @return array the normalized class definition |
315
|
|
|
* @throws InvalidConfigException if the definition is invalid. |
316
|
|
|
*/ |
317
|
326 |
|
protected function normalizeDefinition($class, $definition) |
318
|
|
|
{ |
319
|
326 |
|
if (empty($definition)) { |
320
|
1 |
|
return ['class' => $class]; |
321
|
326 |
|
} elseif (is_string($definition)) { |
322
|
1 |
|
return ['class' => $definition]; |
323
|
326 |
|
} elseif (is_callable($definition, true) || is_object($definition)) { |
324
|
316 |
|
return $definition; |
325
|
10 |
|
} elseif (is_array($definition)) { |
326
|
10 |
|
if (!isset($definition['class'])) { |
327
|
|
|
if (strpos($class, '\\') !== false) { |
328
|
|
|
$definition['class'] = $class; |
329
|
|
|
} else { |
330
|
|
|
throw new InvalidConfigException("A class definition requires a \"class\" member."); |
331
|
|
|
} |
332
|
|
|
} |
333
|
10 |
|
return $definition; |
334
|
|
|
} else { |
335
|
|
|
throw new InvalidConfigException("Unsupported definition type for \"$class\": " . gettype($definition)); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns the list of the object definitions or the loaded shared objects. |
341
|
|
|
* @return array the list of the object definitions or the loaded shared objects (type or ID => definition or instance). |
342
|
|
|
*/ |
343
|
|
|
public function getDefinitions() |
344
|
|
|
{ |
345
|
|
|
return $this->_definitions; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Creates an instance of the specified class. |
350
|
|
|
* This method will resolve dependencies of the specified class, instantiate them, and inject |
351
|
|
|
* them into the new instance of the specified class. |
352
|
|
|
* @param string $class the class name |
353
|
|
|
* @param array $params constructor parameters |
354
|
|
|
* @param array $config configurations to be applied to the new instance |
355
|
|
|
* @return object the newly created instance of the specified class |
356
|
|
|
*/ |
357
|
1535 |
|
protected function build($class, $params, $config) |
358
|
|
|
{ |
359
|
|
|
/* @var $reflection ReflectionClass */ |
360
|
1535 |
|
list ($reflection, $dependencies) = $this->getDependencies($class); |
361
|
|
|
|
362
|
1534 |
|
foreach ($params as $index => $param) { |
363
|
233 |
|
$dependencies[$index] = $param; |
364
|
1534 |
|
} |
365
|
|
|
|
366
|
1534 |
|
$dependencies = $this->resolveDependencies($dependencies, $reflection); |
367
|
1534 |
|
if (empty($config)) { |
368
|
1005 |
|
return $reflection->newInstanceArgs($dependencies); |
369
|
|
|
} |
370
|
|
|
|
371
|
1484 |
|
if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable')) { |
372
|
|
|
// set $config as the last parameter (existing one will be overwritten) |
373
|
1483 |
|
$dependencies[count($dependencies) - 1] = $config; |
374
|
1483 |
|
return $reflection->newInstanceArgs($dependencies); |
375
|
|
|
} else { |
376
|
1 |
|
$object = $reflection->newInstanceArgs($dependencies); |
377
|
1 |
|
foreach ($config as $name => $value) { |
378
|
1 |
|
$object->$name = $value; |
379
|
1 |
|
} |
380
|
1 |
|
return $object; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Merges the user-specified constructor parameters with the ones registered via [[set()]]. |
386
|
|
|
* @param string $class class name, interface name or alias name |
387
|
|
|
* @param array $params the constructor parameters |
388
|
|
|
* @return array the merged parameters |
389
|
|
|
*/ |
390
|
11 |
|
protected function mergeParams($class, $params) |
391
|
|
|
{ |
392
|
11 |
|
if (empty($this->_params[$class])) { |
393
|
11 |
|
return $params; |
394
|
1 |
|
} elseif (empty($params)) { |
395
|
1 |
|
return $this->_params[$class]; |
396
|
|
|
} else { |
397
|
|
|
$ps = $this->_params[$class]; |
398
|
|
|
foreach ($params as $index => $value) { |
399
|
|
|
$ps[$index] = $value; |
400
|
|
|
} |
401
|
|
|
return $ps; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Returns the dependencies of the specified class. |
407
|
|
|
* @param string $class class name, interface name or alias name |
408
|
|
|
* @return array the dependencies of the specified class. |
409
|
|
|
*/ |
410
|
1535 |
|
protected function getDependencies($class) |
411
|
|
|
{ |
412
|
1535 |
|
if (isset($this->_reflections[$class])) { |
413
|
1520 |
|
return [$this->_reflections[$class], $this->_dependencies[$class]]; |
414
|
|
|
} |
415
|
|
|
|
416
|
80 |
|
$dependencies = []; |
417
|
80 |
|
$reflection = new ReflectionClass($class); |
418
|
|
|
|
419
|
79 |
|
$constructor = $reflection->getConstructor(); |
420
|
79 |
|
if ($constructor !== null) { |
421
|
78 |
|
foreach ($constructor->getParameters() as $param) { |
422
|
78 |
|
if ($param->isDefaultValueAvailable()) { |
423
|
78 |
|
$dependencies[] = $param->getDefaultValue(); |
424
|
78 |
|
} else { |
425
|
9 |
|
$c = $param->getClass(); |
426
|
9 |
|
$dependencies[] = Instance::of($c === null ? null : $c->getName()); |
427
|
|
|
} |
428
|
78 |
|
} |
429
|
78 |
|
} |
430
|
|
|
|
431
|
79 |
|
$this->_reflections[$class] = $reflection; |
432
|
79 |
|
$this->_dependencies[$class] = $dependencies; |
433
|
|
|
|
434
|
79 |
|
return [$reflection, $dependencies]; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Resolves dependencies by replacing them with the actual object instances. |
439
|
|
|
* @param array $dependencies the dependencies |
440
|
|
|
* @param ReflectionClass $reflection the class reflection associated with the dependencies |
441
|
|
|
* @return array the resolved dependencies |
442
|
|
|
* @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
443
|
|
|
*/ |
444
|
1534 |
|
protected function resolveDependencies($dependencies, $reflection = null) |
445
|
|
|
{ |
446
|
1534 |
|
foreach ($dependencies as $index => $dependency) { |
447
|
1533 |
|
if ($dependency instanceof Instance) { |
448
|
2 |
|
if ($dependency->id !== null) { |
449
|
2 |
|
$dependencies[$index] = $this->get($dependency->id); |
450
|
2 |
|
} elseif ($reflection !== null) { |
451
|
|
|
$name = $reflection->getConstructor()->getParameters()[$index]->getName(); |
452
|
|
|
$class = $reflection->getName(); |
453
|
|
|
throw new InvalidConfigException("Missing required parameter \"$name\" when instantiating \"$class\"."); |
454
|
|
|
} |
455
|
2 |
|
} |
456
|
1534 |
|
} |
457
|
1534 |
|
return $dependencies; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Invoke a callback with resolving dependencies in parameters. |
462
|
|
|
* |
463
|
|
|
* This methods allows invoking a callback and let type hinted parameter names to be |
464
|
|
|
* resolved as objects of the Container. It additionally allow calling function using named parameters. |
465
|
|
|
* |
466
|
|
|
* For example, the following callback may be invoked using the Container to resolve the formatter dependency: |
467
|
|
|
* |
468
|
|
|
* ```php |
469
|
|
|
* $formatString = function($string, \yii\i18n\Formatter $formatter) { |
470
|
|
|
* // ... |
471
|
|
|
* } |
472
|
|
|
* Yii::$container->invoke($formatString, ['string' => 'Hello World!']); |
473
|
|
|
* ``` |
474
|
|
|
* |
475
|
|
|
* This will pass the string `'Hello World!'` as the first param, and a formatter instance created |
476
|
|
|
* by the DI container as the second param to the callable. |
477
|
|
|
* |
478
|
|
|
* @param callable $callback callable to be invoked. |
479
|
|
|
* @param array $params The array of parameters for the function. |
480
|
|
|
* This can be either a list of parameters, or an associative array representing named function parameters. |
481
|
|
|
* @return mixed the callback return value. |
482
|
|
|
* @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
483
|
|
|
* @since 2.0.7 |
484
|
|
|
*/ |
485
|
4 |
|
public function invoke(callable $callback, $params = []) |
486
|
|
|
{ |
487
|
4 |
|
if (is_callable($callback)) { |
488
|
4 |
|
return call_user_func_array($callback, $this->resolveCallableDependencies($callback, $params)); |
489
|
|
|
} else { |
490
|
|
|
return call_user_func_array($callback, $params); |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Resolve dependencies for a function. |
496
|
|
|
* |
497
|
|
|
* This method can be used to implement similar functionality as provided by [[invoke()]] in other |
498
|
|
|
* components. |
499
|
|
|
* |
500
|
|
|
* @param callable $callback callable to be invoked. |
501
|
|
|
* @param array $params The array of parameters for the function, can be either numeric or associative. |
502
|
|
|
* @return array The resolved dependencies. |
503
|
|
|
* @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
504
|
|
|
* @since 2.0.7 |
505
|
|
|
*/ |
506
|
5 |
|
public function resolveCallableDependencies(callable $callback, $params = []) |
507
|
|
|
{ |
508
|
5 |
|
if (is_array($callback)) { |
509
|
2 |
|
$reflection = new \ReflectionMethod($callback[0], $callback[1]); |
510
|
2 |
|
} else { |
511
|
5 |
|
$reflection = new \ReflectionFunction($callback); |
512
|
|
|
} |
513
|
|
|
|
514
|
5 |
|
$args = []; |
515
|
|
|
|
516
|
5 |
|
$associative = ArrayHelper::isAssociative($params); |
517
|
|
|
|
518
|
5 |
|
foreach ($reflection->getParameters() as $param) { |
519
|
4 |
|
$name = $param->getName(); |
520
|
4 |
|
if (($class = $param->getClass()) !== null) { |
521
|
2 |
|
$className = $class->getName(); |
522
|
2 |
|
if ($associative && isset($params[$name]) && $params[$name] instanceof $className) { |
523
|
|
|
$args[] = $params[$name]; |
524
|
|
|
unset($params[$name]); |
525
|
2 |
|
} elseif (!$associative && isset($params[0]) && $params[0] instanceof $className) { |
526
|
1 |
|
$args[] = array_shift($params); |
527
|
2 |
|
} elseif (isset(Yii::$app) && Yii::$app->has($name) && ($obj = Yii::$app->get($name)) instanceof $className) { |
528
|
1 |
|
$args[] = $obj; |
529
|
1 |
|
} else { |
530
|
2 |
|
$args[] = $this->get($className); |
531
|
|
|
} |
532
|
4 |
|
} elseif ($associative && isset($params[$name])) { |
533
|
2 |
|
$args[] = $params[$name]; |
534
|
2 |
|
unset($params[$name]); |
535
|
4 |
|
} elseif (!$associative && count($params)) { |
536
|
3 |
|
$args[] = array_shift($params); |
537
|
4 |
|
} elseif ($param->isDefaultValueAvailable()) { |
538
|
3 |
|
$args[] = $param->getDefaultValue(); |
539
|
3 |
|
} elseif (!$param->isOptional()) { |
540
|
|
|
$funcName = $reflection->getName(); |
541
|
|
|
throw new InvalidConfigException("Missing required parameter \"$name\" when calling \"$funcName\"."); |
542
|
|
|
} |
543
|
5 |
|
} |
544
|
|
|
|
545
|
5 |
|
foreach ($params as $value) { |
546
|
|
|
$args[] = $value; |
547
|
5 |
|
} |
548
|
5 |
|
return $args; |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
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: