Skip to content

Commit

Permalink
feat(loader): resolve ref late so future values are interpolated
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Jan 9, 2020
1 parent 7dde0e3 commit 34d8e2b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,32 @@ protected function setEnv(array $vars, $override, $mode = self::PUTENV)
continue;
}

$value = $this->resolveRef($value);

$this->toEnv($key, $value, $mode);
$this->toServer($key, $value, $mode);
$this->toPutenv($key, $value, $mode);
$this->set($key, $value, $mode);
}

$this->resolveRefs($vars, $mode);
}

protected function resolveRef($value)
protected function set($key, $value, $mode)
{
if (!$value || \strpos($value, '${') === false) {
return $value;
}
$this->toEnv($key, $value, $mode);
$this->toServer($key, $value, $mode);
$this->toPutenv($key, $value, $mode);
}

return \preg_replace_callback('~\$\{(\w+)\}~', function ($m) {
return (null === $ref = Retriever::getEnv($m[1], null)) ? $m[0] : $ref;
}, $value);
protected function resolveRefs($vars, $mode)
{
foreach ($vars as $key => $value) {
if (!$value || \strpos($value, '${') === false) {
continue;
}

$value = \preg_replace_callback('~\$\{(\w+)\}~', function ($m) {
return (null === $ref = Retriever::getEnv($m[1], null)) ? $m[0] : $ref;
}, $value);

$this->set($key, $value, $mode);
}
}

private function toEnv($key, $value, $mode)
Expand Down

0 comments on commit 34d8e2b

Please sign in to comment.