19
19
*/
20
20
class DeploymentConfig
21
21
{
22
+ private const MAGENTO_ENV_PREFIX = 'MAGENTO_DC_ ' ;
23
+ private const ENV_NAME_PATTERN = '~^%env\(\s*(?<name>\w+)\s*(,\s*"(?<default>[^"]+)")?\)%$~ ' ;
24
+
22
25
/**
23
26
* Configuration reader
24
27
*
@@ -140,6 +143,21 @@ public function isDbAvailable()
140
143
return isset ($ this ->data ['db ' ]);
141
144
}
142
145
146
+ /**
147
+ * Get additional configuration from env variable MAGENTO_DC__OVERRIDE
148
+ * Data should be JSON encoded
149
+ *
150
+ * @return array
151
+ */
152
+ private function getEnvOverride () : array
153
+ {
154
+ $ env = getenv (self ::MAGENTO_ENV_PREFIX . '_OVERRIDE ' );
155
+ if (!empty ($ env )) {
156
+ return json_decode ($ env , true ) ?? [];
157
+ }
158
+ return [];
159
+ }
160
+
143
161
/**
144
162
* Loads the configuration data
145
163
*
@@ -150,10 +168,11 @@ public function isDbAvailable()
150
168
private function load ()
151
169
{
152
170
if (empty ($ this ->data )) {
153
- $ this ->data = $ this ->reader ->load ();
154
- if ($ this ->overrideData ) {
155
- $ this ->data = array_replace ($ this ->data , $ this ->overrideData );
156
- }
171
+ $ this ->data = array_replace (
172
+ $ this ->reader ->load (),
173
+ $ this ->overrideData ?? [],
174
+ $ this ->getEnvOverride ()
175
+ );
157
176
// flatten data for config retrieval using get()
158
177
$ this ->flatData = $ this ->flattenParams ($ this ->data );
159
178
}
@@ -187,9 +206,25 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
187
206
//phpcs:ignore Magento2.Exceptions.DirectThrow
188
207
throw new RuntimeException (new Phrase ("Key collision '%1' is already defined. " , [$ newPath ]));
189
208
}
190
- $ flattenResult [ $ newPath ] = $ param ;
209
+
191
210
if (is_array ($ param )) {
211
+ $ flattenResult [$ newPath ] = $ param ;
192
212
$ this ->flattenParams ($ param , $ newPath , $ flattenResult );
213
+ } else {
214
+ // allow reading values from env variables
215
+ // value need to be specified in %env(NAME, "default value")% format
216
+ // like %env(DB_PASSWORD)%, %env(DB_NAME, "test")%
217
+ if (preg_match (self ::ENV_NAME_PATTERN , $ param ,$ matches )) {
218
+ $ param = getenv ($ matches ['name ' ]) ?: ($ matches ['default ' ] ?? null );
219
+ }
220
+
221
+ // allow reading values from env variables by convention
222
+ // MAGENTO_DC_{path}, like db/connection/default/host =>
223
+ // can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
224
+ $ envName = self ::MAGENTO_ENV_PREFIX . strtoupper (str_replace (['/ ' ], ['__ ' ], $ newPath ));
225
+ $ param = getenv ($ envName ) ?: $ param ;
226
+
227
+ $ flattenResult [$ newPath ] = $ param ;
193
228
}
194
229
}
195
230
0 commit comments