Skip to content

Commit 9612cef

Browse files
committed
Fix clean settings
Do not forget settings that expect array as a value.
1 parent da7004e commit 9612cef

File tree

1 file changed

+129
-86
lines changed

1 file changed

+129
-86
lines changed

src/Repositories/DatabaseRepository.php

Lines changed: 129 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88

99
class DatabaseRepository
1010
{
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;
3232

3333
/**
3434
* Constructor
@@ -37,16 +37,16 @@ class DatabaseRepository
3737
* @param CacheRepository $cache
3838
* @param array $config
3939
*/
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+
}
5050

5151
/**
5252
* Store value into registry
@@ -103,31 +103,31 @@ public function set($key, $value)
103103
return $value;
104104
}
105105

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']))
131131
{
132132
$key2 = $this->config['primary_config_file'] . '.' . $key;
133133
if (Config::has($key2))
@@ -136,11 +136,11 @@ public function get($key, $default = null)
136136
}
137137
}
138138

139-
return Config::get($key, null);
140-
}
139+
return Config::get($key, null);
140+
}
141141

142142
return $default;
143-
}
143+
}
144144

145145
/**
146146
* Fetch all values
@@ -203,25 +203,25 @@ public function has($key)
203203
return false;
204204
}
205205

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+
{
215215
$keyExp = explode('.', $key);
216216

217217

218-
$query = $this->database
218+
$query = $this->database
219219
->table($this->config['db_table'])
220220
->where('setting_key', $keyExp[0]);
221221

222-
$row = $query->first(['setting_value']);
222+
$row = $query->first(['setting_value']);
223223

224-
if ( ! is_null($row))
224+
if ( ! is_null($row))
225225
{
226226
if (count($keyExp) > 1)
227227
{
@@ -240,8 +240,8 @@ public function forget($key)
240240
}
241241

242242

243-
$this->cache->forget($key);
244-
}
243+
$this->cache->forget($key);
244+
}
245245

246246
/**
247247
* Clean unused settings
@@ -255,7 +255,6 @@ public function clean($params = [])
255255
$default_settings = $this->array_dot(Config::get($this->config['primary_config_file']), true);
256256
$settings = $this->array_dot($this->getAll(false));
257257

258-
259258
if (array_key_exists('flush', $params) && $params['flush'] == true)
260259
{
261260
$this->flush();
@@ -271,7 +270,10 @@ public function clean($params = [])
271270
{
272271
if ( ! array_key_exists($key, $default_settings))
273272
{
274-
$this->forget($key);
273+
if ( ! $this->expect_array_as_a_value($key, $default_settings))
274+
{
275+
$this->forget($key);
276+
}
275277
}
276278
}
277279

@@ -281,17 +283,17 @@ public function clean($params = [])
281283
}
282284
}
283285

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();
292294

293-
return $this->database->table($this->config['db_table'])->delete();
294-
}
295+
return $this->database->table($this->config['db_table'])->delete();
296+
}
295297

296298

297299
/**
@@ -334,7 +336,7 @@ private function _update($default_settings, $settings)
334336
// update with new settings
335337
foreach ($default_settings as $key => $value)
336338
{
337-
if ( ! array_key_exists($key, $settings))
339+
if ( ! $this->preg_key_exists($key, $settings))
338340
{
339341
$this->set($key, $value);
340342
}
@@ -372,4 +374,45 @@ private function array_dot(array $array, $default_settings = false)
372374
return $newArray;
373375
}
374376

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+
375418
}

0 commit comments

Comments
 (0)