Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables configurable thru yaml file #394

36 changes: 31 additions & 5 deletions src/Config/Environment.php
Expand Up @@ -10,6 +10,11 @@
*/
class Environment
{
/**
* @var GlobalSection
*/
private $stageConfig;

/**
* Regex pattern for detecting main branch.
* The name of the main branch must be started from one of three prefixes:
Expand All @@ -31,6 +36,16 @@ class Environment
const DEFAULT_ADMIN_FIRSTNAME = 'Admin';
const DEFAULT_ADMIN_LASTNAME = 'Username';

/**
* Environment constructor.
* @param GlobalSection $stageConfig
*/
public function __construct(
GlobalSection $stageConfig
) {
vkerkhoff marked this conversation as resolved.
Show resolved Hide resolved
$this->stageConfig = $stageConfig;
}

/**
* @var array
*/
Expand Down Expand Up @@ -60,7 +75,8 @@ public function getEnv(string $key)
*/
public function get(string $key, $default = null)
{
$value = $this->getEnv($key);
$envVarName = $this->getEnvironmentVariableName($key);
$value = $this->getEnv($envVarName);
if (false === $value) {
return $default;
}
Expand All @@ -79,7 +95,7 @@ public function getRoutes(): array
return $this->data['routes'];
}

return $this->data['routes'] = $this->get('MAGENTO_CLOUD_ROUTES', []);
return $this->data['routes'] = $this->get(StageConfigInterface::VAR_ENV_ROUTES, []);
}

/**
Expand All @@ -93,7 +109,7 @@ public function getRelationships(): array
return $this->data['relationships'];
}

return $this->data['relationships'] = $this->get('MAGENTO_CLOUD_RELATIONSHIPS', []);
return $this->data['relationships'] = $this->get(StageConfigInterface::VAR_ENV_RELATIONSHIPS, []);
}

/**
Expand All @@ -120,7 +136,7 @@ public function getVariables(): array
return $this->data['variables'];
}

return $this->data['variables'] = $this->get('MAGENTO_CLOUD_VARIABLES', []);
return $this->data['variables'] = $this->get(StageConfigInterface::VAR_ENV_VARIABLES, []);
}

/**
Expand All @@ -132,7 +148,17 @@ public function getApplication(): array
return $this->data['application'];
}

return $this->data['application'] = $this->get('MAGENTO_CLOUD_APPLICATION', []);
return $this->data['application'] = $this->get(StageConfigInterface::VAR_ENV_APPLICATION, []);
}

/**
* Get environment variable name from .magento.env.yaml configuration file
* @param string $name
* @return string
*/
protected function getEnvironmentVariableName(string $name): string
{
return $this->stageConfig->get($name);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Config/Schema.php
Expand Up @@ -155,6 +155,42 @@ public function getSchema()
StageConfigInterface::STAGE_DEPLOY => false,
],
],
StageConfigInterface::VAR_ENV_RELATIONSHIPS => [
self::SCHEMA_TYPE => ['string'],
self::SCHEMA_STAGE => [
StageConfigInterface::STAGE_GLOBAL
],
self::SCHEMA_DEFAULT_VALUE => [
StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_RELATIONSHIPS',
],
],
StageConfigInterface::VAR_ENV_ROUTES => [
self::SCHEMA_TYPE => ['string'],
self::SCHEMA_STAGE => [
StageConfigInterface::STAGE_GLOBAL
],
self::SCHEMA_DEFAULT_VALUE => [
StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_ROUTES',
],
],
StageConfigInterface::VAR_ENV_VARIABLES => [
self::SCHEMA_TYPE => ['string'],
self::SCHEMA_STAGE => [
StageConfigInterface::STAGE_GLOBAL
],
self::SCHEMA_DEFAULT_VALUE => [
StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_VARIABLES',
],
],
StageConfigInterface::VAR_ENV_APPLICATION => [
self::SCHEMA_TYPE => ['string'],
self::SCHEMA_STAGE => [
StageConfigInterface::STAGE_GLOBAL
],
self::SCHEMA_DEFAULT_VALUE => [
StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_APPLICATION',
],
],
StageConfigInterface::VAR_SKIP_HTML_MINIFICATION => [
self::SCHEMA_TYPE => ['boolean'],
self::SCHEMA_STAGE => [
Expand Down
8 changes: 8 additions & 0 deletions src/Config/StageConfigInterface.php
Expand Up @@ -36,6 +36,14 @@ interface StageConfigInterface
const VAR_SKIP_HTML_MINIFICATION = 'SKIP_HTML_MINIFICATION';
const VAR_SCD_MATRIX = 'SCD_MATRIX';

/**
* Environment variables.
*/
vkerkhoff marked this conversation as resolved.
Show resolved Hide resolved
const VAR_ENV_RELATIONSHIPS = 'ENV_RELATIONSHIPS';
const VAR_ENV_ROUTES = 'ENV_ROUTES';
const VAR_ENV_VARIABLES = 'ENV_VARIABLES';
const VAR_ENV_APPLICATION = 'ENV_APPLICATION';

/**
* Settings for deployment from git.
*/
Expand Down