Skip to content

Commit

Permalink
Fix clean method
Browse files Browse the repository at this point in the history
If last value from key is numeric, escape it from clean
because setting value is an array and user could interact with it.
  • Loading branch information
oriceon committed May 25, 2017
1 parent 13da88c commit da7004e
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/Repositories/DatabaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ public function clean($params = [])
{
if ( ! empty($this->config['primary_config_file']))
{
$default_settings = array_dot(Config::get($this->config['primary_config_file']));
$settings = array_dot($this->getAll(false));
$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)
Expand All @@ -266,15 +266,16 @@ public function clean($params = [])
}
else
{
// clean unused settings
foreach ($settings as $key => $value)
{
// clean unused settings
if ( ! array_key_exists($key, $default_settings))
{
$this->forget($key);
}
}


$this->_update($default_settings, $settings);
}
}
Expand Down Expand Up @@ -330,14 +331,45 @@ private function fetch($key)
*/
private function _update($default_settings, $settings)
{
// update with new settings
foreach ($default_settings as $key => $value)
{
// update with new settings
if ( ! array_key_exists($key, $settings))
{
$this->set($key, $value);
}
}
}

private function array_dot(array $array, $default_settings = false)
{
$newArray = [];

$dots = array_dot($array);
foreach ($dots as $key => $value)
{
$expKey = explode('.', $key);
$lastKey = array_last($expKey);

if (is_numeric($lastKey))
{
$newKey = implode('.', array_slice($expKey, 0, -1));
$newValue = [];

if ($default_settings && ! empty($this->config['primary_config_file']))
{
$newValue = Config::get($this->config['primary_config_file'] . '.' . $newKey, $newValue);
}

$newArray[$newKey] = $newValue;
}
else
{
$newArray[$key] = $value;
}
}

return $newArray;
}

}

0 comments on commit da7004e

Please sign in to comment.