Skip to content

Commit

Permalink
remove metaconfig handling - rubbish idea; improve placeholder handling;
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Jun 7, 2016
1 parent 5fef5d1 commit fdc3dc4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
13 changes: 1 addition & 12 deletions src/Parser/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Ini extends AbstractParser
*/
public function parse($file)
{
$config = parse_ini_string($this->replaceConstants(file_get_contents($file)), true);
$config = parse_ini_string(file_get_contents($file), true);

if (false === $config) {
throw new ParserException(sprintf('Couldn\'t parse config file %s', $file));
Expand All @@ -29,17 +29,6 @@ public function parse($file)
return $this->expand($config);
}

/**
* @param $str
* @return mixed
*/
protected function replaceConstants($str)
{
return strtr($str, [
'%APP_PATH%' => APP_PATH,
]);
}

/**
* @param $config
* @return array
Expand Down
72 changes: 52 additions & 20 deletions src/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Space
*/
protected $usesEnvironment = true;

/**
* @var array placeholders
*/
protected $placeholders = [];

/**
* Set constructor.
* @param string $name
Expand Down Expand Up @@ -118,17 +123,6 @@ public function set($key, $value)
$this->config[$key] = $value;
}

/**
* we split this out to be able to easily change
* the filesystem implementation (e.g. in tests)
*
* @return string the class name of the filesystem helper
*/
protected function getFilesystemHelperName()
{
return 'Shared\Helper\Filesystem';
}

/**
* parses the configs
*
Expand All @@ -141,24 +135,34 @@ public function parse()
$config = [];
foreach ($this->getReadableSources() as $path) {
try {
$config = $this->merge($config, $parser->parse($path));
$config = $this->merge($config, $this->replacePlaceholders($parser->parse($path)));
} catch (ParserException $e) {
// ignore parse errors
}
}

if (!empty($config['meta']['environment'])) {
$this->setEnvironment($config['meta']['environment']);
if ($this->usesEnvironment) {
return $this->flattenByEnvironment($config, $this->getEnvironment());
}

if (!empty($config['meta']['use_environment'])) {
$this->usesEnvironment = (bool) $config['meta']['use_environment'];
}
return $config;
}

unset($config['meta']);
/**
* @param array $config
*/
protected function replacePlaceholders(array $config)
{
if (empty($this->placeholders)) {
return $config;
}

if ($this->usesEnvironment) {
return $this->flattenByEnvironment($config, $this->getEnvironment());
foreach ($config as $key => $value) {
if (is_array($value)) {
$config[$key] = $this->replacePlaceholders($value);
} else if (is_string($value)) {
$config[$key] = strtr($value, $this->placeholders);
}
}

return $config;
Expand Down Expand Up @@ -435,4 +439,32 @@ public function useEnvironment($flag = null)
return $this;
}

/**
* @return array
*/
public function getPlaceholders()
{
return $this->placeholders;
}

/**
* @param array $placeholders
* @return $this
*/
public function setPlaceholders(array $placeholders)
{
$this->placeholders = $placeholders;

return $this;
}

/**
* @param $placeholder
* @param $value
*/
public function addPlaceholder($placeholder, $value)
{
$this->placeholders[$placeholder] = $value;
}

}

0 comments on commit fdc3dc4

Please sign in to comment.