Skip to content

Instantly share code, notes, and snippets.

@jclecas
Last active March 22, 2018 16:06
Show Gist options
  • Save jclecas/096665b695f8275b797fdff2532342b4 to your computer and use it in GitHub Desktop.
Save jclecas/096665b695f8275b797fdff2532342b4 to your computer and use it in GitHub Desktop.
Magento 2 // Get Config Value
<?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