You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, setting an environment variable value in .env to true or false string literals and fetching that using Craft::parseEnv() does not return the boolean equivalent value, as might be expected by many developers. Instead the string value 'true' or 'false' is returned, so boolean comparisons on the parsed environment variable always equate to true.
Some other popular frameworks natively implement this string to boolean conversion functionality in their environment parsing methods. For example, Laravel's environment parsing method does the following:
/**
* 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 (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
return substr($value, 1, -1);
}
return $value;
}
It would be great if Craft::parseEnv() could be made more intelligent to handle boolean type environment variables and alias values.
FYI: There is an interesting discussion about the support of boolean environment variables on the official phpdotenv github.
Steps to reproduce
Define an environment variable in .env with a false string value (e.g. TEST_MODE=false)
Description
Currently, setting an environment variable value in
.env
totrue
orfalse
string literals and fetching that usingCraft::parseEnv()
does not return the boolean equivalent value, as might be expected by many developers. Instead the string value'true'
or'false'
is returned, so boolean comparisons on the parsed environment variable always equate totrue
.Some other popular frameworks natively implement this string to boolean conversion functionality in their environment parsing methods. For example, Laravel's environment parsing method does the following:
It would be great if
Craft::parseEnv()
could be made more intelligent to handleboolean
type environment variables and alias values.FYI: There is an interesting discussion about the support of boolean environment variables on the official phpdotenv github.
Steps to reproduce
.env
with afalse
string value (e.g.TEST_MODE=false
)$isTestMode = Craft::parseEnv('TEST_MODE')
echo ($isTestMode ? 'In Test Mode' : 'Not In Test Mode');
outputsIn Test Mode
instead ofNot In Test Mode
.Additional info
The text was updated successfully, but these errors were encountered: