Default values
I think it would be a great feature if we could add default values to dotenv. This way if an environment variable is not set, dotenv could set a default value. In some cases it would be useful instead of requiring a specific variable to be set. For example: application environment: dev by default, can be staging, production.
You can achieve default values with ?: operator.
$foo = getenv("FOO") ?: "BAR";
Yeah, that's obvious.......and ugly. Also. default values are set when the environment is loaded. You cannot do it inside your application or at least it is ugly again.
Yeh, that's not really a proper solution to the problem here.
BTW what I want would look like something like this:
if (!getenv('FOO')) {
putenv('FOO=bar');
}
The best way to achieve this would be, in my opinion, to use the bash standard for this, so we can keep the behavior when sourcing the dotenv file from the shell.
hchinchilla@bowser ~/projects/dockerlib$ echo $foo
hchinchilla@bowser ~/projects/dockerlib$ echo ${foo:-my_default_value}
my_default_value
@sagikazarmark , I would sugest the Laravel implementation syntax in this case. Something like:
getenv('APP_ENV', 'production')
And I fully agree, this would be a great addition.
That's the same as @tuupola's solution and the problem is the same: I want to set defaults at bootstrap time, not in the middle of my application. Environment is not something dynamic, it should be fixed, even if it is a default. And the correct place for it is at bootstrap time.
:+1: default values in bootstrap time will good I think.
I second @mattboban's suggestion, because other frameworks do it like this, and I think it's a good idea to harmonize approaches - and best practices - across frameworks.
Also Python does it like Laravel. Add a second parameter to retrieve a default value in case the value is not set. Without default value the call can fail with an exception (good feedback). Specify a default value to allow robust, self-healing, retrieval.
As a workaround I used the following functions to create my own version of the env() function, which allows a default value as fallback.
/**
* Check an env. var is set, else return default value
*
* @param $param
* @param $value
* @return mixed
*/
function getEnvValue($param, $value)
{
return env($param, $value);
}
/**
* Gets the value of an environment variable. Supports boolean, empty and null.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
if (startsWith($value, '"') && endsWith($value, '"')) {
return substr($value, 1, -1);
}
return $value;
}
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function startsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
return true;
}
}
return false;
}
/**
* Determine if a given string ends with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function endsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if (substr($haystack, -strlen($needle)) === (string) $needle) {
return true;
}
}
return false;
}
There's a PR for this feature already open: #229 — maybe this issue should be closed, and discussion continued over there?
That PR has been open since April alongside PR dating back to October 2016.
Is this project dead? Sure looks like it.
It seems it might be. :-(
I don't think I want to include this in the library core, in the interest of keeping things light. Particularly, since the recommended usage of this library would be to read env variables into config one-time (such as in Laravel), and so defaults can be set at that point.