Passed
Push — master ( b8611a...a5444c )
by Alexander
02:14
created

DependencyResolver::shouldCloneOnResolve()   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 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
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 103
    public function __construct(?ContainerInterface $container)
42
    {
43 103
        $this->container = $container;
44 103
    }
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 resolveReference(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
    /**
86
     * @param mixed $definition
87
     */
88 76
    public function setFactoryDefinition(string $id, $definition): void
89
    {
90 76
        $this->definitions[$id] = $definition;
91 76
    }
92
93
    /**
94
     * @param mixed $config
95
     *
96
     * @throws InvalidConfigException
97
     */
98 68
    public function createDefinition($config): DefinitionInterface
99
    {
100 68
        if (is_string($config) && isset($this->definitions[$config])) {
101 39
            return Normalizer::normalize(
102 39
                is_object($this->definitions[$config])
103 9
                    ? clone $this->definitions[$config]
104 39
                    : $this->definitions[$config],
105
                $config
106
            );
107
        }
108
109 29
        $definition = Normalizer::normalize($config);
110
111
        if (
112 28
            ($definition instanceof ArrayDefinition) &&
113 28
            isset($this->definitions[$definition->getClass()])
114
        ) {
115 4
            $definition = $this->mergeDefinitions(
116 4
                $this->getDefinition($definition->getClass()),
117
                $definition
118
            );
119
        }
120
121 28
        return $definition;
122
    }
123
124
    /**
125
     * @param string $id
126
     *
127
     * @throws InvalidConfigException
128
     * @throws NotFoundException
129
     * @throws NotInstantiableException
130
     *
131
     * @return mixed|object
132
     */
133 32
    private function getFromFactory($id)
134
    {
135 32
        return $this->getDefinition($id)->resolve($this);
136
    }
137
138
    /**
139
     * @throws InvalidConfigException
140
     */
141 36
    private function getDefinition(string $id): DefinitionInterface
142
    {
143 36
        if (!isset($this->definitionInstances[$id])) {
144 36
            if (isset($this->definitions[$id])) {
145 24
                $this->definitionInstances[$id] = Normalizer::normalize($this->definitions[$id], $id);
146
            } else {
147
                /** @psalm-var class-string $id */
148 29
                $this->definitionInstances[$id] = ArrayDefinition::fromPreparedData($id);
149
            }
150
        }
151
152 36
        return $this->definitionInstances[$id];
153
    }
154
155 14
    private function getInjector(): Injector
156
    {
157 14
        return $this->injector ??= new Injector($this);
158
    }
159
160 1
    private function canBeCreatedByFactory(string $id): bool
161
    {
162 1
        return isset($this->definitions[$id]) || class_exists($id);
163
    }
164
165 4
    private function mergeDefinitions(DefinitionInterface $one, ArrayDefinition $two): DefinitionInterface
166
    {
167 4
        return $one instanceof ArrayDefinition ? $one->merge($two) : $two;
168
    }
169
}
170