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

DynamicReference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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