Skip to content
Snippets Groups Projects
Commit 6339ec8f authored by jurgenhaas's avatar jurgenhaas
Browse files

Merge remote-tracking branch 'origin/master'

parents c7dbe281 ccd81f17
No related branches found
No related tags found
No related merge requests found
Deploy:
script:
- curl -XPOST -H'content-type:application/json' "https://packagist.org/api/update-package?username=${PACKAGIST_USERNAME}&apiToken=${PACKAGIST_API_TOKEN}" -d'{"repository":{"url":"'${CI_PROJECT_URL}'"}}'
only:
- tags
- triggers
- schedules
......@@ -76,8 +76,8 @@ final class Dotenv {
if (empty($value)) {
$value = $default;
}
$this->put($key, $value);
}
$this->put($key, $value);
return $value;
}
......@@ -92,19 +92,80 @@ final class Dotenv {
* The name of the environment variable.
* @param string $value
* The value of the environment variable.
* @param bool $overwrite
*/
public function put($key, $value) {
public function put($key, $value, $overwrite = FALSE) {
$envContent = file_exists('.env') ? file_get_contents('.env') : '';
$data = $this->dotenv->parse($envContent);
$key = strtoupper($key);
if (!empty(getenv($key))) {
if (!$overwrite && isset($data[$key])) {
return;
}
$env = file_exists('.env') ? file_get_contents('.env') : '';
$env .= PHP_EOL . $key . '=' . $value . PHP_EOL;
file_put_contents('.env', $env);
$data[$key] = $value;
$envContent = '';
foreach ($data as $k => $v) {
$envContent .= $k . '=' . $v . PHP_EOL;
}
file_put_contents('.env', $envContent);
$this->load();
passthru(escapeshellcmd('git -c "user.email=dotenv@lakedrops.com" ignore .env'));
}
/**
* Replace environment variables in keys and values of a given array.
*
* Credit: https://github.com/composer/composer/pull/5837
*
* @param mixed $data
* The keyed array in which to replace environment variables.
*
* @return array
* The same data with environment variables being replaced.
*/
public function replaceEnvironmentVariables($data) {
array_walk_recursive($data, array($this, 'findEnvironmentVariables'));
return $data;
}
/**
* Identify and replace environment variables in a key pair
*
* @param string $item
* @param string $key
*/
private function findEnvironmentVariables(&$item, &$key) {
/** @noinspection NotOptimalRegularExpressionsInspection */
$pattern = '@\{\$env:([A-Za-z0-9_]*)\}@i';
preg_match_all($pattern, $item, $item_matches);
preg_match_all($pattern, $key, $key_matches);
if (!empty($item_matches[1])) {
$item = $this->replaceItemEnvironmentVariables($item, $item_matches[1]);
}
if (!empty($key_matches[1])) {
$key = $this->replaceItemEnvironmentVariables($key, $key_matches[1]);
}
}
/**
* Replace environment variables in a string
*
* @param string $item
* @param array $matches
*
* @return string
*/
private function replaceItemEnvironmentVariables($item, $matches) {
foreach ($matches as $var) {
$value = getenv($var);
if (!$value) {
$value = '';
}
$item = str_replace( '{$env:' . $var . '}', $value, $item );
}
return $item;
}
/**
* Load the local .env file.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment