1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
9
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Definitions\DefinitionStorage; |
11
|
|
|
use Yiisoft\Di\Helpers\DefinitionNormalizer; |
12
|
|
|
|
13
|
|
|
use function array_keys; |
14
|
|
|
use function implode; |
15
|
|
|
|
16
|
|
|
final class Factory |
17
|
|
|
{ |
18
|
|
|
private ContainerInterface $container; |
19
|
|
|
|
20
|
|
|
private DefinitionStorage $definitions; |
21
|
|
|
|
22
|
|
|
private array $building; |
23
|
|
|
|
24
|
|
|
|
25
|
111 |
|
public function __construct(ContainerInterface $container, DefinitionStorage $definitions = null) |
26
|
|
|
{ |
27
|
111 |
|
$this->container = $container; |
28
|
|
|
|
29
|
111 |
|
if($definitions === null) { |
30
|
4 |
|
$this->definitions = new DefinitionStorage(); |
31
|
4 |
|
$this->definitions->setDelegateContainer($container); |
32
|
|
|
} else { |
33
|
111 |
|
$this->definitions = $definitions; |
34
|
|
|
} |
35
|
111 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates new instance by either interface name or alias. |
39
|
|
|
* |
40
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
41
|
|
|
* |
42
|
|
|
* @throws CircularReferenceException |
43
|
|
|
* @throws InvalidConfigException |
44
|
|
|
* @throws NotFoundException |
45
|
|
|
* |
46
|
|
|
* @return mixed|object New built instance of the specified class. |
47
|
|
|
* |
48
|
|
|
* @internal |
49
|
|
|
*/ |
50
|
97 |
|
public function create(string $id) |
51
|
|
|
{ |
52
|
97 |
|
if (isset($this->building[$id])) { |
53
|
88 |
|
if ($id === ContainerInterface::class) { |
54
|
88 |
|
return $this->container; |
55
|
|
|
} |
56
|
4 |
|
throw new CircularReferenceException(sprintf( |
57
|
4 |
|
'Circular reference to "%s" detected while building: %s.', |
58
|
|
|
$id, |
59
|
4 |
|
implode(', ', array_keys($this->building)) |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
97 |
|
$this->building[$id] = 1; |
64
|
|
|
try { |
65
|
|
|
/** @var mixed $object */ |
66
|
97 |
|
$object = $this->buildInternal($id); |
67
|
88 |
|
} finally { |
68
|
97 |
|
unset($this->building[$id]); |
69
|
|
|
} |
70
|
|
|
|
71
|
88 |
|
return $object; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function withDefinitions(array $definitions) |
75
|
|
|
{ |
76
|
3 |
|
$new = clone $this; |
77
|
3 |
|
$new->definitions = new DefinitionStorage($definitions); |
78
|
3 |
|
$new->definitions->setDelegateContainer($this->container); |
79
|
|
|
|
80
|
3 |
|
return $new; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $id |
85
|
|
|
* |
86
|
|
|
* @throws InvalidConfigException |
87
|
|
|
* @throws NotFoundException |
88
|
|
|
* |
89
|
|
|
* @return mixed|object |
90
|
|
|
*/ |
91
|
97 |
|
private function buildInternal(string $id) |
92
|
|
|
{ |
93
|
97 |
|
if ($this->definitions->has($id)) { |
94
|
88 |
|
$definition = DefinitionNormalizer::normalize($this->definitions->get($id), $id); |
95
|
|
|
|
96
|
88 |
|
return $definition->resolve($this->container->get(ContainerInterface::class)); |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
throw new NotFoundException($id, $this->definitions->getBuildStack()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|