Completed
Push — sam-rework ( 1696e6 )
by Andrii
38:13 queued 23:11
created

DynamicReference   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

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

3 Methods

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