Skip to content

Commit

Permalink
changes in php config adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-graute committed Jan 12, 2021
1 parent 4eda9b5 commit 8012921
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Pressmind/Config/Adapter/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function read()
'testing' => [],
];

require_once $this->_config_file;
include_once $this->_config_file;
$return_config['development'] = $config['development'];
$return_config['production'] = array_merge($config['development'], $config['production']);
$return_config['testing'] = array_merge($config['development'], $config['testing']);
Expand All @@ -56,9 +56,29 @@ public function read()
*/
public function write($data)
{
require_once $this->_config_file;
include $this->_config_file;
/** @var array $config */
$config[$this->_environment] = $data;
file_put_contents($this->_config_file, var_export($config, true));
$config_text = "<?php\n\$config = " . $this->_var_export($config, true) . ';';
file_put_contents($this->_config_file, $config_text);
}

/**
* @param $expression
* @param bool $return
* @return mixed|string|string[]|null
*/
private function _var_export($expression, $return = false) {
$export = var_export($expression, true);
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
$array = preg_split("/\r\n|\n|\r/", $export);
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array);
$export = join(PHP_EOL, array_filter(["["] + $array));
if ($return) {
return $export;
} else {
echo $export;
}
return null;
}
}

0 comments on commit 8012921

Please sign in to comment.