|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace PFlorek\AwsParameterStore; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Aws\Ssm\SsmClient; |
|
8
|
|
|
use function PFlorek\Elevator\array_elevate; |
|
9
|
|
|
|
|
10
|
|
|
class ConfigProvider |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Options |
|
14
|
|
|
*/ |
|
15
|
|
|
private $options; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Reader |
|
19
|
|
|
*/ |
|
20
|
|
|
private $reader; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array|SsmClient|Reader $client |
|
24
|
|
|
* @param Options|string[] $options |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($client, $options) |
|
27
|
|
|
{ |
|
28
|
|
|
if (is_array($options)) { |
|
29
|
|
|
$options = Options::create($options); |
|
30
|
|
|
} |
|
31
|
|
|
$this->options = $options; |
|
32
|
|
|
|
|
33
|
|
|
if(!$client instanceof Reader) { |
|
34
|
|
|
$client = new Reader($client); |
|
35
|
|
|
} |
|
36
|
|
|
$this->reader = $client; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string|string[] $profile |
|
41
|
|
|
* @return string[]|int[]|float[]|bool[] |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __invoke($profile = []): array |
|
44
|
|
|
{ |
|
45
|
|
|
if (!is_array($profile)) { |
|
46
|
|
|
$profile = (array)$profile; |
|
47
|
|
|
} |
|
48
|
|
|
$paths = $this->createPaths($profile); |
|
|
|
|
|
|
49
|
|
|
$paths = array_reverse($paths); |
|
50
|
|
|
|
|
51
|
|
|
$config = []; |
|
52
|
|
|
foreach ($paths as $context) { |
|
53
|
|
|
$config += $this->reader->fromPath($context); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$config = array_map([SimpleCast::class, 'cast'], $config); |
|
57
|
|
|
|
|
58
|
|
|
return array_elevate($config); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string[] $profiles |
|
63
|
|
|
* @return string[] |
|
64
|
|
|
*/ |
|
65
|
|
|
private function createPaths(array $profiles): array |
|
66
|
|
|
{ |
|
67
|
|
|
$prefix = $this->options->getPrefix(); |
|
68
|
|
|
$prefix = trim($prefix, '/'); |
|
69
|
|
|
|
|
70
|
|
|
$sharedContext = $this->options->getSharedContext(); |
|
71
|
|
|
$sharedContext = trim($sharedContext, '/'); |
|
72
|
|
|
|
|
73
|
|
|
$name = $this->options->getApplicationName(); |
|
74
|
|
|
$name = trim($name, '/'); |
|
75
|
|
|
|
|
76
|
|
|
$separator = $this->options->getProfileSeparator(); |
|
77
|
|
|
|
|
78
|
|
|
$paths = []; |
|
79
|
|
|
if ($sharedContext) { |
|
80
|
|
|
$paths[] = "/{$prefix}/{$sharedContext}"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$paths[] = "/{$prefix}/{$name}"; // base context |
|
84
|
|
|
|
|
85
|
|
|
foreach ($profiles as $profile) { |
|
86
|
|
|
$profile = trim($profile, '/'); |
|
87
|
|
|
$paths[] = "/{$prefix}/{$name}{$separator}{$profile}"; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $paths; |
|
91
|
|
|
} |
|
92
|
|
|
} |