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