This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.php
245 lines (214 loc) · 8.6 KB
/
Config.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @see https://github.com/zendframework/zend-auradi-config for the canonical source repository
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-auradi-config/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Zend\AuraDi\Config;
use ArrayObject;
use Aura\Di\Container;
use Aura\Di\ContainerConfigInterface;
use Aura\Di\Exception\ServiceNotFound;
use Psr\Container\ContainerInterface;
use function array_search;
use function class_exists;
use function is_array;
use function is_callable;
use function is_string;
use function sprintf;
use function var_export;
/**
* Configuration for the Aura.Di container.
*
* This class provides functionality for the following service types:
*
* - Aliases
* - Delegators
* - Factories
* - Invokable classes
* - Services (known instances)
*/
class Config implements ContainerConfigInterface
{
/**
* @var array
*/
private $config;
public function __construct(array $config)
{
$this->config = $config;
}
/**
* Configure the container
*
* - Adds the 'config' service.
* - If delegators are defined, maps the service to lazyGetCall an
* ExpressiveAuraDelegatorFactory::build invocation using the configured
* delegator and whatever factory was responsible for it.
* - If factories are defined, maps each factory class as a lazily
* instantiable service, and the service to lazyGetCall the factory to
* create the instance.
* - If invokables are defined, maps each to lazyNew the target.
* - If aliases are defined, maps each to lazyGet the target.
*/
public function define(Container $container)
{
// Convert config to an object and inject it
$container->set('config', new ArrayObject($this->config, ArrayObject::ARRAY_AS_PROPS));
if (empty($this->config['dependencies'])
|| ! is_array($this->config['dependencies'])
) {
return;
}
$dependencies = $this->config['dependencies'];
// Inject delegator factories
// This is done early because Aura.Di does not allow modification of a
// service after creation. As such, we need to create custom factories
// for each service with delegators.
if (isset($dependencies['delegators'])
&& is_array($dependencies['delegators'])
) {
$dependencies = $this->marshalDelegators($container, $dependencies);
}
// Inject services
if (isset($dependencies['services'])
&& is_array($dependencies['services'])
) {
foreach ($dependencies['services'] as $name => $service) {
$container->set($name, $service);
}
}
// Inject factories
if (isset($dependencies['factories'])
&& is_array($dependencies['factories'])
) {
foreach ($dependencies['factories'] as $service => $factory) {
if (is_callable($factory)) {
$container->set($service, $container->lazy($factory, $container, $service));
continue;
}
$container->set(
$service,
$container->lazy(function (ContainerInterface $container, string $service) use ($factory) {
if (! is_string($factory) || ! class_exists($factory)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot be initialized by factory %s',
$service,
is_string($factory) ? $factory : var_export($factory, true)
));
}
$instance = new $factory();
if (! is_callable($instance)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot be initalized by non invokable factory %s',
$service,
$factory
));
}
return $instance($container, $service);
}, $container, $service)
);
}
}
// Inject invokables
if (isset($dependencies['invokables'])
&& is_array($dependencies['invokables'])
) {
foreach ($dependencies['invokables'] as $service => $class) {
if ($service !== $class) {
$container->set($service, $container->lazyGet($class));
}
$container->set($class, $container->lazy(function () use ($class) {
if (! is_string($class) || ! class_exists($class)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot be created',
is_string($class) ? $class : var_export($class, true)
));
}
return new $class();
}));
}
}
// Inject aliases
if (isset($dependencies['aliases'])
&& is_array($dependencies['aliases'])
) {
foreach ($dependencies['aliases'] as $alias => $target) {
$container->set($alias, $container->lazyGet($target));
}
}
}
/**
* This method is purposely a no-op.
*/
public function modify(Container $container)
{
}
/**
* Marshal all services with delegators.
*
* @return array List of dependencies minus any services, factories, or
* invokables that match services using delegator factories.
*/
private function marshalDelegators(Container $container, array $dependencies) : array
{
foreach ($dependencies['delegators'] as $service => $delegatorNames) {
$factory = null;
if (isset($dependencies['factories'][$service])) {
// Marshal from factory
$serviceFactory = $dependencies['factories'][$service];
$factory = function () use ($service, $serviceFactory, $container) {
if (is_callable($serviceFactory)) {
$factory = $serviceFactory;
} elseif (is_string($serviceFactory) && ! class_exists($serviceFactory)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot by initialized by factory %s; factory class does not exist',
$service,
$serviceFactory
));
} else {
$factory = new $serviceFactory();
}
if (! is_callable($factory)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot by initalized by factory %s; factory is not callable',
$service,
$serviceFactory
));
}
return $factory($container, $service);
};
unset($dependencies['factories'][$service]);
}
if (isset($dependencies['invokables'])) {
while (false !== ($key = array_search($service, $dependencies['invokables'], true))) {
// Marshal from invokable
$class = $dependencies['invokables'][$key];
$factory = function () use ($class) {
if (! is_string($class) || ! class_exists($class)) {
throw new ServiceNotFound(sprintf(
'Service %s cannot be created',
is_string($class) ? $class : var_export($class, true)
));
}
return new $class();
};
unset($dependencies['invokables'][$key]);
if ($key !== $service) {
$dependencies['aliases'][$key] = $service;
}
}
}
if (! $factory) {
continue;
}
$delegatorFactory = new DelegatorFactory($delegatorNames, $factory);
$container->set(
$service,
$container->lazy([$delegatorFactory, 'build'], $container, $service)
);
}
return $dependencies;
}
}