Skip to content

Commit

Permalink
Support exporting enviroment variables (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Nov 27, 2020
1 parent 67af382 commit b25aae9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/CosyComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,45 @@ public function handleDrupalContribSa($cdata)
}
}

/**
* Export things.
*/
protected function exportEnvVars()
{
if (!$this->project) {
return;
}
$env = $this->project->getEnvString();
if (empty($env)) {
return;
}
// One per line.
$env_array = preg_split("/\r\n|\n|\r/", $env);
if (empty($env_array)) {
return;
}
foreach ($env_array as $env_string) {
if (empty($env_string)) {
continue;
}
$env_parts = explode('=', $env_string, 2);
if (count($env_parts) != 2) {
continue;
}
// We do not allow to override ENV vars.
$key = $env_parts[0];
$existing_env = getenv($key);
if ($existing_env) {
$this->logger->log('info', new Message("The ENV variable $key was skipped because it exists and can not be overwritten"));
continue;
}
$value = $env_parts[1];
$this->logger->log('info', new Message("Exporting ENV variable $key: $value"));
putenv($env_string);
$_ENV[$key] = $value;
}
}

/**
* @throws \eiriksm\CosyComposer\Exceptions\ChdirException
* @throws \eiriksm\CosyComposer\Exceptions\GitCloneException
Expand All @@ -477,6 +516,8 @@ public function run()
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
}
}
// Export the environment variables if needed.
$this->exportEnvVars();
if (!empty($_SERVER['violinist_hostname'])) {
$this->log(sprintf('Running update check on %s', $_SERVER['violinist_hostname']));
}
Expand Down

0 comments on commit b25aae9

Please sign in to comment.