Completed
Push — master ( b0867d...8ab030 )
by Alexander
24s queued 11s
created

DynamicReference   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 17
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A to() 0 3 1
A resolve() 0 3 1
1
<?php
2
namespace yii\di;
3
4
use Psr\Container\ContainerInterface;
5
use yii\di\contracts\Definition;
6
use yii\di\definitions\Normalizer;
7
8
/**
9
 * Class DynamicReference allows us to define a dependency to a service not defined in the container.
10
 * This class implements the array configuration syntax common to Yii
11
 * For example:
12
 * ```php
13
 * [
14
 *    InterfaceA::class => ConcreteA::class,
15
 *    'alternativeForA' => ConcreteB::class,
16
 *    Service1::class => [
17
 *        '__construct()' => [
18
 *            DynamicReference::to([
19
 *                '__class' => SomeClass::class,
20
 *                'someProp' => 15
21
 *            ]
22
 *        ]
23
 *    ]
24
 * ]
25
 * ```
26
 */
27
class DynamicReference implements Definition
28
{
29
    private $definition;
30
31
    private function __construct($definition)
32
    {
33
        $this->definition = Normalizer::normalize($definition);
34
    }
35
36
    public static function to($definition): DynamicReference
37
    {
38
        return new self($definition);
39
    }
40
41
    public function resolve(ContainerInterface $container, array $params = [])
42
    {
43
        return $this->definition->resolve($container, $params);
44
    }
45
}
46