@@ -51,6 +51,16 @@ class DeploymentConfig
5151 */
5252 private $ overrideData ;
5353
54+ /**
55+ * @var array
56+ */
57+ private $ envOverrides = [];
58+
59+ /**
60+ * @var array
61+ */
62+ private $ readerLoad = [];
63+
5464 /**
5565 * Constructor
5666 *
@@ -84,7 +94,9 @@ public function get($key = null, $defaultValue = null)
8494 }
8595 $ result = $ this ->getByKey ($ key );
8696 if ($ result === null ) {
87- $ this ->reloadData ();
97+ if (empty ($ this ->flatData ) || count ($ this ->getAllEnvOverrides ())) {
98+ $ this ->reloadData ();
99+ }
88100 $ result = $ this ->getByKey ($ key );
89101 }
90102 return $ result ?? $ defaultValue ;
@@ -114,13 +126,13 @@ public function getConfigData($key = null)
114126 {
115127 if ($ key === null ) {
116128 if (empty ($ this ->data )) {
117- $ this ->reloadData ();
129+ $ this ->reloadInitialData ();
118130 }
119131 return $ this ->data ;
120132 }
121133 $ result = $ this ->getConfigDataByKey ($ key );
122134 if ($ result === null ) {
123- $ this ->reloadData ();
135+ $ this ->reloadInitialData ();
124136 $ result = $ this ->getConfigDataByKey ($ key );
125137 }
126138 return $ result ;
@@ -170,28 +182,55 @@ private function getEnvOverride() : array
170182 * @throws FileSystemException
171183 * @throws RuntimeException
172184 */
173- private function reloadData (): void
185+ private function reloadInitialData (): void
174186 {
187+ if (empty ($ this ->readerLoad ) || empty ($ this ->data ) || empty ($ this ->flatData )) {
188+ $ this ->readerLoad = $ this ->reader ->load ();
189+ }
175190 $ this ->data = array_replace (
176- $ this ->reader -> load () ,
191+ $ this ->readerLoad ,
177192 $ this ->overrideData ?? [],
178193 $ this ->getEnvOverride ()
179194 );
195+ }
196+
197+ /**
198+ * Loads the configuration data
199+ *
200+ * @return void
201+ * @throws FileSystemException
202+ * @throws RuntimeException
203+ */
204+ private function reloadData (): void
205+ {
206+ $ this ->reloadInitialData ();
180207 // flatten data for config retrieval using get()
181208 $ this ->flatData = $ this ->flattenParams ($ this ->data );
209+ $ this ->flatData = $ this ->getAllEnvOverrides () + $ this ->flatData ;
210+ }
182211
183- // allow reading values from env variables by convention
184- // MAGENTO_DC_{path}, like db/connection/default/host =>
185- // can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
186- foreach (getenv () as $ key => $ value ) {
187- if (false !== \strpos ($ key , self ::MAGENTO_ENV_PREFIX )
188- && $ key !== self ::OVERRIDE_KEY
189- ) {
190- // convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
191- $ flatKey = strtolower (str_replace ([self ::MAGENTO_ENV_PREFIX , '__ ' ], ['' , '/ ' ], $ key ));
192- $ this ->flatData [$ flatKey ] = $ value ;
212+ /**
213+ * Load all getenv() configs once
214+ *
215+ * @return array
216+ */
217+ private function getAllEnvOverrides (): array
218+ {
219+ if (empty ($ this ->envOverrides )) {
220+ // allow reading values from env variables by convention
221+ // MAGENTO_DC_{path}, like db/connection/default/host =>
222+ // can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
223+ foreach (getenv () as $ key => $ value ) {
224+ if (false !== \strpos ($ key , self ::MAGENTO_ENV_PREFIX )
225+ && $ key !== self ::OVERRIDE_KEY
226+ ) {
227+ // convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
228+ $ flatKey = strtolower (str_replace ([self ::MAGENTO_ENV_PREFIX , '__ ' ], ['' , '/ ' ], $ key ));
229+ $ this ->envOverrides [$ flatKey ] = $ value ;
230+ }
193231 }
194232 }
233+ return $ this ->envOverrides ;
195234 }
196235
197236 /**
0 commit comments