-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurator.php
More file actions
324 lines (285 loc) · 9.08 KB
/
Configurator.php
File metadata and controls
324 lines (285 loc) · 9.08 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
declare(strict_types=1);
namespace Lit\Air;
use Lit\Air\Psr\Container;
use Lit\Air\Psr\ContainerException;
use Lit\Air\Recipe\AbstractRecipe;
use Lit\Air\Recipe\AutowireRecipe;
use Lit\Air\Recipe\BuilderRecipe;
use Lit\Air\Recipe\Decorator\AbstractRecipeDecorator;
use Lit\Air\Recipe\Decorator\CallbackDecorator;
use Lit\Air\Recipe\Decorator\SingletonDecorator;
use Lit\Air\Recipe\FixedValueRecipe;
use Lit\Air\Recipe\InstanceRecipe;
use Lit\Air\Recipe\RecipeInterface;
/**
* Configurator helps to build an array configuration, and writes array configuration into a container.
* http://litphp.github.io/docs/air-config
*/
class Configurator
{
protected static $decorators = [
'callback' => CallbackDecorator::class,
'singleton' => SingletonDecorator::class,
];
/**
* Write a configuration array into a container
*
* @param Container $container The container.
* @param array $config The configuration array.
* @param boolean $force Whether overwrite existing values.
* @return void
*/
public static function config(Container $container, array $config, bool $force = true): void
{
foreach ($config as $key => $value) {
if (!$force && $container->has($key)) {
continue;
}
self::write($container, $key, $value);
}
}
/**
* Convert a mixed value into a recipe.
*
* @param mixed $value The value.
* @return RecipeInterface
*/
public static function convertToRecipe($value): RecipeInterface
{
if (is_object($value) && $value instanceof RecipeInterface) {
return $value;
}
if (is_callable($value)) {
return (new BuilderRecipe($value))->singleton();
}
if (is_array($value)) {
$r = self::convertArrayToRecipe($value);
if ($r !== null) {
return $r;
}
trigger_error("array should be wrapped with C::value", E_USER_NOTICE);
}
return AbstractRecipe::value($value);
}
/**
* Configuration indicating a singleton
*
* @param string $classname The class name.
* @param array $extra Extra parameters.
* @return array
*/
public static function singleton(string $classname, array $extra = []): array
{
return self::decorateSingleton(self::instance($classname, $extra));
}
/**
* Decorate a configuration, makes it a singleton (\Lit\Air\Recipe\Decorator\SingletonDecorator)
*
* @param array $config The configuration.
* @return array
*/
public static function decorateSingleton(array $config): array
{
$config['decorator'] = $config['decorator'] ?? [];
$config['decorator']['singleton'] = true;
return $config;
}
/**
* Decorate a configuration with provided callback
*
* @param array $config The configuration.
* @param callable $callback The callback.
* @return array
*/
public static function decorateCallback(array $config, callable $callback): array
{
$config['decorator'] = $config['decorator'] ?? [];
$config['decorator']['callback'] = $callback;
return $config;
}
/**
* Configuration indicating an autowired entry.
*
* @param string $classname The class name.
* @param array $extra Extra parameters.
* @param bool $cached Whether to save the instance if it's not defined in container.
* @return array
*/
public static function produce(string $classname, array $extra = [], bool $cached = true): array
{
return [
'$' => 'autowire',
$classname,
$extra,
$cached,
];
}
/**
* Configuration indicating an instance created by factory.
*
* @param string $classname The class name.
* @param array $extra Extra parameters.
* @return array
*/
public static function instance(string $classname, array $extra = []): array
{
return [
'$' => 'instance',
$classname,
$extra,
];
}
/**
* Configuration indicating an alias
*
* @param string ...$key Multiple keys will be auto joined.
* @return array
*/
public static function alias(string ...$key): array
{
return [
'$' => 'alias',
self::join(...$key),
];
}
/**
* Configuration wrapping a builder method
*
* @param callable $builder The builder method.
* @param array $extra Extra parameters.
* @return array
*/
public static function builder(callable $builder, array $extra = []): array
{
return [
'$' => 'builder',
$builder,
$extra,
];
}
/**
* Configuration wraps an arbitary value. For arrays it's recommended to always wrap with this.
*
* @param mixed $value The value.
* @return array
*/
public static function value($value): array
{
return [
'$' => 'value',
$value,
];
}
protected static function write(Container $container, $key, $value)
{
if (is_scalar($value) || is_resource($value)) {
$container->set($key, $value);
return;
}
if (
substr($key, -2) === '::'
&& class_exists(substr($key, 0, -2))
) {
$container->set($key, self::mapArrayValueToRecipe($value));
return;
}
$recipe = self::convertToRecipe($value);
if ($recipe instanceof FixedValueRecipe) {
$container->set($key, $recipe->getValue());
} else {
$container->flush($key);
$container->define($key, $recipe);
}
}
protected static function mapArrayValueToRecipe(array $arr): array
{
$result = [];
foreach ($arr as $k => $v) {
$result[$k] = self::convertToRecipe($v);
if ($result[$k] instanceof FixedValueRecipe) {
$result[$k] = $result[$k]->getValue();
}
}
return $result;
}
protected static function convertArrayToRecipe(array $arr): ?RecipeInterface
{
if (array_key_exists(0, $arr) && !empty($arr['$'])) {
return self::makeRecipe($arr);
}
if (Utils::isSequentialArray($arr, 1) && is_string($arr[0])) {
return new AutowireRecipe($arr[0], [], false);
}
if (Utils::isSequentialArray($arr, 2) && is_string($arr[0]) && class_exists($arr[0])) {
return new InstanceRecipe($arr[0], $arr[1]);
}
return null;
}
protected static function makeRecipe(array $arr): RecipeInterface
{
$type = $arr['$'];
unset($arr['$']);
if (
array_key_exists($type, [
'alias' => 1,
'autowire' => 1,
'instance' => 1,
'builder' => 1,
'value' => 1,
])
) {
$valueDecorator = $arr['decorator'] ?? null;
unset($arr['decorator']);
$builder = [AbstractRecipe::class, $type];
assert(is_callable($builder));
/**
* @var RecipeInterface $recipe
*/
$recipe = $builder(...$arr);
if ($valueDecorator) {
$recipe = self::wrapRecipeWithDecorators($valueDecorator, $recipe);
}
return $recipe;
}
throw new ContainerException("cannot understand given recipe");
}
/**
* Apply decorators to a recipe and return the decorated recipe
*
* @param array $decorators Assoc array of decorator names => options.
* @param RecipeInterface $recipe The decorated recipe instance.
* @return RecipeInterface
*/
protected static function wrapRecipeWithDecorators(array $decorators, RecipeInterface $recipe): RecipeInterface
{
foreach ($decorators as $name => $option) {
if (isset(self::$decorators[$name])) {
$decorateFn = [self::$decorators[$name], 'decorate'];
assert(is_callable($decorateFn));
$recipe = call_user_func($decorateFn, $recipe);
} elseif (is_subclass_of($name, AbstractRecipeDecorator::class)) {
$decorateFn = [$name, 'decorate'];
assert(is_callable($decorateFn));
$recipe = call_user_func($decorateFn, $recipe);
} else {
throw new ContainerException("cannot understand recipe decorator [$name]");
}
assert($recipe instanceof AbstractRecipeDecorator);
if (!empty($option)) {
$recipe->setOption($option);
}
}
return $recipe;
}
/**
* Join multiple strings with air conventional separator `::`
*
* @param string ...$args Parts of the key to be joined.
* @return string
*/
public static function join(string ...$args): string
{
return implode('::', $args);
}
}