|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config\GacelaFileConfig; |
|
6
|
|
|
|
|
7
|
|
|
final class GacelaConfigFile |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array<string,GacelaConfigItem> */ |
|
10
|
|
|
private array $configs; |
|
11
|
|
|
|
|
12
|
|
|
/** @var array<string,string|callable> */ |
|
13
|
|
|
private array $mappingInterfaces; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array<string,GacelaConfigItem> $configs |
|
17
|
|
|
* @param array<string,string|callable> $mappingInterfaces |
|
18
|
|
|
*/ |
|
19
|
21 |
|
private function __construct( |
|
20
|
|
|
array $configs, |
|
21
|
|
|
array $mappingInterfaces |
|
22
|
|
|
) { |
|
23
|
21 |
|
$this->configs = $configs; |
|
24
|
21 |
|
$this->mappingInterfaces = $mappingInterfaces; |
|
25
|
21 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array{ |
|
29
|
|
|
* config: array<array>|array{type:string,path:string,path_local:string}, |
|
30
|
|
|
* mapping-interfaces: array<string,string|callable>, |
|
31
|
|
|
* } $array |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public static function fromArray(array $array): self |
|
34
|
|
|
{ |
|
35
|
6 |
|
return new self( |
|
36
|
6 |
|
self::getConfigItems($array['config'] ?? []), |
|
37
|
6 |
|
$array['mapping-interfaces'] ?? [] |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array<array>|array{type:string,path:string,path_local:string} $config |
|
43
|
|
|
* |
|
44
|
|
|
* @return array<string,GacelaConfigItem> |
|
45
|
|
|
*/ |
|
46
|
6 |
|
private static function getConfigItems(array $config): array |
|
47
|
|
|
{ |
|
48
|
6 |
|
if (self::isSingleConfigFile($config)) { |
|
49
|
2 |
|
$c = GacelaConfigItem::fromArray($config); |
|
50
|
2 |
|
return [$c->type() => $c]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
$result = []; |
|
54
|
|
|
|
|
55
|
|
|
/** @var array<array{type:string,path:string,path_local:string}> $config */ |
|
56
|
4 |
|
foreach ($config as $configItem) { |
|
57
|
3 |
|
$c = GacelaConfigItem::fromArray($configItem); |
|
58
|
3 |
|
$result[$c->type()] = $c; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
6 |
|
private static function isSingleConfigFile(array $config): bool |
|
65
|
|
|
{ |
|
66
|
6 |
|
return isset($config['type']) |
|
67
|
4 |
|
|| isset($config['path']) |
|
68
|
6 |
|
|| isset($config['path_local']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
15 |
|
public static function withDefaults(): self |
|
72
|
|
|
{ |
|
73
|
15 |
|
$configItem = GacelaConfigItem::withDefaults(); |
|
74
|
|
|
|
|
75
|
15 |
|
return new self( |
|
76
|
15 |
|
[$configItem->type() => $configItem], |
|
77
|
15 |
|
[] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array<string,GacelaConfigItem> |
|
83
|
|
|
*/ |
|
84
|
21 |
|
public function getConfigs(): array |
|
85
|
|
|
{ |
|
86
|
21 |
|
return $this->configs; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Map interfaces to concrete classes or callable (which will be resolved on runtime). |
|
91
|
|
|
* This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array<string,string|callable> |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getMappingInterfaces(): array |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->mappingInterfaces; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|