Skip to content

Commit

Permalink
Fix clean settings
Browse files Browse the repository at this point in the history
Do not forget settings that expect array as a value.
  • Loading branch information
oriceon committed Jul 8, 2017
1 parent da7004e commit 9612cef
Showing 1 changed file with 129 additions and 86 deletions.
215 changes: 129 additions & 86 deletions src/Repositories/DatabaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@

class DatabaseRepository
{
/**
* Registry config
*
* @var array
*/
protected $config;


/**
* Database manager instance
*
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;

/**
* Cache
*
* @var CacheRepository
*/
protected $cache;
/**
* Registry config
*
* @var array
*/
protected $config;


/**
* Database manager instance
*
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;

/**
* Cache
*
* @var CacheRepository
*/
protected $cache;

/**
* Constructor
Expand All @@ -37,16 +37,16 @@ class DatabaseRepository
* @param CacheRepository $cache
* @param array $config
*/
public function __construct(
DatabaseManager $database,
CacheRepository $cache,
$config = []
)
{
$this->database = $database;
$this->config = $config;
$this->cache = $cache;
}
public function __construct(
DatabaseManager $database,
CacheRepository $cache,
$config = []
)
{
$this->database = $database;
$this->config = $config;
$this->cache = $cache;
}

/**
* Store value into registry
Expand Down Expand Up @@ -103,31 +103,31 @@ public function set($key, $value)
return $value;
}

/**
* Gets a value
*
* @param string $key
* @param string $default
*
* @return mixed
*/
public function get($key, $default = null)
{
$value = $this->fetch($key);

if ( ! is_null($value))
{
return $value;
}

if ($default != null)
{
return $default;
}

if ($this->config['fallback'])
{
if ( ! is_null($this->config['primary_config_file']))
/**
* Gets a value
*
* @param string $key
* @param string $default
*
* @return mixed
*/
public function get($key, $default = null)
{
$value = $this->fetch($key);

if ( ! is_null($value))
{
return $value;
}

if ($default != null)
{
return $default;
}

if ($this->config['fallback'])
{
if ( ! is_null($this->config['primary_config_file']))
{
$key2 = $this->config['primary_config_file'] . '.' . $key;
if (Config::has($key2))
Expand All @@ -136,11 +136,11 @@ public function get($key, $default = null)
}
}

return Config::get($key, null);
}
return Config::get($key, null);
}

return $default;
}
}

/**
* Fetch all values
Expand Down Expand Up @@ -203,25 +203,25 @@ public function has($key)
return false;
}

/**
* Remove a setting
*
* @param string $key
*
* @return void
*/
public function forget($key)
{
/**
* Remove a setting
*
* @param string $key
*
* @return void
*/
public function forget($key)
{
$keyExp = explode('.', $key);


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

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

if ( ! is_null($row))
if ( ! is_null($row))
{
if (count($keyExp) > 1)
{
Expand All @@ -240,8 +240,8 @@ public function forget($key)
}


$this->cache->forget($key);
}
$this->cache->forget($key);
}

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


if (array_key_exists('flush', $params) && $params['flush'] == true)
{
$this->flush();
Expand All @@ -271,7 +270,10 @@ public function clean($params = [])
{
if ( ! array_key_exists($key, $default_settings))
{
$this->forget($key);
if ( ! $this->expect_array_as_a_value($key, $default_settings))
{
$this->forget($key);
}
}
}

Expand All @@ -281,17 +283,17 @@ public function clean($params = [])
}
}

/**
* Remove all settings
*
* @return bool
*/
public function flush()
{
$this->cache->flush();
/**
* Remove all settings
*
* @return bool
*/
public function flush()
{
$this->cache->flush();

return $this->database->table($this->config['db_table'])->delete();
}
return $this->database->table($this->config['db_table'])->delete();
}


/**
Expand Down Expand Up @@ -334,7 +336,7 @@ private function _update($default_settings, $settings)
// update with new settings
foreach ($default_settings as $key => $value)
{
if ( ! array_key_exists($key, $settings))
if ( ! $this->preg_key_exists($key, $settings))
{
$this->set($key, $value);
}
Expand Down Expand Up @@ -372,4 +374,45 @@ private function array_dot(array $array, $default_settings = false)
return $newArray;
}

private function expect_array_as_a_value($key, $default_settings)
{
$exp = explode('.', $key);
$cnt_exp = count($exp) - 1;

// if found prev key
if (isset($exp[$cnt_exp - 1]))
{
// unset last key
unset($exp[$cnt_exp]);

// reconstruct dotted keys
$key = implode('.', $exp);

// if found path in default settings
// and path expect value as a array
// then we should NOT forget path from settings
if (array_key_exists($key, $default_settings) && is_array($default_settings[$key]))
{
return true;
}
}

return false;
}

private function preg_key_exists($default_settings_key, array $settings)
{
$found = false;

foreach ($settings as $key => $value)
{
if (preg_match('/' . $default_settings_key . '/i', $key))
{
$found = true;
}
}

return $found;
}

}

0 comments on commit 9612cef

Please sign in to comment.