1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maiorano\ContainerConfig; |
4
|
|
|
|
5
|
|
|
use League\Container\Definition\DefinitionAggregateInterface; |
6
|
|
|
use League\Container\Definition\DefinitionInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DefinitionBuilder. |
10
|
|
|
*/ |
11
|
|
|
final class DefinitionBuilder implements BuilderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DefinitionAggregateInterface |
15
|
|
|
*/ |
16
|
|
|
private DefinitionAggregateInterface $definitions; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DefinitionBuilder constructor. |
20
|
|
|
* |
21
|
|
|
* @param DefinitionAggregateInterface $definitions |
22
|
|
|
*/ |
23
|
6 |
|
public function __construct(DefinitionAggregateInterface $definitions) |
24
|
|
|
{ |
25
|
6 |
|
$this->definitions = $definitions; |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $config |
30
|
|
|
* |
31
|
|
|
* @return DefinitionAggregateInterface |
32
|
|
|
*/ |
33
|
2 |
|
public function build(array $config): DefinitionAggregateInterface |
34
|
|
|
{ |
35
|
2 |
|
foreach ($config as $key => $value) { |
36
|
2 |
|
$this->buildDefinition((string) $key, $value); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
return $this->definitions; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $key |
44
|
|
|
* @param mixed $value |
45
|
|
|
* |
46
|
|
|
* @return DefinitionInterface |
47
|
|
|
*/ |
48
|
5 |
|
public function buildDefinition(string $key, $value): DefinitionInterface |
49
|
|
|
{ |
50
|
5 |
|
if ($value instanceof DefinitionInterface) { |
51
|
2 |
|
$alias = is_numeric($key) ? $value->getAlias() : $key; |
52
|
|
|
|
53
|
2 |
|
return $this->definitions->add($alias, $value, $value->isShared()); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$concrete = $this->resolveConcrete($value); |
57
|
3 |
|
$alias = $this->resolveAlias($key, $value, $concrete); |
58
|
3 |
|
$definition = $this->definitions->add($alias, $concrete, $this->resolveShared($value)); |
59
|
3 |
|
if (is_array($value)) { |
60
|
3 |
|
$this->resolveArguments($definition, $value['arguments'] ?? []); |
61
|
3 |
|
$this->resolveMethods($definition, $value['methods'] ?? []); |
62
|
3 |
|
$this->resolveTags($definition, $value['tags'] ?? []); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
return $definition; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $value |
70
|
|
|
* |
71
|
|
|
* @return array|mixed|string |
72
|
|
|
*/ |
73
|
3 |
|
private function resolveConcrete($value) |
74
|
|
|
{ |
75
|
3 |
|
if (is_array($value) && isset($value['concrete'])) { |
76
|
3 |
|
return $value['concrete']; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* @param mixed $concrete |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
3 |
|
private function resolveAlias(string $key, $value, $concrete): string |
90
|
|
|
{ |
91
|
3 |
|
if (is_array($value) && isset($value['alias'])) { |
92
|
3 |
|
return $value['alias']; |
93
|
|
|
} |
94
|
2 |
|
if (is_numeric($key)) { |
95
|
1 |
|
return $this->resolveAliasFromConcrete($key, $concrete); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $key; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $key |
103
|
|
|
* @param mixed $concrete |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
private function resolveAliasFromConcrete(string $key, $concrete): string |
107
|
|
|
{ |
108
|
1 |
|
if (is_string($concrete) && (class_exists($concrete) || interface_exists($concrete))) { |
109
|
1 |
|
return $concrete; |
110
|
|
|
} |
111
|
1 |
|
return is_object($concrete) ? get_class($concrete) : $key; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $value |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
3 |
|
private function resolveShared($value): bool |
120
|
|
|
{ |
121
|
3 |
|
if (is_array($value) && isset($value['shared'])) { |
122
|
3 |
|
return $value['shared'] === true; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param DefinitionInterface $definition |
130
|
|
|
* @param array $arguments |
131
|
|
|
* |
132
|
|
|
* @return DefinitionInterface |
133
|
|
|
*/ |
134
|
3 |
|
private function resolveArguments(DefinitionInterface $definition, array $arguments): DefinitionInterface |
135
|
|
|
{ |
136
|
3 |
|
return $definition->addArguments($arguments); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param DefinitionInterface $definition |
141
|
|
|
* @param array $methods |
142
|
|
|
* |
143
|
|
|
* @return DefinitionInterface |
144
|
|
|
*/ |
145
|
3 |
|
private function resolveMethods(DefinitionInterface $definition, array $methods): DefinitionInterface |
146
|
|
|
{ |
147
|
3 |
|
return $definition->addMethodCalls($methods); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param DefinitionInterface $definition |
152
|
|
|
* @param array $tags |
153
|
|
|
* |
154
|
|
|
* @return DefinitionInterface |
155
|
|
|
*/ |
156
|
3 |
|
private function resolveTags(DefinitionInterface $definition, array $tags): DefinitionInterface |
157
|
|
|
{ |
158
|
3 |
|
foreach ($tags as $tag) { |
159
|
1 |
|
$definition->addTag($tag); |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
return $definition; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|