8
8
9
9
class DatabaseRepository
10
10
{
11
- /**
12
- * Registry config
13
- *
14
- * @var array
15
- */
16
- protected $ config ;
17
-
18
-
19
- /**
20
- * Database manager instance
21
- *
22
- * @var \Illuminate\Database\DatabaseManager
23
- */
24
- protected $ database ;
25
-
26
- /**
27
- * Cache
28
- *
29
- * @var CacheRepository
30
- */
31
- protected $ cache ;
11
+ /**
12
+ * Registry config
13
+ *
14
+ * @var array
15
+ */
16
+ protected $ config ;
17
+
18
+
19
+ /**
20
+ * Database manager instance
21
+ *
22
+ * @var \Illuminate\Database\DatabaseManager
23
+ */
24
+ protected $ database ;
25
+
26
+ /**
27
+ * Cache
28
+ *
29
+ * @var CacheRepository
30
+ */
31
+ protected $ cache ;
32
32
33
33
/**
34
34
* Constructor
@@ -37,16 +37,16 @@ class DatabaseRepository
37
37
* @param CacheRepository $cache
38
38
* @param array $config
39
39
*/
40
- public function __construct (
41
- DatabaseManager $ database ,
42
- CacheRepository $ cache ,
43
- $ config = []
44
- )
45
- {
46
- $ this ->database = $ database ;
47
- $ this ->config = $ config ;
48
- $ this ->cache = $ cache ;
49
- }
40
+ public function __construct (
41
+ DatabaseManager $ database ,
42
+ CacheRepository $ cache ,
43
+ $ config = []
44
+ )
45
+ {
46
+ $ this ->database = $ database ;
47
+ $ this ->config = $ config ;
48
+ $ this ->cache = $ cache ;
49
+ }
50
50
51
51
/**
52
52
* Store value into registry
@@ -103,31 +103,31 @@ public function set($key, $value)
103
103
return $ value ;
104
104
}
105
105
106
- /**
107
- * Gets a value
108
- *
109
- * @param string $key
110
- * @param string $default
111
- *
112
- * @return mixed
113
- */
114
- public function get ($ key , $ default = null )
115
- {
116
- $ value = $ this ->fetch ($ key );
117
-
118
- if ( ! is_null ($ value ))
119
- {
120
- return $ value ;
121
- }
122
-
123
- if ($ default != null )
124
- {
125
- return $ default ;
126
- }
127
-
128
- if ($ this ->config ['fallback ' ])
129
- {
130
- if ( ! is_null ($ this ->config ['primary_config_file ' ]))
106
+ /**
107
+ * Gets a value
108
+ *
109
+ * @param string $key
110
+ * @param string $default
111
+ *
112
+ * @return mixed
113
+ */
114
+ public function get ($ key , $ default = null )
115
+ {
116
+ $ value = $ this ->fetch ($ key );
117
+
118
+ if ( ! is_null ($ value ))
119
+ {
120
+ return $ value ;
121
+ }
122
+
123
+ if ($ default != null )
124
+ {
125
+ return $ default ;
126
+ }
127
+
128
+ if ($ this ->config ['fallback ' ])
129
+ {
130
+ if ( ! is_null ($ this ->config ['primary_config_file ' ]))
131
131
{
132
132
$ key2 = $ this ->config ['primary_config_file ' ] . '. ' . $ key ;
133
133
if (Config::has ($ key2 ))
@@ -136,11 +136,11 @@ public function get($key, $default = null)
136
136
}
137
137
}
138
138
139
- return Config::get ($ key , null );
140
- }
139
+ return Config::get ($ key , null );
140
+ }
141
141
142
142
return $ default ;
143
- }
143
+ }
144
144
145
145
/**
146
146
* Fetch all values
@@ -203,25 +203,25 @@ public function has($key)
203
203
return false ;
204
204
}
205
205
206
- /**
207
- * Remove a setting
208
- *
209
- * @param string $key
210
- *
211
- * @return void
212
- */
213
- public function forget ($ key )
214
- {
206
+ /**
207
+ * Remove a setting
208
+ *
209
+ * @param string $key
210
+ *
211
+ * @return void
212
+ */
213
+ public function forget ($ key )
214
+ {
215
215
$ keyExp = explode ('. ' , $ key );
216
216
217
217
218
- $ query = $ this ->database
218
+ $ query = $ this ->database
219
219
->table ($ this ->config ['db_table ' ])
220
220
->where ('setting_key ' , $ keyExp [0 ]);
221
221
222
- $ row = $ query ->first (['setting_value ' ]);
222
+ $ row = $ query ->first (['setting_value ' ]);
223
223
224
- if ( ! is_null ($ row ))
224
+ if ( ! is_null ($ row ))
225
225
{
226
226
if (count ($ keyExp ) > 1 )
227
227
{
@@ -240,8 +240,8 @@ public function forget($key)
240
240
}
241
241
242
242
243
- $ this ->cache ->forget ($ key );
244
- }
243
+ $ this ->cache ->forget ($ key );
244
+ }
245
245
246
246
/**
247
247
* Clean unused settings
@@ -255,7 +255,6 @@ public function clean($params = [])
255
255
$ default_settings = $ this ->array_dot (Config::get ($ this ->config ['primary_config_file ' ]), true );
256
256
$ settings = $ this ->array_dot ($ this ->getAll (false ));
257
257
258
-
259
258
if (array_key_exists ('flush ' , $ params ) && $ params ['flush ' ] == true )
260
259
{
261
260
$ this ->flush ();
@@ -271,7 +270,10 @@ public function clean($params = [])
271
270
{
272
271
if ( ! array_key_exists ($ key , $ default_settings ))
273
272
{
274
- $ this ->forget ($ key );
273
+ if ( ! $ this ->expect_array_as_a_value ($ key , $ default_settings ))
274
+ {
275
+ $ this ->forget ($ key );
276
+ }
275
277
}
276
278
}
277
279
@@ -281,17 +283,17 @@ public function clean($params = [])
281
283
}
282
284
}
283
285
284
- /**
285
- * Remove all settings
286
- *
287
- * @return bool
288
- */
289
- public function flush ()
290
- {
291
- $ this ->cache ->flush ();
286
+ /**
287
+ * Remove all settings
288
+ *
289
+ * @return bool
290
+ */
291
+ public function flush ()
292
+ {
293
+ $ this ->cache ->flush ();
292
294
293
- return $ this ->database ->table ($ this ->config ['db_table ' ])->delete ();
294
- }
295
+ return $ this ->database ->table ($ this ->config ['db_table ' ])->delete ();
296
+ }
295
297
296
298
297
299
/**
@@ -334,7 +336,7 @@ private function _update($default_settings, $settings)
334
336
// update with new settings
335
337
foreach ($ default_settings as $ key => $ value )
336
338
{
337
- if ( ! array_key_exists ($ key , $ settings ))
339
+ if ( ! $ this -> preg_key_exists ($ key , $ settings ))
338
340
{
339
341
$ this ->set ($ key , $ value );
340
342
}
@@ -372,4 +374,45 @@ private function array_dot(array $array, $default_settings = false)
372
374
return $ newArray ;
373
375
}
374
376
377
+ private function expect_array_as_a_value ($ key , $ default_settings )
378
+ {
379
+ $ exp = explode ('. ' , $ key );
380
+ $ cnt_exp = count ($ exp ) - 1 ;
381
+
382
+ // if found prev key
383
+ if (isset ($ exp [$ cnt_exp - 1 ]))
384
+ {
385
+ // unset last key
386
+ unset($ exp [$ cnt_exp ]);
387
+
388
+ // reconstruct dotted keys
389
+ $ key = implode ('. ' , $ exp );
390
+
391
+ // if found path in default settings
392
+ // and path expect value as a array
393
+ // then we should NOT forget path from settings
394
+ if (array_key_exists ($ key , $ default_settings ) && is_array ($ default_settings [$ key ]))
395
+ {
396
+ return true ;
397
+ }
398
+ }
399
+
400
+ return false ;
401
+ }
402
+
403
+ private function preg_key_exists ($ default_settings_key , array $ settings )
404
+ {
405
+ $ found = false ;
406
+
407
+ foreach ($ settings as $ key => $ value )
408
+ {
409
+ if (preg_match ('/ ' . $ default_settings_key . '/i ' , $ key ))
410
+ {
411
+ $ found = true ;
412
+ }
413
+ }
414
+
415
+ return $ found ;
416
+ }
417
+
375
418
}
0 commit comments