|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Antidot\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ContainerBuilder |
|
12
|
|
|
* @deprecated This class will be removed in 1.0.0 version |
|
13
|
|
|
*/ |
|
14
|
|
|
class ContainerBuilder |
|
15
|
|
|
{ |
|
16
|
7 |
|
private function __construct() |
|
17
|
|
|
{ |
|
18
|
7 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param array<mixed> $dependencies |
|
22
|
|
|
*/ |
|
23
|
7 |
|
public static function build(array $dependencies, bool $autowire = false): ContainerInterface |
|
24
|
|
|
{ |
|
25
|
7 |
|
$self = new self(); |
|
26
|
|
|
|
|
27
|
7 |
|
return new Container( |
|
28
|
7 |
|
$self->parseConfigFor($dependencies), |
|
29
|
|
|
$autowire |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array<mixed> $dependencies |
|
35
|
|
|
*/ |
|
36
|
7 |
|
private function parseConfigFor(array $dependencies): ContainerConfig |
|
37
|
|
|
{ |
|
38
|
|
|
$containerConfig = [ |
|
39
|
7 |
|
'config' => $dependencies, |
|
40
|
|
|
'parameters' => [], |
|
41
|
7 |
|
'delegators' => $dependencies['dependencies']['delegators'] ?? [], |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
7 |
|
foreach ($dependencies['dependencies']['invokables'] ?? [] as $name => $invokable) { |
|
45
|
2 |
|
$containerConfig[$name] = $invokable; |
|
46
|
|
|
} |
|
47
|
7 |
|
foreach ($dependencies['dependencies']['aliases'] ?? [] as $name => $service) { |
|
48
|
1 |
|
$containerConfig[$name] = $service; |
|
49
|
|
|
} |
|
50
|
7 |
|
foreach ($dependencies['dependencies']['factories'] ?? [] as $name => $factory) { |
|
51
|
3 |
|
$containerConfig[$name] = static function (ContainerInterface $container) use ($factory) { |
|
52
|
3 |
|
if (is_array($factory)) { |
|
53
|
1 |
|
$class = array_shift($factory); |
|
54
|
1 |
|
$instance = new $class(); |
|
55
|
1 |
|
$method = new ReflectionMethod($class, '__invoke'); |
|
56
|
1 |
|
return $method->invokeArgs($instance, array_merge([$container], $factory)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if (is_callable($factory)) { |
|
60
|
1 |
|
return $factory($container); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
if (class_exists($factory)) { |
|
64
|
1 |
|
return (new $factory())($container); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
throw new \InvalidArgumentException('Invalid factory type given.'); |
|
68
|
|
|
}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
7 |
|
foreach ($dependencies['dependencies']['conditionals'] ?? [] as $name => $conditional) { |
|
72
|
1 |
|
$containerConfig['parameters'][$name] = $conditional['arguments']; |
|
73
|
1 |
|
$containerConfig[$name] = $conditional['class']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
7 |
|
return new ContainerConfig($containerConfig); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|