@@ -35,14 +35,14 @@ class DeploymentConfig
35
35
*
36
36
* @var array
37
37
*/
38
- private $ data ;
38
+ private $ data = [] ;
39
39
40
40
/**
41
41
* Flattened data
42
42
*
43
43
* @var array
44
44
*/
45
- private $ flatData ;
45
+ private $ flatData = [] ;
46
46
47
47
/**
48
48
* Injected configuration data
@@ -76,16 +76,18 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
76
76
*/
77
77
public function get ($ key = null , $ defaultValue = null )
78
78
{
79
- $ this ->load ();
80
79
if ($ key === null ) {
80
+ if (empty ($ this ->flatData )) {
81
+ $ this ->reloadData ();
82
+ }
81
83
return $ this ->flatData ;
82
84
}
83
-
84
- if (array_key_exists ($ key , $ this ->flatData ) && $ this ->flatData [$ key ] === null ) {
85
- return '' ;
85
+ $ result = $ this ->getByKey ($ key );
86
+ if ($ result === null ) {
87
+ $ this ->reloadData ();
88
+ $ result = $ this ->getByKey ($ key );
86
89
}
87
-
88
- return $ this ->flatData [$ key ] ?? $ defaultValue ;
90
+ return $ result ?? $ defaultValue ;
89
91
}
90
92
91
93
/**
@@ -97,27 +99,31 @@ public function get($key = null, $defaultValue = null)
97
99
*/
98
100
public function isAvailable ()
99
101
{
100
- $ this ->load ();
101
- return isset ($ this ->flatData [ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE ]);
102
+ return $ this ->get (ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE ) !== null ;
102
103
}
103
104
104
105
/**
105
106
* Gets a value specified key from config data
106
107
*
107
- * @param string $key
108
+ * @param string|null $key
108
109
* @return null|mixed
109
110
* @throws FileSystemException
110
111
* @throws RuntimeException
111
112
*/
112
113
public function getConfigData ($ key = null )
113
114
{
114
- $ this ->load ();
115
-
116
- if ($ key !== null && !isset ($ this ->data [$ key ])) {
117
- return null ;
115
+ if ($ key === null ) {
116
+ if (empty ($ this ->data )) {
117
+ $ this ->reloadData ();
118
+ }
119
+ return $ this ->data ;
118
120
}
119
-
120
- return $ this ->data [$ key ] ?? $ this ->data ;
121
+ $ result = $ this ->getConfigDataByKey ($ key );
122
+ if ($ result === null ) {
123
+ $ this ->reloadData ();
124
+ $ result = $ this ->getConfigDataByKey ($ key );
125
+ }
126
+ return $ result ;
121
127
}
122
128
123
129
/**
@@ -127,7 +133,8 @@ public function getConfigData($key = null)
127
133
*/
128
134
public function resetData ()
129
135
{
130
- $ this ->data = null ;
136
+ $ this ->data = [];
137
+ $ this ->flatData = [];
131
138
}
132
139
133
140
/**
@@ -140,8 +147,7 @@ public function resetData()
140
147
*/
141
148
public function isDbAvailable ()
142
149
{
143
- $ this ->load ();
144
- return isset ($ this ->data ['db ' ]);
150
+ return $ this ->getConfigData ('db ' ) !== null ;
145
151
}
146
152
147
153
/**
@@ -164,28 +170,26 @@ private function getEnvOverride() : array
164
170
* @throws FileSystemException
165
171
* @throws RuntimeException
166
172
*/
167
- private function load ()
173
+ private function reloadData (): void
168
174
{
169
- if (empty ($ this ->data )) {
170
- $ this ->data = array_replace (
171
- $ this ->reader ->load (),
172
- $ this ->overrideData ?? [],
173
- $ this ->getEnvOverride ()
174
- );
175
- // flatten data for config retrieval using get()
176
- $ this ->flatData = $ this ->flattenParams ($ this ->data );
177
-
178
- // allow reading values from env variables by convention
179
- // MAGENTO_DC_{path}, like db/connection/default/host =>
180
- // can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
181
- foreach (getenv () as $ key => $ value ) {
182
- if (false !== \strpos ($ key , self ::MAGENTO_ENV_PREFIX )
183
- && $ key !== self ::OVERRIDE_KEY
184
- ) {
185
- // convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
186
- $ flatKey = strtolower (str_replace ([self ::MAGENTO_ENV_PREFIX , '__ ' ], ['' , '/ ' ], $ key ));
187
- $ this ->flatData [$ flatKey ] = $ value ;
188
- }
175
+ $ this ->data = array_replace (
176
+ $ this ->reader ->load (),
177
+ $ this ->overrideData ?? [],
178
+ $ this ->getEnvOverride ()
179
+ );
180
+ // flatten data for config retrieval using get()
181
+ $ this ->flatData = $ this ->flattenParams ($ this ->data );
182
+
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 ;
189
193
}
190
194
}
191
195
}
@@ -197,12 +201,12 @@ private function load()
197
201
* each level of array is accessible by path key
198
202
*
199
203
* @param array $params
200
- * @param string $path
201
- * @param array $flattenResult
204
+ * @param string|null $path
205
+ * @param array|null $flattenResult
202
206
* @return array
203
207
* @throws RuntimeException
204
208
*/
205
- private function flattenParams (array $ params , $ path = null , array &$ flattenResult = null ) : array
209
+ private function flattenParams (array $ params , ? string $ path = null , array &$ flattenResult = null ): array
206
210
{
207
211
if (null === $ flattenResult ) {
208
212
$ flattenResult = [];
@@ -236,4 +240,30 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
236
240
237
241
return $ flattenResult ;
238
242
}
243
+
244
+ /**
245
+ * Returns flat data by key
246
+ *
247
+ * @param string|null $key
248
+ * @return mixed|null
249
+ */
250
+ private function getByKey (?string $ key )
251
+ {
252
+ if (array_key_exists ($ key , $ this ->flatData ) && $ this ->flatData [$ key ] === null ) {
253
+ return '' ;
254
+ }
255
+
256
+ return $ this ->flatData [$ key ] ?? null ;
257
+ }
258
+
259
+ /**
260
+ * Returns data by key
261
+ *
262
+ * @param string|null $key
263
+ * @return mixed|null
264
+ */
265
+ private function getConfigDataByKey (?string $ key )
266
+ {
267
+ return $ this ->data [$ key ] ?? null ;
268
+ }
239
269
}
0 commit comments