1919 */
2020class DeploymentConfig
2121{
22+ private const MAGENTO_ENV_PREFIX = 'MAGENTO_DC_ ' ;
23+ private const ENV_NAME_PATTERN = '~^%env\(\s*(?<name>\w+)\s*(,\s*"(?<default>[^"]+)")?\)%$~ ' ;
24+
2225 /**
2326 * Configuration reader
2427 *
@@ -140,6 +143,21 @@ public function isDbAvailable()
140143 return isset ($ this ->data ['db ' ]);
141144 }
142145
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+
143161 /**
144162 * Loads the configuration data
145163 *
@@ -150,10 +168,11 @@ public function isDbAvailable()
150168 private function load ()
151169 {
152170 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+ );
157176 // flatten data for config retrieval using get()
158177 $ this ->flatData = $ this ->flattenParams ($ this ->data );
159178 }
@@ -187,9 +206,25 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
187206 //phpcs:ignore Magento2.Exceptions.DirectThrow
188207 throw new RuntimeException (new Phrase ("Key collision '%1' is already defined. " , [$ newPath ]));
189208 }
190- $ flattenResult [ $ newPath ] = $ param ;
209+
191210 if (is_array ($ param )) {
211+ $ flattenResult [$ newPath ] = $ param ;
192212 $ 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 ;
193228 }
194229 }
195230
0 commit comments