2 namespace Consolidation\Config\Util;
4 use Consolidation\Config\Config;
5 use Consolidation\Config\ConfigInterface;
8 * Provide a configuration object that fetches values from environment
11 class EnvConfig implements ConfigInterface
17 * EnvConfig constructor
19 * @param $prefix The string to appear before every environment
20 * variable key. For example, if the prefix is 'MYAPP_', then
21 * the key 'foo.bar' will be fetched from the environment variable
24 public function __construct($prefix)
26 // Ensure that the prefix is always uppercase, and always
27 // ends with a '_', regardless of the form the caller provided.
28 $this->prefix = strtoupper(rtrim($prefix, '_')) . '_';
34 public function has($key)
36 return $this->get($key) !== null;
42 public function get($key, $defaultFallback = null)
44 $envKey = $this->prefix . strtoupper(strtr($key, '.-', '__'));
45 $envKey = str_replace($this->prefix . $this->prefix, $this->prefix, $envKey);
46 return getenv($envKey) ?: $defaultFallback;
52 public function set($key, $value)
54 throw new \Exception('Cannot call "set" on environmental configuration.');
60 public function import($data)
68 public function export()
76 public function hasDefault($key)
84 public function getDefault($key, $defaultFallback = null)
86 return $defaultFallback;
92 public function setDefault($key, $value)
94 throw new \Exception('Cannot call "setDefault" on environmental configuration.');