-
Notifications
You must be signed in to change notification settings - Fork 3
/
DelegatingContainer.php
199 lines (169 loc) · 5.28 KB
/
DelegatingContainer.php
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
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Dhii\Container\Exception\ContainerException;
use Dhii\Container\Exception\NotFoundException;
use Dhii\Container\Util\StringTranslatingTrait;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use UnexpectedValueException;
class DelegatingContainer implements ContainerInterface
{
use StringTranslatingTrait;
/**
* @var ServiceProviderInterface
*/
protected $provider;
/**
* @var PsrContainerInterface|null
*/
protected $parent;
/**
* Keys represent the list of service names accessed recursively, in order of access
* @var array<string, true>
*/
protected $stack = [];
/**
*/
public function __construct(ServiceProviderInterface $provider, PsrContainerInterface $parent = null)
{
$this->provider = $provider;
$this->parent = $parent;
}
/**
* {@inheritDoc}
*/
public function get(string $id)
{
if (array_key_exists($id, $this->stack)) {
$trace = implode(' -> ', array_keys($this->stack)) . ' -> ' . $id;
throw new ContainerException(
$this->__("Circular dependency detected:\n%s", [$trace]),
0,
null
);
}
$this->stack[$id] = true;
try {
return $this->createService($id);
} finally {
unset($this->stack[$id]);
}
}
/**
* {@inheritDoc}
*/
public function has(string $id): bool
{
$services = $this->provider->getFactories();
return array_key_exists($id, $services);
}
/**
* Creates a service, using the factory that corresponds to a specific key.
*
* @since [*next-version*]
*
* @param string $key The key of the service to be created.
*
* @return mixed The created service.
*
* @throws NotFoundException If no factory corresponds to the given $key.
* @throws ContainerException If an error occurred while creating the service.
*/
protected function createService(string $key)
{
$provider = $this->provider;
$services = $provider->getFactories();
if (!array_key_exists($key, $services)) {
throw new NotFoundException(
$this->__('Service not found for key "%1$s"', [$key]),
0,
null
);
}
$service = $services[$key];
try {
$service = $this->invokeFactory($service);
} catch (UnexpectedValueException $e) {
throw new ContainerException(
$this->__('Could not create service "%1$s"', [$key]),
0,
$e
);
}
$extensions = $provider->getExtensions();
if (!array_key_exists($key, $extensions)) {
return $service;
}
$extension = $extensions[$key];
try {
$service = $this->invokeExtension($extension, $service);
} catch (UnexpectedValueException $e) {
throw new ContainerException(
$this->__('Could not extend service "%1$s"', [$key]),
0,
$e
);
}
return $service;
}
/**
* Retrieves a service by invoking its factory.
*
* @param callable $factory The factory to invoke.
*
* @return mixed The service created by the factory.
*
* @throws UnexpectedValueException If factory could not be invoked.
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
protected function invokeFactory(callable $factory)
{
if (!is_callable($factory)) {
throw new UnexpectedValueException(
$this->__('Factory could not be invoked'),
0,
null
);
}
$baseContainer = $this->getBaseContainer();
$service = $factory($baseContainer);
return $service;
}
/**
* Extends the service by invoking the extension with it.
*
* @param callable $extension The extension to invoke.
* @param mixed $service The service to extend.
*
* @return mixed The extended service.
*
* @throws UnexpectedValueException If extension cannot be invoked.
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
protected function invokeExtension(callable $extension, $service)
{
if (!is_callable($extension)) {
throw new UnexpectedValueException(
$this->__('Factory could not be invoked'),
0,
null
);
}
$baseContainer = $this->getBaseContainer();
$service = $extension($baseContainer, $service);
return $service;
}
/**
* Retrieves the container to be used for definitions and extensions.
*
* @return PsrContainerInterface The parent container, if set. Otherwise, this instance.
*/
protected function getBaseContainer(): PsrContainerInterface
{
return $this->parent instanceof PsrContainerInterface
? $this->parent
: $this;
}
}