Last active
March 22, 2018 16:06
-
-
Save jclecas/096665b695f8275b797fdff2532342b4 to your computer and use it in GitHub Desktop.
Magento 2 // Get Config Value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Magento\Framework\App\DeploymentConfig; | |
use Psr\Log\LoggerInterface; | |
class Config | |
{ | |
const MAGENTO_CLOUD_VARIABLES_KEY = 'MAGENTO_CLOUD_VARIABLES'; | |
const DEPLOYMENT_CONFIG_VARIABLES_KEY = 'caudalie'; | |
/** @var DeploymentConfig */ | |
private $deploymentConfig; | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
/** | |
* Config constructor. | |
* | |
* @param DeploymentConfig $deploymentConfig | |
* @param LoggerInterface $logger | |
*/ | |
public function __construct(DeploymentConfig $deploymentConfig, LoggerInterface $logger) | |
{ | |
$this->deploymentConfig = $deploymentConfig; | |
$this->logger = $logger; | |
} | |
/** | |
* Get environment variable | |
* Check Cloud environment first then local then config.php | |
* | |
* @param $key | |
* @return bool|mixed | |
*/ | |
public function getEnvironmentVariable($key) | |
{ | |
try { | |
if (getenv($key)) { | |
return getenv($key); | |
} | |
// DEPLOYMENT config ? | |
$variables = $this->deploymentConfig->get(self::DEPLOYMENT_CONFIG_VARIABLES_KEY, false); | |
if ($variables && is_array($variables) && array_key_exists($key, $variables)) { | |
return $variables[$key]; | |
} | |
$this->logger->error("Environment variable [ $key ] not found!"); | |
} catch (\Exception $e) { | |
$this->logger->error("Error while getting the environment variable [$key]", array('error' => $e)); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment