koriym/env-json

0.1.0 2022-12-05 06:26 UTC

This package is auto-updated.

Last update: 2024-03-27 01:32:49 UTC


README

Japanese

Use JSON instead of .env file to set environment variables. Validation by JSON schema is performed on environment variables as well as JSON.

Installation

composer require koriym/env-json

Usage

Specify the directory of the env.schema.json schema file to load().

(new EnvJson())->load($dir);
  1. If environment variables are already set, they are validated by env.schema.json to see if they are correct.
  2. If not, env.json or env.dist.json is read, validated by env.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.

Convert ini file

JSON and its schema file are generated from the .env file with ini2json.

. /vendor/bin/ini2json .env