Passed
Push — master ( d48de1...7a1d98 )
by Alexander
07:05 queued 04:34
created

DependencyResolver::getInjector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use Yiisoft\Factory\Definition\ArrayDefinition;
11
use Yiisoft\Factory\Definition\DefinitionInterface;
12
use Yiisoft\Factory\Definition\Normalizer;
13
use Yiisoft\Factory\Exception\InvalidConfigException;
14
use Yiisoft\Factory\Exception\NotFoundException;
15
use Yiisoft\Factory\Exception\NotInstantiableException;
16
use Yiisoft\Injector\Injector;
17
18
use function is_object;
19
use function is_string;
20
21
/**
22
 * @internal
23
 */
24
final class DependencyResolver implements DependencyResolverInterface
25
{
26
    private ?ContainerInterface $container;
27
    private ?Injector $injector = null;
28
29
    /**
30
     * @var mixed[] Definitions
31
     * @psalm-var array<string, mixed>
32
     */
33
    private array $definitions = [];
34
35
    /**
36
     * @var DefinitionInterface[] object definitions indexed by their types
37
     * @psalm-var array<string, DefinitionInterface>
38
     */
39
    private array $definitionInstances = [];
40
41 104
    public function __construct(?ContainerInterface $container)
42
    {
43 104
        $this->container = $container;
44 104
    }
45
46
    /**
47
     * @param string $id
48
     *
49
     * @throws NotFoundExceptionInterface
50
     * @throws ContainerExceptionInterface
51
     *
52
     * @return mixed|object
53
     *
54
     * @psalm-suppress InvalidThrow
55
     */
56 24
    public function get($id)
57
    {
58 24
        if ($this->container !== null) {
59 8
            return $this->container->get($id);
60
        }
61 16
        return $this->getFromFactory($id);
62
    }
63
64
    /**
65
     * @param string $id
66
     */
67 2
    public function has($id): bool
68
    {
69 2
        if ($this->container !== null) {
70 1
            return $this->container->has($id);
71
        }
72 1
        return $this->canBeCreatedByFactory($id);
73
    }
74
75 26
    public function resolve(string $id)
76
    {
77 26
        return $this->getFromFactory($id);
78
    }
79
80 14
    public function invoke(callable $callable)
81
    {
82 14
        return $this->getInjector()->invoke($callable);
83
    }
84
85 66
    public function shouldCloneOnResolve(): bool
86
    {
87 66
        return $this->container === null;
88
    }
89
90
    /**
91
     * @param mixed $definition
92
     */
93 75
    public function setFactoryDefinition(string $id, $definition): void
94
    {
95 75
        $this->definitions[$id] = $definition;
96 75
    }
97
98
    /**
99
     * @param mixed $config
100
     *
101
     * @throws InvalidConfigException
102
     */
103 67
    public function createDefinition($config): DefinitionInterface
104
    {
105 67
        if (is_string($config) && isset($this->definitions[$config])) {
106 38
            return Normalizer::normalize(
107 38
                is_object($this->definitions[$config])
108 9
                    ? clone $this->definitions[$config]
109 38
                    : $this->definitions[$config],
110
                $config
111
            );
112
        }
113
114 29
        $definition = Normalizer::normalize($config);
115
116
        if (
117 28
            ($definition instanceof ArrayDefinition) &&
118 28
            isset($this->definitions[$definition->getClass()])
119
        ) {
120 4
            $definition = $this->mergeDefinitions(
121 4
                $this->getDefinition($definition->getClass()),
122
                $definition
123
            );
124
        }
125
126 28
        return $definition;
127
    }
128
129
    /**
130
     * @param string $id
131
     *
132
     * @throws InvalidConfigException
133
     * @throws NotFoundException
134
     * @throws NotInstantiableException
135
     *
136
     * @return mixed|object
137
     */
138 32
    private function getFromFactory($id)
139
    {
140 32
        return $this->getDefinition($id)->resolve($this);
141
    }
142
143
    /**
144
     * @throws InvalidConfigException
145
     */
146 36
    private function getDefinition(string $id): DefinitionInterface
147
    {
148 36
        if (!isset($this->definitionInstances[$id])) {
149 36
            if (isset($this->definitions[$id])) {
150 24
                $this->definitionInstances[$id] = Normalizer::normalize($this->definitions[$id], $id);
151
            } else {
152
                /** @psalm-var class-string $id */
153 29
                $this->definitionInstances[$id] = ArrayDefinition::fromPreparedData($id);
154
            }
155
        }
156
157 36
        return $this->definitionInstances[$id];
158
    }
159
160 14
    private function getInjector(): Injector
161
    {
162 14
        return $this->injector ??= new Injector($this);
163
    }
164
165 1
    private function canBeCreatedByFactory(string $id): bool
166
    {
167 1
        return isset($this->definitions[$id]) || class_exists($id);
168
    }
169
170 4
    private function mergeDefinitions(DefinitionInterface $one, ArrayDefinition $two): DefinitionInterface
171
    {
172 4
        return $one instanceof ArrayDefinition ? $one->merge($two) : $two;
173
    }
174
}
175