From d0e61570598114f379c2b84e14913d56ce45bfb8 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 09:11:48 +0100 Subject: [PATCH 01/12] Environment variables configurable thru yaml file Make the RELATIONSHIP/ROUTES/APPLICATION and VARIABLES configurable thru the .magento.app.yaml file to allow users to specify an alternative variable name that is used to get the configuration value. An example is the use of the ECE tools on a native Platform.sh environment where the variables used are prefixed by PLATFORM instead of MAGENTO_CLOUD. --- src/Config/Environment.php | 36 +++++++++++++++++++++++++---- src/Config/Schema.php | 36 +++++++++++++++++++++++++++++ src/Config/StageConfigInterface.php | 8 +++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/Config/Environment.php b/src/Config/Environment.php index 7947436381..f1df958945 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -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: @@ -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 + ) { + $this->stageConfig = $stageConfig; + } + /** * @var array */ @@ -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; } @@ -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, []); } /** @@ -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, []); } /** @@ -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, []); } /** @@ -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); } /** diff --git a/src/Config/Schema.php b/src/Config/Schema.php index 41b764db82..c7d112b6a0 100644 --- a/src/Config/Schema.php +++ b/src/Config/Schema.php @@ -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 => [ diff --git a/src/Config/StageConfigInterface.php b/src/Config/StageConfigInterface.php index 669ba7cda2..b9f336fe79 100644 --- a/src/Config/StageConfigInterface.php +++ b/src/Config/StageConfigInterface.php @@ -36,6 +36,14 @@ interface StageConfigInterface const VAR_SKIP_HTML_MINIFICATION = 'SKIP_HTML_MINIFICATION'; const VAR_SCD_MATRIX = 'SCD_MATRIX'; + /** + * Environment variables. + */ + 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. */ From 07c534553943adcb0e86686e9a255aae789ce43e Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 12:38:35 +0100 Subject: [PATCH 02/12] Moved variable section to own system element Added extra section in the .magento.env.yaml that allows storing the new variable options Created a reader/processor for getting the options from the system section --- src/App/Container.php | 7 +- src/Config/Environment.php | 21 +++--- src/Config/Schema.php | 33 +++++----- src/Config/Stage/Deploy.php | 4 +- src/Config/System/Variables.php | 82 ++++++++++++++++++++++++ src/Config/System/VariablesInterface.php | 15 +++++ src/Config/SystemConfigInterface.php | 44 +++++++++++++ 7 files changed, 177 insertions(+), 29 deletions(-) create mode 100644 src/Config/System/Variables.php create mode 100644 src/Config/System/VariablesInterface.php create mode 100644 src/Config/SystemConfigInterface.php diff --git a/src/App/Container.php b/src/App/Container.php index a5c57e62f8..85c2f9f78d 100644 --- a/src/App/Container.php +++ b/src/App/Container.php @@ -124,7 +124,7 @@ function () { $this->container->singleton(\Magento\MagentoCloud\Config\Environment\Reader::class); $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Build::class); $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Deploy::class); - $this->container->singleton(\Magento\MagentoCloud\Config\Stage\PostDeploy::class); + $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Variables::class); $this->container->singleton(\Magento\MagentoCloud\Config\RepositoryFactory::class); $this->container->singleton( \Magento\MagentoCloud\Config\Stage\BuildInterface::class, @@ -138,6 +138,11 @@ function () { \Magento\MagentoCloud\Config\Stage\PostDeployInterface::class, \Magento\MagentoCloud\Config\Stage\PostDeploy::class ); + $this->container->singleton( + \Magento\MagentoCloud\Config\System\VariablesInterface::class, + \Magento\MagentoCloud\Config\System\Variables::class + ); + $this->container->singleton(\Magento\MagentoCloud\Shell\UtilityManager::class); $this->container->singleton( \Magento\MagentoCloud\View\RendererInterface::class, diff --git a/src/Config/Environment.php b/src/Config/Environment.php index f1df958945..cb1127bd41 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -5,15 +5,16 @@ */ namespace Magento\MagentoCloud\Config; +use Magento\MagentoCloud\Config\System\VariablesInterface; /** * Contains logic for interacting with the server environment */ class Environment { /** - * @var GlobalSection + * @var VariablesInterface */ - private $stageConfig; + private $systemConfig; /** * Regex pattern for detecting main branch. @@ -38,12 +39,12 @@ class Environment /** * Environment constructor. - * @param GlobalSection $stageConfig + * @param VariablesInterface $systemConfig */ public function __construct( - GlobalSection $stageConfig + VariablesInterface $systemConfig ) { - $this->stageConfig = $stageConfig; + $this->systemConfig = $systemConfig; } /** @@ -95,7 +96,7 @@ public function getRoutes(): array return $this->data['routes']; } - return $this->data['routes'] = $this->get(StageConfigInterface::VAR_ENV_ROUTES, []); + return $this->data['routes'] = $this->get(SystemConfigInterface::VAR_ENV_ROUTES, []); } /** @@ -109,7 +110,7 @@ public function getRelationships(): array return $this->data['relationships']; } - return $this->data['relationships'] = $this->get(StageConfigInterface::VAR_ENV_RELATIONSHIPS, []); + return $this->data['relationships'] = $this->get(SystemConfigInterface::VAR_ENV_RELATIONSHIPS, []); } /** @@ -136,7 +137,7 @@ public function getVariables(): array return $this->data['variables']; } - return $this->data['variables'] = $this->get(StageConfigInterface::VAR_ENV_VARIABLES, []); + return $this->data['variables'] = $this->get(SystemConfigInterface::VAR_ENV_VARIABLES, []); } /** @@ -148,7 +149,7 @@ public function getApplication(): array return $this->data['application']; } - return $this->data['application'] = $this->get(StageConfigInterface::VAR_ENV_APPLICATION, []); + return $this->data['application'] = $this->get(SystemConfigInterface::VAR_ENV_APPLICATION, []); } /** @@ -158,7 +159,7 @@ public function getApplication(): array */ protected function getEnvironmentVariableName(string $name): string { - return $this->stageConfig->get($name); + return $this->systemConfig->get($name); } /** diff --git a/src/Config/Schema.php b/src/Config/Schema.php index c7d112b6a0..ebcec15ac5 100644 --- a/src/Config/Schema.php +++ b/src/Config/Schema.php @@ -16,6 +16,7 @@ class Schema const SCHEMA_TYPE = 'type'; const SCHEMA_VALUE_VALIDATION = 'value_validation'; const SCHEMA_STAGE = 'stage'; + const SCHEMA_SYSTEM = 'system'; const SCHEMA_DEFAULT_VALUE = 'default_values'; /** @@ -155,40 +156,40 @@ public function getSchema() StageConfigInterface::STAGE_DEPLOY => false, ], ], - StageConfigInterface::VAR_ENV_RELATIONSHIPS => [ + SystemConfigInterface::VAR_ENV_RELATIONSHIPS => [ self::SCHEMA_TYPE => ['string'], - self::SCHEMA_STAGE => [ - StageConfigInterface::STAGE_GLOBAL + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES ], self::SCHEMA_DEFAULT_VALUE => [ - StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_RELATIONSHIPS', + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_RELATIONSHIPS', ], ], - StageConfigInterface::VAR_ENV_ROUTES => [ + SystemConfigInterface::VAR_ENV_ROUTES => [ self::SCHEMA_TYPE => ['string'], - self::SCHEMA_STAGE => [ - StageConfigInterface::STAGE_GLOBAL + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES ], self::SCHEMA_DEFAULT_VALUE => [ - StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_ROUTES', + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_ROUTES', ], ], - StageConfigInterface::VAR_ENV_VARIABLES => [ + SystemConfigInterface::VAR_ENV_VARIABLES => [ self::SCHEMA_TYPE => ['string'], - self::SCHEMA_STAGE => [ - StageConfigInterface::STAGE_GLOBAL + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES ], self::SCHEMA_DEFAULT_VALUE => [ - StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_VARIABLES', + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_VARIABLES', ], ], - StageConfigInterface::VAR_ENV_APPLICATION => [ + SystemConfigInterface::VAR_ENV_APPLICATION => [ self::SCHEMA_TYPE => ['string'], - self::SCHEMA_STAGE => [ - StageConfigInterface::STAGE_GLOBAL + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES ], self::SCHEMA_DEFAULT_VALUE => [ - StageConfigInterface::STAGE_GLOBAL => 'MAGENTO_CLOUD_APPLICATION', + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_APPLICATION', ], ], StageConfigInterface::VAR_SKIP_HTML_MINIFICATION => [ diff --git a/src/Config/Stage/Deploy.php b/src/Config/Stage/Deploy.php index 2c58a9c564..eefd19c9a4 100644 --- a/src/Config/Stage/Deploy.php +++ b/src/Config/Stage/Deploy.php @@ -34,7 +34,7 @@ class Deploy implements DeployInterface private $mergedConfig; /** - * @var Environment + * @var Variables */ private $environment; @@ -44,7 +44,7 @@ class Deploy implements DeployInterface private $schema; /** - * @param Environment $environment + * @param Variables $environment * @param EnvironmentReader $environmentReader * @param EnvironmentConfig $environmentConfig * @param Schema $schema diff --git a/src/Config/System/Variables.php b/src/Config/System/Variables.php new file mode 100644 index 0000000000..dfd0283822 --- /dev/null +++ b/src/Config/System/Variables.php @@ -0,0 +1,82 @@ +environmentReader = $environmentReader; + $this->schema = $schema; + } + + /** + * @inheritdoc + */ + public function get(string $name) + { + if (!array_key_exists($name, $this->schema->getDefaults(SystemConfigInterface::SYSTEM_VARIABLES))) { + throw new \RuntimeException(sprintf( + 'Config %s was not defined.', + $name + )); + } + + try { + return $this->mergeConfig()[$name]; + } catch (\Exception $exception) { + throw new \RuntimeException( + $exception->getMessage(), + $exception->getCode(), + $exception + ); + } + } + + /** + * @return array + * @throws \Magento\MagentoCloud\Filesystem\FileSystemException + */ + private function mergeConfig(): array + { + if (null === $this->mergedConfig) { + $envConfig = $this->environmentReader->read()[self::SECTION_SYSTEM] ?? []; + + $this->mergedConfig = array_replace( + $this->schema->getDefaults(SystemConfigInterface::SYSTEM_VARIABLES), + $envConfig[self::SYSTEM_VARIABLES] ?? [] + ); + } + + return $this->mergedConfig; + } +} diff --git a/src/Config/System/VariablesInterface.php b/src/Config/System/VariablesInterface.php new file mode 100644 index 0000000000..751f4902dd --- /dev/null +++ b/src/Config/System/VariablesInterface.php @@ -0,0 +1,15 @@ + Date: Wed, 12 Dec 2018 12:43:38 +0100 Subject: [PATCH 03/12] Fixed: Unwanted rename of class --- src/App/Container.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/Container.php b/src/App/Container.php index 85c2f9f78d..93db48748e 100644 --- a/src/App/Container.php +++ b/src/App/Container.php @@ -124,7 +124,7 @@ function () { $this->container->singleton(\Magento\MagentoCloud\Config\Environment\Reader::class); $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Build::class); $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Deploy::class); - $this->container->singleton(\Magento\MagentoCloud\Config\Stage\Variables::class); + $this->container->singleton(\Magento\MagentoCloud\Config\Stage\PostDeploy::class); $this->container->singleton(\Magento\MagentoCloud\Config\RepositoryFactory::class); $this->container->singleton( \Magento\MagentoCloud\Config\Stage\BuildInterface::class, From 43abc92345944c7bb7600a1b4b779e62595341d1 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 12:50:55 +0100 Subject: [PATCH 04/12] Fixed: Travis build error --- src/Config/Environment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Config/Environment.php b/src/Config/Environment.php index cb1127bd41..3c727c8b11 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -6,6 +6,7 @@ namespace Magento\MagentoCloud\Config; use Magento\MagentoCloud\Config\System\VariablesInterface; + /** * Contains logic for interacting with the server environment */ From 1021a1aa29d0ae2773a0c1cabe008d078ba034b1 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 13:16:03 +0100 Subject: [PATCH 05/12] Removed unneeded VariablesInterface --- src/App/Container.php | 4 ---- src/Config/Environment.php | 8 ++++---- src/Config/System/Variables.php | 2 +- src/Config/System/VariablesInterface.php | 15 --------------- 4 files changed, 5 insertions(+), 24 deletions(-) delete mode 100644 src/Config/System/VariablesInterface.php diff --git a/src/App/Container.php b/src/App/Container.php index 93db48748e..716d46aab9 100644 --- a/src/App/Container.php +++ b/src/App/Container.php @@ -138,10 +138,6 @@ function () { \Magento\MagentoCloud\Config\Stage\PostDeployInterface::class, \Magento\MagentoCloud\Config\Stage\PostDeploy::class ); - $this->container->singleton( - \Magento\MagentoCloud\Config\System\VariablesInterface::class, - \Magento\MagentoCloud\Config\System\Variables::class - ); $this->container->singleton(\Magento\MagentoCloud\Shell\UtilityManager::class); $this->container->singleton( diff --git a/src/Config/Environment.php b/src/Config/Environment.php index 3c727c8b11..47da043ed4 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -5,7 +5,7 @@ */ namespace Magento\MagentoCloud\Config; -use Magento\MagentoCloud\Config\System\VariablesInterface; +use Magento\MagentoCloud\Config\System\Variables; /** * Contains logic for interacting with the server environment @@ -13,7 +13,7 @@ class Environment { /** - * @var VariablesInterface + * @var Variables */ private $systemConfig; @@ -40,10 +40,10 @@ class Environment /** * Environment constructor. - * @param VariablesInterface $systemConfig + * @param Variables $systemConfig */ public function __construct( - VariablesInterface $systemConfig + Variables $systemConfig ) { $this->systemConfig = $systemConfig; } diff --git a/src/Config/System/Variables.php b/src/Config/System/Variables.php index dfd0283822..cb95023cae 100644 --- a/src/Config/System/Variables.php +++ b/src/Config/System/Variables.php @@ -12,7 +12,7 @@ /** * @inheritdoc */ -class Variables implements VariablesInterface +class Variables implements SystemConfigInterface { /** * @var EnvironmentReader diff --git a/src/Config/System/VariablesInterface.php b/src/Config/System/VariablesInterface.php deleted file mode 100644 index 751f4902dd..0000000000 --- a/src/Config/System/VariablesInterface.php +++ /dev/null @@ -1,15 +0,0 @@ - Date: Wed, 12 Dec 2018 13:36:06 +0100 Subject: [PATCH 06/12] Fixed: Build error on testGet --- src/Config/Environment.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Config/Environment.php b/src/Config/Environment.php index 47da043ed4..2f3f3424db 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -77,8 +77,7 @@ public function getEnv(string $key) */ public function get(string $key, $default = null) { - $envVarName = $this->getEnvironmentVariableName($key); - $value = $this->getEnv($envVarName); + $value = $this->getEnv($key); if (false === $value) { return $default; } @@ -86,6 +85,17 @@ public function get(string $key, $default = null) return json_decode(base64_decode($value), true); } + /** + * Get environment variable and get the name from .magento.env.yaml configuration file + * @param string $name + * @param string|int|null $default + * @return array|string|int|null + */ + protected function getEnvVar(string $name, $default = null) + { + return $this->get($this->systemConfig->get($name), $default); + } + /** * Get routes information from MagentoCloud environment variable. * @@ -97,7 +107,7 @@ public function getRoutes(): array return $this->data['routes']; } - return $this->data['routes'] = $this->get(SystemConfigInterface::VAR_ENV_ROUTES, []); + return $this->data['routes'] = $this->getEnvVar(SystemConfigInterface::VAR_ENV_ROUTES, []); } /** @@ -111,7 +121,7 @@ public function getRelationships(): array return $this->data['relationships']; } - return $this->data['relationships'] = $this->get(SystemConfigInterface::VAR_ENV_RELATIONSHIPS, []); + return $this->data['relationships'] = $this->getEnvVar(SystemConfigInterface::VAR_ENV_RELATIONSHIPS, []); } /** @@ -138,7 +148,7 @@ public function getVariables(): array return $this->data['variables']; } - return $this->data['variables'] = $this->get(SystemConfigInterface::VAR_ENV_VARIABLES, []); + return $this->data['variables'] = $this->getEnvVar(SystemConfigInterface::VAR_ENV_VARIABLES, []); } /** @@ -150,17 +160,7 @@ public function getApplication(): array return $this->data['application']; } - return $this->data['application'] = $this->get(SystemConfigInterface::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->systemConfig->get($name); + return $this->data['application'] = $this->getEnvVar(SystemConfigInterface::VAR_ENV_APPLICATION, []); } /** From e836ae3263591634004932c9549542338ee6a02f Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 13:58:59 +0100 Subject: [PATCH 07/12] Removed un-used constant --- src/Config/SystemConfigInterface.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Config/SystemConfigInterface.php b/src/Config/SystemConfigInterface.php index 55fdfb89b6..0b7c3ba3af 100644 --- a/src/Config/SystemConfigInterface.php +++ b/src/Config/SystemConfigInterface.php @@ -28,11 +28,6 @@ interface SystemConfigInterface const VAR_ENV_VARIABLES = 'ENV_VARIABLES'; const VAR_ENV_APPLICATION = 'ENV_APPLICATION'; - /** - * Option for enabling merging given configuration with default configuration - */ - const OPTION_MERGE = '_merge'; - /** * Retrieves environment configuration per stage. * From f605941faa98c79649aba42b933c78bd3b3b039d Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 14:01:08 +0100 Subject: [PATCH 08/12] Added Mock of depended class --- src/Test/Unit/Config/EnvironmentTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Test/Unit/Config/EnvironmentTest.php b/src/Test/Unit/Config/EnvironmentTest.php index 0ba6dae794..7a04e0686e 100644 --- a/src/Test/Unit/Config/EnvironmentTest.php +++ b/src/Test/Unit/Config/EnvironmentTest.php @@ -6,6 +6,7 @@ namespace Magento\MagentoCloud\Test\Unit\Config; use Magento\MagentoCloud\Config\Environment; +use Magento\MagentoCloud\Config\System\Variables; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as Mock; @@ -24,6 +25,7 @@ class EnvironmentTest extends TestCase * @var array */ private $environmentData; + private $variableMock; /** * @inheritdoc @@ -32,7 +34,9 @@ protected function setUp() { $this->environmentData = $_ENV; - $this->environment = new Environment(); + $this->variableMock = $this->createMock(Variables::class); + + $this->environment = new Environment($this->variableMock); } /** From 02a4f5ad687f0c656276ef0f4fae4a170c0f79d1 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Wed, 12 Dec 2018 15:39:00 +0100 Subject: [PATCH 09/12] Added docblock --- src/Test/Unit/Config/EnvironmentTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Test/Unit/Config/EnvironmentTest.php b/src/Test/Unit/Config/EnvironmentTest.php index 7a04e0686e..60b9cb709a 100644 --- a/src/Test/Unit/Config/EnvironmentTest.php +++ b/src/Test/Unit/Config/EnvironmentTest.php @@ -25,6 +25,9 @@ class EnvironmentTest extends TestCase * @var array */ private $environmentData; + /** + * @var Variables + */ private $variableMock; /** From d8e623d258fb28a2c27cdc5380f9b123fb5b1ca0 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Thu, 13 Dec 2018 11:16:34 +0100 Subject: [PATCH 10/12] Added usage documentation Fixed comments from code review (doc blocks and code styling) Added MAGENTO_CLOUD_MODE and MAGENTO_CLOUD_ENVIRONMENT variables to be configurable Updated Unit Tests to cover changes --- dist/.magento.env.yaml | 54 ++++++++ src/Config/Environment.php | 29 ++-- src/Config/Schema.php | 18 +++ src/Config/Stage/Deploy.php | 4 +- src/Config/SystemConfigInterface.php | 4 +- src/Test/Unit/Config/EnvironmentTest.php | 27 +++- src/Test/Unit/Config/SchemaTest.php | 23 ++++ src/Test/Unit/Config/Stage/DeployTest.php | 8 ++ src/Test/Unit/Config/System/VariablesTest.php | 128 ++++++++++++++++++ 9 files changed, 282 insertions(+), 13 deletions(-) create mode 100644 src/Test/Unit/Config/System/VariablesTest.php diff --git a/dist/.magento.env.yaml b/dist/.magento.env.yaml index 7437a53deb..307aac39ca 100644 --- a/dist/.magento.env.yaml +++ b/dist/.magento.env.yaml @@ -447,3 +447,57 @@ # port: # # chunk_size: 1024 # ####################################################################################################################### +# ENV_RELATIONSHIPS - Environment variable used to get services relationships. # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_RELATIONSHIPS" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_RELATIONSHIPS: "MAGENTO_CLOUD_RELATIONSHIPS" # +####################################################################################################################### +# ENV_ROUTES - Environment variable used to get routes. # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_ROUTES" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_ROUTES: "MAGENTO_CLOUD_ROUTES" # +####################################################################################################################### +# ENV_VARIABLES - Environment variable used to get variables. # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_VARIABLES" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_VARIABLES: "MAGENTO_CLOUD_VARIABLES" # +####################################################################################################################### +# ENV_APPLICATION - Environment variable used to get application configuration # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_APPLICATION" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_APPLICATION: "MAGENTO_CLOUD_APPLICATION" # +####################################################################################################################### +# ENV_MODE - Environment variable used to get application mode. # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_MODE" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_MODE: "MAGENTO_CLOUD_MODE" # +####################################################################################################################### +# ENV_ENVIRONMENT - Environment variable used to get application environment. # +# Magento Version: Magento 2.1.4 and later # +# Default value: - "MAGENTO_CLOUD_ENVIRONMENT" # +# Section: variables # +# Example: # +# system: # +# variables: # +# ENV_ENVIRONMENT: "MAGENTO_CLOUD_ENVIRONMENT" # +####################################################################################################################### \ No newline at end of file diff --git a/src/Config/Environment.php b/src/Config/Environment.php index 2f3f3424db..9fea9fb20a 100755 --- a/src/Config/Environment.php +++ b/src/Config/Environment.php @@ -40,11 +40,11 @@ class Environment /** * Environment constructor. + * * @param Variables $systemConfig */ - public function __construct( - Variables $systemConfig - ) { + public function __construct(Variables $systemConfig) + { $this->systemConfig = $systemConfig; } @@ -86,14 +86,26 @@ public function get(string $key, $default = null) } /** - * Get environment variable and get the name from .magento.env.yaml configuration file + * Get environment variable and get the name from .magento.env.yaml configuration file. + * * @param string $name * @param string|int|null $default * @return array|string|int|null */ - protected function getEnvVar(string $name, $default = null) + public function getEnvVar(string $name, $default = null) + { + return $this->get($this->getEnvVarName($name), $default); + } + + /** + * Get Environment Variable name from .magento.env.yaml. + * + * @param string $name + * @return string + */ + public function getEnvVarName(string $name): string { - return $this->get($this->systemConfig->get($name), $default); + return $this->systemConfig->get($name); } /** @@ -255,7 +267,8 @@ public function getCryptKey(): string */ public function isMasterBranch(): bool { - return isset($_ENV['MAGENTO_CLOUD_ENVIRONMENT']) - && preg_match(self::GIT_MASTER_BRANCH_RE, $_ENV['MAGENTO_CLOUD_ENVIRONMENT']); + $envVar = $this->systemConfig->get(SystemConfigInterface::VAR_ENV_ENVIRONMENT); + return isset($_ENV[$envVar]) + && preg_match(self::GIT_MASTER_BRANCH_RE, $_ENV[$envVar]); } } diff --git a/src/Config/Schema.php b/src/Config/Schema.php index ebcec15ac5..2c5084f627 100644 --- a/src/Config/Schema.php +++ b/src/Config/Schema.php @@ -192,6 +192,24 @@ public function getSchema() SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_APPLICATION', ], ], + SystemConfigInterface::VAR_ENV_MODE => [ + self::SCHEMA_TYPE => ['string'], + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES + ], + self::SCHEMA_DEFAULT_VALUE => [ + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_MODE', + ], + ], + SystemConfigInterface::VAR_ENV_ENVIRONMENT => [ + self::SCHEMA_TYPE => ['string'], + self::SCHEMA_SYSTEM => [ + SystemConfigInterface::SYSTEM_VARIABLES + ], + self::SCHEMA_DEFAULT_VALUE => [ + SystemConfigInterface::SYSTEM_VARIABLES => 'MAGENTO_CLOUD_ENVIRONMENT', + ], + ], StageConfigInterface::VAR_SKIP_HTML_MINIFICATION => [ self::SCHEMA_TYPE => ['boolean'], self::SCHEMA_STAGE => [ diff --git a/src/Config/Stage/Deploy.php b/src/Config/Stage/Deploy.php index eefd19c9a4..259b986e0b 100644 --- a/src/Config/Stage/Deploy.php +++ b/src/Config/Stage/Deploy.php @@ -10,6 +10,7 @@ use Magento\MagentoCloud\Config\Schema; use Magento\MagentoCloud\Config\Stage\Deploy\EnvironmentConfig; use Magento\MagentoCloud\Config\StageConfigInterface; +use Magento\MagentoCloud\Config\SystemConfigInterface; use Magento\MagentoCloud\Filesystem\FileSystemException; use Symfony\Component\Yaml\Exception\ParseException; @@ -127,8 +128,9 @@ private function mergeConfig(): array private function getDeployConfiguration(): array { $config = []; + $envVar = $this->environment->getEnvVarName(SystemConfigInterface::VAR_ENV_MODE); - if ($this->environment->getEnv('MAGENTO_CLOUD_MODE') === Environment::CLOUD_MODE_ENTERPRISE) { + if ($this->environment->getEnv($envVar) === Environment::CLOUD_MODE_ENTERPRISE) { $config[self::VAR_SCD_THREADS] = 3; } diff --git a/src/Config/SystemConfigInterface.php b/src/Config/SystemConfigInterface.php index 0b7c3ba3af..3a56565023 100644 --- a/src/Config/SystemConfigInterface.php +++ b/src/Config/SystemConfigInterface.php @@ -16,7 +16,7 @@ interface SystemConfigInterface const SECTION_SYSTEM = 'system'; /** - * System sections + * System sections. */ const SYSTEM_VARIABLES = 'variables'; @@ -27,6 +27,8 @@ interface SystemConfigInterface const VAR_ENV_ROUTES = 'ENV_ROUTES'; const VAR_ENV_VARIABLES = 'ENV_VARIABLES'; const VAR_ENV_APPLICATION = 'ENV_APPLICATION'; + const VAR_ENV_MODE = 'ENV_MODE'; + const VAR_ENV_ENVIRONMENT = 'ENV_ENVIRONMENT'; /** * Retrieves environment configuration per stage. diff --git a/src/Test/Unit/Config/EnvironmentTest.php b/src/Test/Unit/Config/EnvironmentTest.php index 60b9cb709a..3fd04be0e0 100644 --- a/src/Test/Unit/Config/EnvironmentTest.php +++ b/src/Test/Unit/Config/EnvironmentTest.php @@ -5,7 +5,10 @@ */ namespace Magento\MagentoCloud\Test\Unit\Config; +use Magento\MagentoCloud\Config\Environment\Reader as EnvironmentReader; use Magento\MagentoCloud\Config\Environment; +use Magento\MagentoCloud\Config\Schema; +use Magento\MagentoCloud\Config\SystemConfigInterface; use Magento\MagentoCloud\Config\System\Variables; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as Mock; @@ -25,10 +28,11 @@ class EnvironmentTest extends TestCase * @var array */ private $environmentData; + /** * @var Variables */ - private $variableMock; + private $variable; /** * @inheritdoc @@ -37,9 +41,26 @@ protected function setUp() { $this->environmentData = $_ENV; - $this->variableMock = $this->createMock(Variables::class); + $this->environmentReaderMock = $this->createMock(EnvironmentReader::class); + $this->schemaMock = $this->createMock(Schema::class); + $this->schemaMock->expects($this->any()) + ->method('getDefaults') + ->with(SystemConfigInterface::SYSTEM_VARIABLES) + ->willReturn([ + SystemConfigInterface::VAR_ENV_RELATIONSHIPS => 'MAGENTO_CLOUD_RELATIONSHIPS', + SystemConfigInterface::VAR_ENV_ROUTES => 'MAGENTO_CLOUD_ROUTES', + SystemConfigInterface::VAR_ENV_VARIABLES => 'MAGENTO_CLOUD_VARIABLES', + SystemConfigInterface::VAR_ENV_APPLICATION => 'MAGENTO_CLOUD_APPLICATION', + SystemConfigInterface::VAR_ENV_MODE => 'MAGENTO_CLOUD_MODE', + SystemConfigInterface::VAR_ENV_ENVIRONMENT => 'MAGENTO_CLOUD_ENVIRONMENT', + ]); + + $this->variable = new Variables( + $this->environmentReaderMock, + $this->schemaMock + ); - $this->environment = new Environment($this->variableMock); + $this->environment = new Environment($this->variable); } /** diff --git a/src/Test/Unit/Config/SchemaTest.php b/src/Test/Unit/Config/SchemaTest.php index 85f842e312..21e6b183c2 100644 --- a/src/Test/Unit/Config/SchemaTest.php +++ b/src/Test/Unit/Config/SchemaTest.php @@ -10,6 +10,7 @@ use Magento\MagentoCloud\Config\Stage\DeployInterface; use Magento\MagentoCloud\Config\Stage\PostDeployInterface; use Magento\MagentoCloud\Config\StageConfigInterface; +use Magento\MagentoCloud\Config\SystemConfigInterface; use PHPUnit\Framework\TestCase; /** @@ -87,6 +88,22 @@ public function testGetDefaultsForPostDeploy() ); } + + public function testGetDefaultsForSystemVariables() + { + $this->assertEquals( + [ + SystemConfigInterface::VAR_ENV_RELATIONSHIPS => 'MAGENTO_CLOUD_RELATIONSHIPS', + SystemConfigInterface::VAR_ENV_ROUTES => 'MAGENTO_CLOUD_ROUTES', + SystemConfigInterface::VAR_ENV_VARIABLES => 'MAGENTO_CLOUD_VARIABLES', + SystemConfigInterface::VAR_ENV_APPLICATION => 'MAGENTO_CLOUD_APPLICATION', + SystemConfigInterface::VAR_ENV_MODE => 'MAGENTO_CLOUD_MODE', + SystemConfigInterface::VAR_ENV_ENVIRONMENT => 'MAGENTO_CLOUD_ENVIRONMENT', + ], + $this->schema->getDefaults(SystemConfigInterface::SYSTEM_VARIABLES) + ); + } + public function testGetDefaultsForGlobalSection() { $this->assertEquals( @@ -128,6 +145,12 @@ public function testGetSchemaItemsExists() DeployInterface::VAR_MYSQL_USE_SLAVE_CONNECTION, DeployInterface::VAR_GENERATED_CODE_SYMLINK, PostDeployInterface::VAR_WARM_UP_PAGES, + SystemConfigInterface::VAR_ENV_RELATIONSHIPS, + SystemConfigInterface::VAR_ENV_ROUTES, + SystemConfigInterface::VAR_ENV_VARIABLES, + SystemConfigInterface::VAR_ENV_APPLICATION, + SystemConfigInterface::VAR_ENV_MODE, + SystemConfigInterface::VAR_ENV_ENVIRONMENT ]; foreach ($requiredItems as $item) { diff --git a/src/Test/Unit/Config/Stage/DeployTest.php b/src/Test/Unit/Config/Stage/DeployTest.php index f45e57e042..739960acd1 100644 --- a/src/Test/Unit/Config/Stage/DeployTest.php +++ b/src/Test/Unit/Config/Stage/DeployTest.php @@ -12,6 +12,7 @@ use Magento\MagentoCloud\Config\Stage\Deploy\EnvironmentConfig; use Magento\MagentoCloud\Config\Stage\DeployInterface; use Magento\MagentoCloud\Config\StageConfigInterface; +use Magento\MagentoCloud\Config\SystemConfigInterface; use Magento\MagentoCloud\Filesystem\FileSystemException; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as Mock; @@ -52,8 +53,15 @@ class DeployTest extends TestCase protected function setUp() { $this->environmentMock = $this->createMock(Environment::class); + $this->environmentMock->expects($this->any()) + ->method('getEnvVarName') + ->with(SystemConfigInterface::VAR_ENV_MODE) + ->willReturn('MAGENTO_CLOUD_MODE'); + $this->environmentReaderMock = $this->createMock(EnvironmentReader::class); + $this->environmentConfigMock = $this->createMock(EnvironmentConfig::class); + $this->schemaMock = $this->createMock(Schema::class); $this->schemaMock->expects($this->any()) ->method('getDefaults') diff --git a/src/Test/Unit/Config/System/VariablesTest.php b/src/Test/Unit/Config/System/VariablesTest.php new file mode 100644 index 0000000000..5a22718f2f --- /dev/null +++ b/src/Test/Unit/Config/System/VariablesTest.php @@ -0,0 +1,128 @@ +environmentReaderMock = $this->createMock(EnvironmentReader::class); + $this->schemaMock = $this->createMock(Schema::class); + $this->schemaMock->expects($this->any()) + ->method('getDefaults') + ->with(SystemConfigInterface::SYSTEM_VARIABLES) + ->willReturn([ + SystemConfigInterface::VAR_ENV_RELATIONSHIPS => 'MAGENTO_CLOUD_RELATIONSHIPS', + SystemConfigInterface::VAR_ENV_ROUTES => 'MAGENTO_CLOUD_ROUTES', + SystemConfigInterface::VAR_ENV_VARIABLES => 'MAGENTO_CLOUD_VARIABLES', + SystemConfigInterface::VAR_ENV_APPLICATION => 'MAGENTO_CLOUD_APPLICATION', + SystemConfigInterface::VAR_ENV_MODE => 'MAGENTO_CLOUD_MODE', + SystemConfigInterface::VAR_ENV_ENVIRONMENT => 'MAGENTO_CLOUD_ENVIRONMENT', + ]); + + $this->config = new Variables( + $this->environmentReaderMock, + $this->schemaMock + ); + } + + /** + * @param string $name + * @param array $envConfig + * @param array $buildConfig + * @param mixed $expectedValue + * @dataProvider getDataProvider + */ + public function testGet(string $name, array $envConfig, $expectedValue) + { + $this->environmentReaderMock->expects($this->once()) + ->method('read') + ->willReturn([SystemConfigInterface::SYSTEM_VARIABLES => $envConfig]); + + $this->assertSame($expectedValue, $this->config->get($name)); + } + + /** + * @return array + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function getDataProvider(): array + { + return [ + 'default relationships' => [ + SystemConfigInterface::VAR_ENV_RELATIONSHIPS, + [], + 'MAGENTO_CLOUD_RELATIONSHIPS', + ], + 'default routes' => [ + SystemConfigInterface::VAR_ENV_ROUTES, + [], + 'MAGENTO_CLOUD_ROUTES', + ], + 'default variables' => [ + SystemConfigInterface::VAR_ENV_VARIABLES, + [], + 'MAGENTO_CLOUD_VARIABLES', + ], + 'default application' => [ + SystemConfigInterface::VAR_ENV_APPLICATION, + [], + 'MAGENTO_CLOUD_APPLICATION', + ], + 'default mode' => [ + SystemConfigInterface::VAR_ENV_MODE, + [], + 'MAGENTO_CLOUD_MODE', + ], + 'default environment' => [ + SystemConfigInterface::VAR_ENV_ENVIRONMENT, + [], + 'MAGENTO_CLOUD_ENVIRONMENT', + ], + ]; + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Config NOT_EXISTS_VALUE was not defined. + */ + public function testNotExists() + { + $this->environmentReaderMock->expects($this->any()) + ->method('read') + ->willReturn([]); + + $this->config->get('NOT_EXISTS_VALUE'); + } +} From 387e543770de664b035016a6ef2679df4b8c62b3 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Thu, 13 Dec 2018 11:40:17 +0100 Subject: [PATCH 11/12] Fixed double newline --- src/Test/Unit/Config/SchemaTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Test/Unit/Config/SchemaTest.php b/src/Test/Unit/Config/SchemaTest.php index 21e6b183c2..0ea114892b 100644 --- a/src/Test/Unit/Config/SchemaTest.php +++ b/src/Test/Unit/Config/SchemaTest.php @@ -88,7 +88,6 @@ public function testGetDefaultsForPostDeploy() ); } - public function testGetDefaultsForSystemVariables() { $this->assertEquals( From d2f32d3aefd0efd80aae29d4d708783526aee907 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Thu, 13 Dec 2018 19:51:03 +0100 Subject: [PATCH 12/12] Updated doc block comments --- src/Config/Stage/Deploy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Config/Stage/Deploy.php b/src/Config/Stage/Deploy.php index 259b986e0b..59da65baa5 100644 --- a/src/Config/Stage/Deploy.php +++ b/src/Config/Stage/Deploy.php @@ -35,7 +35,7 @@ class Deploy implements DeployInterface private $mergedConfig; /** - * @var Variables + * @var Environment */ private $environment; @@ -45,7 +45,7 @@ class Deploy implements DeployInterface private $schema; /** - * @param Variables $environment + * @param Environment $environment * @param EnvironmentReader $environmentReader * @param EnvironmentConfig $environmentConfig * @param Schema $schema @@ -130,7 +130,7 @@ private function getDeployConfiguration(): array $config = []; $envVar = $this->environment->getEnvVarName(SystemConfigInterface::VAR_ENV_MODE); - if ($this->environment->getEnv($envVar) === Environment::CLOUD_MODE_ENTERPRISE) { + if ($this->environment->getEnv($envVar) === Environment::CLOUD_MODE_ENTERPRISE) { $config[self::VAR_SCD_THREADS] = 3; }