phpdotenv icon indicating copy to clipboard operation
phpdotenv copied to clipboard

Default values

Open sagikazarmark opened this issue 11 years ago • 14 comments

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.

sagikazarmark avatar Aug 08 '15 13:08 sagikazarmark

You can achieve default values with ?: operator.

$foo = getenv("FOO") ?: "BAR";

tuupola avatar Aug 09 '15 18:08 tuupola

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.

sagikazarmark avatar Aug 09 '15 20:08 sagikazarmark

Yeh, that's not really a proper solution to the problem here.

GrahamCampbell avatar Aug 09 '15 21:08 GrahamCampbell

BTW what I want would look like something like this:

if (!getenv('FOO')) {
    putenv('FOO=bar');
}

sagikazarmark avatar Aug 09 '15 21:08 sagikazarmark

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

hugochinchilla avatar Sep 04 '15 07:09 hugochinchilla

@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.

matijaboban avatar Feb 21 '16 19:02 matijaboban

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.

sagikazarmark avatar Feb 21 '16 19:02 sagikazarmark

:+1: default values in bootstrap time will good I think.

erayaydin avatar Feb 24 '16 22:02 erayaydin

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.

bittner avatar Jun 22 '17 08:06 bittner

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;
}

tecjam avatar Oct 31 '17 16:10 tecjam

There's a PR for this feature already open: #229 — maybe this issue should be closed, and discussion continued over there?

samwilson avatar Oct 31 '17 23:10 samwilson

That PR has been open since April alongside PR dating back to October 2016.

Is this project dead? Sure looks like it.

tecjam avatar Nov 27 '17 09:11 tecjam

It seems it might be. :-(

samwilson avatar Nov 27 '17 09:11 samwilson

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.

GrahamCampbell avatar Jul 01 '18 10:07 GrahamCampbell