1 | <?php declare(strict_types=1); |
||
14 | class Config |
||
15 | { |
||
16 | /** |
||
17 | * @var EnvironmentResolver |
||
18 | */ |
||
19 | private $resolver; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $header; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $defaultEnvironment; |
||
30 | |||
31 | /** |
||
32 | * @var ConfigEnvironment[] |
||
33 | */ |
||
34 | private $environments; |
||
35 | |||
36 | /** |
||
37 | * @var RuntimeParameters |
||
38 | */ |
||
39 | private $runtimeParameters; |
||
40 | |||
41 | /** |
||
42 | * @param ConfigEnvironment[] $environments |
||
43 | */ |
||
44 | public function __construct( |
||
45 | EnvironmentResolver $resolver, |
||
46 | string $defaultEnvironment, |
||
47 | array $environments, |
||
48 | RuntimeParameters $runtimeParameters, |
||
49 | ?string $header = null |
||
50 | ) { |
||
51 | $this->resolver = $resolver; |
||
52 | $this->defaultEnvironment = $defaultEnvironment; |
||
53 | $this->environments = $environments; |
||
54 | $this->runtimeParameters = $runtimeParameters; |
||
55 | $this->header = $header; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return ScriptsPath[] |
||
60 | */ |
||
61 | public function getAllScriptsPaths(): array |
||
62 | { |
||
63 | $paths = []; |
||
64 | |||
65 | foreach ($this->environments as $name => $environmentConfig) { |
||
66 | foreach ($environmentConfig->getAllScriptsPaths() as $path) { |
||
67 | $paths[] = $path; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return $paths; |
||
72 | } |
||
73 | |||
74 | public function getTemplates(?string $environment = null): array |
||
75 | { |
||
76 | return $this->createResult( |
||
77 | [$this->getEnvironment(), 'getTemplates'], |
||
78 | [$this->getEnvironment($environment), 'getTemplates'] |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | public function getDynamicVariables(?string $environment = null): array |
||
83 | { |
||
84 | return $this->resolver->resolveVariables($this->createUpperCaseResult( |
||
85 | [$this->getEnvironment(), 'getDynamicVariables'], |
||
86 | [$this->getEnvironment($environment), 'getDynamicVariables'] |
||
87 | )); |
||
88 | } |
||
89 | |||
90 | public function getConstants(?string $environment = null): array |
||
91 | { |
||
92 | return $this->resolver->resolveConstants($this->createUpperCaseResult( |
||
93 | [$this->getEnvironment(), 'getConstants'], |
||
94 | [$this->getEnvironment($environment), 'getConstants'], |
||
95 | [$this, 'getParams'] |
||
96 | )); |
||
97 | } |
||
98 | |||
99 | public function getAllPlaceholders(?string $environment = null): array |
||
100 | { |
||
101 | return array_merge( |
||
102 | $this->getConstants($environment), |
||
103 | $this->getDotenvVariables($environment), |
||
104 | $this->getDynamicVariables($environment) |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return ValueProvider[] |
||
110 | */ |
||
111 | public function getDotenvVariables(?string $environment = null): array |
||
112 | { |
||
113 | $paths = $this->getDotenvPaths($environment); |
||
114 | |||
115 | return $this->resolver->resolveDotenvVariables($paths); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return RequiredValue[] |
||
120 | */ |
||
121 | public function getRequiredVariables(?string $environment = null): array |
||
122 | { |
||
123 | $requiredValues = $this->createUpperCaseResult( |
||
124 | [$this->getEnvironment(), 'getRequiredVariables'], |
||
125 | [$this->getEnvironment($environment), 'getRequiredVariables'] |
||
126 | ); |
||
127 | |||
128 | $result = []; |
||
129 | foreach ($requiredValues as $name => $description) { |
||
130 | $result[$name] = new RequiredValue($name, $description); |
||
131 | } |
||
132 | |||
133 | return $result; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return DotenvFile[] |
||
138 | */ |
||
139 | public function getDotenvPaths(?string $environment = null): array |
||
140 | { |
||
141 | $paths = $this->createResult( |
||
142 | [$this->getEnvironment(), 'getDotenvPaths'], |
||
143 | [$this->getEnvironment($environment), 'getDotenvPaths'] |
||
144 | ); |
||
145 | |||
146 | return array_map(static function (string $path): DotenvFile { |
||
147 | return new DotenvFile($path); |
||
148 | }, $paths); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getHeader(): ?string |
||
155 | { |
||
156 | return $this->header; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return ConfigEnvironment[] |
||
161 | */ |
||
162 | public function getEnvironments(): array |
||
163 | { |
||
164 | return $this->environments; |
||
165 | } |
||
166 | |||
167 | public function getDefaultEnvironment(): string |
||
168 | { |
||
169 | return $this->defaultEnvironment; |
||
170 | } |
||
171 | |||
172 | public function hasOption(string $name): bool |
||
176 | |||
177 | public function getImports(): array |
||
181 | |||
182 | public function getScriptNames(): array |
||
186 | |||
187 | private function getParams(): array |
||
191 | |||
192 | private function createResult(callable ...$valueProviders): array |
||
204 | |||
205 | private function createUpperCaseResult(callable ...$valueProviders): array |
||
217 | |||
218 | private function getEnvironment(?string $name = null): ConfigEnvironment |
||
226 | } |
||
227 |