Skip to content

Making possible to define configuration by environment #12361

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/internal/Magento/Framework/App/DeploymentConfig/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class Reader
{
const CONFIG_ENV_MODE = 'CONFIG_ENV_MODE';

/**
* @var DirectoryList
*/
Expand All @@ -41,6 +43,11 @@ class Reader
*/
private $files;

/**
* @var string
*/
private $configEnvMode;

/**
* Constructor
*
Expand Down Expand Up @@ -92,6 +99,11 @@ public function getFiles()
*/
public function load($fileKey = null)
{
$configEnvMode = $this->getConfigEnvMode();
if ($configEnvMode) {
putenv(self::CONFIG_ENV_MODE . "=" . $configEnvMode);
}

$path = $this->dirList->getPath(DirectoryList::CONFIG);
$fileDriver = $this->driverPool->getDriver(DriverPool::FILE);
$result = [];
Expand Down Expand Up @@ -120,6 +132,27 @@ public function load($fileKey = null)
return $result ?: [];
}

/**
* Get CONFIG_ENV_MODE from env.php file configuration
*
* @return string
*/
private function getConfigEnvMode() : string
{
if (!isset($this->configEnvMode)) {
$configPath = $this->dirList->getPath(DirectoryList::CONFIG);
$fileDriver = $this->driverPool->getDriver(DriverPool::FILE);
$envFile = $configPath . '/' . $this->configFilePool->getPath(ConfigFilePool::APP_ENV);
if ($fileDriver->isExists($envFile)) {
$config = include $envFile;
$this->configEnvMode = $config[self::CONFIG_ENV_MODE] ?? "";
} else {
$this->configEnvMode = "";
}
}
return $this->configEnvMode;
}

/**
* Loads the configuration file.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/Magento/Framework/Config/File/ConfigFilePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class ConfigFilePool
{
const APP_CONFIG = 'app_config';
const APP_CONFIG_ENV = 'app_config_env';
const APP_ENV = 'app_env';

/**
Expand All @@ -32,6 +33,7 @@ class ConfigFilePool
*/
private $applicationConfigFiles = [
self::APP_CONFIG => 'config.php',
self::APP_CONFIG_ENV => 'config_env.php',
self::APP_ENV => 'env.php',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testGetPaths()
{
$expected['new_key'] = 'new_config.php';
$expected[ConfigFilePool::APP_CONFIG] = 'config.php';
$expected[ConfigFilePool::APP_CONFIG_ENV] = 'config_env.php';
$expected[ConfigFilePool::APP_ENV] = 'env.php';

$this->assertEquals($expected, $this->configFilePool->getPaths());
Expand Down