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

DynamicReference::resolve()   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
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