@@ -38,7 +38,7 @@ class DeploymentConfig
38
38
*
39
39
* @var array
40
40
*/
41
- private $ flatData = [] ;
41
+ private $ flatData ;
42
42
43
43
/**
44
44
* Injected configuration data
@@ -55,7 +55,7 @@ class DeploymentConfig
55
55
* @param DeploymentConfig\Reader $reader
56
56
* @param array $overrideData
57
57
*/
58
- public function __construct (DeploymentConfig \Reader $ reader , $ overrideData = [])
58
+ public function __construct (DeploymentConfig \Reader $ reader , array $ overrideData = [])
59
59
{
60
60
$ this ->reader = $ reader ;
61
61
$ this ->overrideData = $ overrideData ;
@@ -64,63 +64,50 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
64
64
/**
65
65
* Gets data from flattened data
66
66
*
67
- * @param string $key
67
+ * @param string|null $key
68
68
* @param mixed $defaultValue
69
69
* @return mixed|null
70
70
* @throws FileSystemException
71
71
* @throws RuntimeException
72
72
*/
73
- public function get ($ key = null , $ defaultValue = null )
73
+ public function get (string $ key = null , $ defaultValue = null )
74
74
{
75
75
$ result = $ this ->getByKey ($ key );
76
76
if ($ result === null ) {
77
- $ this ->resetData ();
78
- $ this ->load ();
77
+ $ this ->reloadData ();
79
78
$ result = $ this ->getByKey ($ key );
80
79
}
81
80
return $ result ?? $ defaultValue ;
82
81
}
83
82
84
- /**
85
- * Checks if data available
86
- *
87
- * @return bool
88
- * @throws FileSystemException
89
- * @throws RuntimeException
90
- */
91
- public function isAvailable ()
92
- {
93
- $ this ->load ();
94
- return isset ($ this ->flatData [ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE ]);
95
- }
96
-
97
83
/**
98
84
* Gets a value specified key from config data
99
85
*
100
- * @param string $key
86
+ * @param string|null $key
101
87
* @return null|mixed
102
88
* @throws FileSystemException
103
89
* @throws RuntimeException
104
90
*/
105
- public function getConfigData ($ key = null )
91
+ public function getConfigData (string $ key = null )
106
92
{
107
- $ this ->load ( );
108
-
109
- if ( $ key !== null && ! isset ( $ this ->data [ $ key ])) {
110
- return null ;
93
+ $ result = $ this ->getConfigDataByKey ( $ key );
94
+ if ( $ result === null ) {
95
+ $ this ->reloadData ();
96
+ $ result = $ this -> getConfigDataByKey ( $ key ) ;
111
97
}
112
-
113
- return $ this ->data [$ key ] ?? $ this ->data ;
98
+ return $ result ;
114
99
}
115
100
116
101
/**
117
- * Resets config data
102
+ * Checks if data available
118
103
*
119
- * @return void
104
+ * @return bool
105
+ * @throws FileSystemException
106
+ * @throws RuntimeException
120
107
*/
121
- public function resetData ()
108
+ public function isAvailable (): bool
122
109
{
123
- $ this ->data = null ;
110
+ return $ this ->get (ConfigOptionsListConstants:: CONFIG_PATH_INSTALL_DATE ) != = null ;
124
111
}
125
112
126
113
/**
@@ -131,10 +118,20 @@ public function resetData()
131
118
* @throws RuntimeException
132
119
* @since 100.1.3
133
120
*/
134
- public function isDbAvailable ()
121
+ public function isDbAvailable (): bool
135
122
{
136
- $ this ->load ();
137
- return isset ($ this ->data ['db ' ]);
123
+ return $ this ->getConfigData ('db ' ) !== null ;
124
+ }
125
+
126
+ /**
127
+ * Resets config data
128
+ *
129
+ * @return void
130
+ */
131
+ public function resetData (): void
132
+ {
133
+ $ this ->data = null ;
134
+ $ this ->flatData = null ;
138
135
}
139
136
140
137
/**
@@ -144,16 +141,14 @@ public function isDbAvailable()
144
141
* @throws FileSystemException
145
142
* @throws RuntimeException
146
143
*/
147
- private function load ()
144
+ private function reloadData (): void
148
145
{
149
- if (empty ($ this ->data )) {
150
- $ this ->data = $ this ->reader ->load ();
151
- if ($ this ->overrideData ) {
152
- $ this ->data = array_replace ($ this ->data , $ this ->overrideData );
153
- }
154
- // flatten data for config retrieval using get()
155
- $ this ->flatData = $ this ->flattenParams ($ this ->data );
146
+ $ this ->data = $ this ->reader ->load ();
147
+ if ($ this ->overrideData ) {
148
+ $ this ->data = array_replace ($ this ->data , $ this ->overrideData );
156
149
}
150
+ // flatten data for config retrieval using get()
151
+ $ this ->flatData = $ this ->flattenParams ($ this ->data );
157
152
}
158
153
159
154
/**
@@ -163,12 +158,12 @@ private function load()
163
158
* each level of array is accessible by path key
164
159
*
165
160
* @param array $params
166
- * @param string $path
167
- * @param array $flattenResult
161
+ * @param string|null $path
162
+ * @param array|null $flattenResult
168
163
* @return array
169
164
* @throws RuntimeException
170
165
*/
171
- private function flattenParams (array $ params , $ path = null , array &$ flattenResult = null ): array
166
+ private function flattenParams (array $ params , ? string $ path = null , array &$ flattenResult = null ): array
172
167
{
173
168
if (null === $ flattenResult ) {
174
169
$ flattenResult = [];
@@ -195,17 +190,29 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
195
190
196
191
/**
197
192
* @param string|null $key
198
- * @return array| mixed|string
193
+ * @return mixed|null
199
194
*/
200
- protected function getByKey ($ key )
195
+ private function getByKey (? string $ key )
201
196
{
202
197
if ($ key === null ) {
203
198
return $ this ->flatData ?: null ;
204
199
}
205
- if (array_key_exists ($ key , $ this ->flatData ) && $ this ->flatData [$ key ] === null ) {
200
+ if (is_array ( $ this -> flatData ) && array_key_exists ($ key , $ this ->flatData ) && $ this ->flatData [$ key ] === null ) {
206
201
return '' ;
207
202
}
208
203
209
204
return $ this ->flatData [$ key ] ?? null ;
210
205
}
206
+
207
+ /**
208
+ * @param string|null $key
209
+ * @return mixed|null
210
+ */
211
+ private function getConfigDataByKey (?string $ key )
212
+ {
213
+ if ($ key === null ) {
214
+ return $ this ->data ;
215
+ }
216
+ return $ this ->data [$ key ] ?? null ;
217
+ }
211
218
}
0 commit comments