Repository navigation
Expand file tree
/
Copy pathContainer.php
More file actions
191 lines (170 loc) · 4.5 KB
/
Copy pathContainer.php
File metadata and controls
191 lines (170 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Selective\Container;
use LogicException;
use Psr\Container\ContainerInterface;
use Selective\Container\Exceptions\ContainerException;
use Selective\Container\Exceptions\NotFoundException;
use Selective\Container\Resolver\DefinitionResolverInterface;
use Throwable;
/**
* The container with factories.
*/
final class Container implements ContainerInterface, FactoryInterface
{
/**
* @var array<string, callable>
*/
private $factories = [];
/**
* @var array<string, mixed>
*/
private $services = [];
/**
* @var DefinitionResolverInterface[]
*/
private $resolvers = [];
/**
* The constructor.
*
* @param array<string, callable> $factories The factory definitions
*/
public function __construct(array $factories = [])
{
$this->set(__CLASS__, $this);
$this->set(ContainerInterface::class, $this);
$this->factories($factories);
}
/**
* Add factory definitions.
*
* @param array<string, callable> $factories The callables
*
* @return void
*/
public function factories(array $factories): void
{
foreach ($factories as $id => $factory) {
$this->factory($id, $factory);
}
}
/**
* Add factory definition.
*
* @param string $id The container id
* @param callable $factory The callable
*
* @throws LogicException When the given factory already exists
*
* @return void
*/
public function factory(string $id, callable $factory): void
{
if (isset($this->factories[$id])) {
throw new LogicException('The factory cannot be modified');
}
$this->factories[$id] = $factory;
}
/**
* Define an object or a value in the container.
*
* @param string $id The identifier
* @param mixed $value The value
*
* @return void
*/
public function set(string $id, $value): void
{
$this->services[$id] = $value;
}
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id The identifier of the entry to look for
*
* @return mixed The entry
*/
public function get(string $id)
{
return $this->services[$id] ?? $this->services[$id] = $this->create($id);
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id The identifier of the entry to look for
*
* @return bool The status
*/
public function has(string $id): bool
{
return isset($this->services[$id]) || isset($this->factories[$id]) || $this->isResolvable($id);
}
/**
* Create instance.
*
* @param string $id The id
*
* @return mixed The value
*/
private function create(string $id)
{
try {
if (isset($this->factories[$id])) {
return ($this->factories[$id])($this);
}
} catch (Throwable $throwable) {
throw ContainerException::create($id, $throwable);
}
if (class_exists($id, true)) {
return $this->autowire($id);
}
throw NotFoundException::create($id);
}
/**
* Add resolver.
*
* @param DefinitionResolverInterface $resolver The resolver
*
* @return void
*/
public function addResolver(DefinitionResolverInterface $resolver): void
{
$this->resolvers[] = $resolver;
}
/**
* Check if a definition can be resolved.
*
* @param string|class-string $id The full class name
*
* @return bool Status
*/
private function isResolvable(string $id): bool
{
foreach ($this->resolvers as $resolver) {
if ($resolver->isResolvable($id)) {
return true;
}
}
return false;
}
/**
* Autowire dependencies.
*
* @param string $id The id
*
* @return mixed|null The value obtained from the definition
*/
private function autowire(string $id)
{
foreach ($this->resolvers as $resolver) {
$value = $resolver->resolve($id);
if ($value !== null) {
return $value;
}
}
return null;
}
}