Use JSON instead of .env
file to set environment variables.
Validation by JSON schema is performed on environment variables as well as JSON.
composer require koriym/env-json
Specify the directory of the env.schema.json
schema file to load()
.
(new EnvJson())->load($dir);
- If environment variables are already set, they are validated by
env.schema.json
to see if they are correct. - If not,
env.json
orenv.dist.json
is read, validated byenv.schema.json
, and exported as the environment variables.
$dir/env.json
or $dir/env.dist.json
{
"$schema": "./env.schema.json",
"FOO": "foo1",
"BAR": "bar1"
}
$dir/env.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"FOO", "BAR"
],
"properties": {
"FOO": {
"description": "Foo's value",
"minLength": 3
},
"BAR": {
"description": "Bar's value",
"enum": ["bar1", "bar2"]
}
}
}
It can provide more appropriate documentation and constraints compared to .env
files.
JSON and its schema file are generated from the .env
file with ini2json
.
. /vendor/bin/ini2json .env