Passed
Pull Request — master (#151)
by Sergei
02:12
created

FactoryContainer::create()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory;
6
7
use LogicException;
8
use Psr\Container\ContainerInterface;
9
use Yiisoft\Definitions\ArrayDefinition;
10
use Yiisoft\Definitions\Contract\DefinitionInterface;
11
use Yiisoft\Definitions\Contract\ReferenceInterface;
12
use Yiisoft\Definitions\Exception\CircularReferenceException;
13
use Yiisoft\Definitions\Exception\InvalidConfigException;
14
use Yiisoft\Definitions\Exception\NotInstantiableClassException;
15
use Yiisoft\Definitions\Exception\NotInstantiableException;
16
use Yiisoft\Definitions\Helpers\Normalizer;
17
18
use function array_key_exists;
19
use function is_object;
20
use function is_string;
21
22
/**
23
 * Factory's primary container.
24
 *
25
 * @internal
26
 */
27
final class FactoryContainer implements ContainerInterface
28
{
29
    /**
30
     * @var ContainerInterface Container to use for resolving dependencies.
31
     */
32
    private ContainerInterface $container;
33
34
    /**
35
     * @var array Definitions to create objects with.
36
     * @psalm-var array<string, mixed>
37
     */
38
    private array $definitions = [];
39
40
    /**
41
     * @var DefinitionInterface[] Object created from definitions indexed by their types.
42
     * @psalm-var array<string, DefinitionInterface>
43
     */
44
    private array $definitionInstances = [];
45
46
    /**
47
     * @var array Used to collect IDs instantiated during build to detect circular references.
48
     *
49
     * @psalm-var array<string,1>
50
     */
51
    private array $creatingIds = [];
52
53
    /**
54
     * @param ContainerInterface $container Container to use for resolving dependencies.
55
     */
56 109
    public function __construct(ContainerInterface $container)
57
    {
58 109
        $this->container = $container;
59 109
    }
60
61
    /**
62
     * @inheritDoc
63
     *
64
     * @param string $id
65
     *
66
     * @return mixed|object
67
     * @psalm-suppress InvalidThrow
68
     */
69 51
    public function get($id)
70
    {
71 51
        if ($this->hasDefinition($id)) {
72 33
            return $this->build($id);
73
        }
74
75 21
        if ($this->container->has($id)) {
76 14
            return $this->container->get($id);
77
        }
78
79 7
        throw new NotInstantiableClassException($id);
80
    }
81
82 2
    public function has($id): bool
83
    {
84 2
        return $this->hasDefinition($id) || $this->container->has($id);
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90 93
    public function create(DefinitionInterface $definition)
91
    {
92 93
        if ($definition instanceof ArrayDefinition) {
93 75
            $this->creatingIds[$definition->getClass()] = 1;
94
        }
95
96
        try {
97 93
            return $definition->resolve($this);
98
        } finally {
99 93
            if ($definition instanceof ArrayDefinition) {
100 93
                unset($this->creatingIds[$definition->getClass()]);
101
            }
102
        }
103
    }
104
105
    /**
106
     * Get definition by identifier provided.
107
     *
108
     * @throws InvalidConfigException
109
     */
110 71
    public function getDefinition(string $id): DefinitionInterface
111
    {
112 71
        if (!isset($this->definitionInstances[$id])) {
113 71
            if (isset($this->definitions[$id])) {
114 70
                if (is_object($this->definitions[$id]) && !($this->definitions[$id] instanceof ReferenceInterface)) {
115 13
                    return Normalizer::normalize(clone $this->definitions[$id], $id);
116
                }
117
118
                if (
119 65
                    is_string($this->definitions[$id])
120 65
                    && $this->hasDefinition($this->definitions[$id])
121 65
                    && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]
122
                ) {
123 1
                    return $this->getDefinition($this->definitions[$id]);
124
                }
125
126 65
                $this->definitionInstances[$id] = Normalizer::normalize(
127 65
                    is_string($this->definitions[$id]) && class_exists($this->definitions[$id])
128 30
                        ? ['class' => $this->definitions[$id]]
129 65
                        : $this->definitions[$id],
130
                    $id
131
                );
132
            } else {
133 1
                throw new LogicException(
134 1
                    sprintf('No definition found for "%s".', $id)
135
                );
136
            }
137
        }
138
139 64
        return $this->definitionInstances[$id];
140
    }
141
142
    /**
143
     * Check if there is a definition with a given identifier.
144
     *
145
     * @param string $id Identifier to look for.
146
     *
147
     * @return bool If there is a definition with a given identifier.
148
     */
149 99
    public function hasDefinition(string $id): bool
150
    {
151 99
        return array_key_exists($id, $this->definitions);
152
    }
153
154
    /**
155
     * Set definition for a given identifier.
156
     *
157
     * @param string $id Identifier to set definition for.
158
     * @param mixed $definition Definition to set.
159
     */
160 70
    public function setDefinition(string $id, $definition): void
161
    {
162 70
        $this->definitions[$id] = $definition;
163 70
    }
164
165
    /**
166
     * @param string $id
167
     *
168
     * @throws CircularReferenceException
169
     * @throws InvalidConfigException
170
     * @throws NotFoundException
171
     * @throws NotInstantiableException
172
     *
173
     * @return mixed|object
174
     */
175 33
    private function build(string $id)
176
    {
177 33
        if (isset($this->creatingIds[$id])) {
178 5
            throw new CircularReferenceException(sprintf(
179 5
                'Circular reference to "%s" detected while creating: %s.',
180
                $id,
181 5
                implode(', ', array_keys($this->creatingIds))
182
            ));
183
        }
184
185 32
        $definition = $this->getDefinition($id);
186
187 32
        $this->creatingIds[$id] = 1;
188
        try {
189 32
            return $definition->resolve($this);
190
        } finally {
191 32
            unset($this->creatingIds[$id]);
192
        }
193
    }
194
}
195