定制 koriym/env-json 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

koriym/env-json

最新稳定版本:1.0.1

Composer 安装命令:

composer require koriym/env-json

包简介

Type-safe, schema-driven alternative to .env files with JSON Schema validation

README 文档

README

Continuous Integration codecov Type Coverage

A modern approach to environment variables using JSON instead of .env files, with built-in validation via JSON Schema.

env.json logo

Installation

composer require koriym/env-json

Basic Usage

// Load and validate environment variables
$env = (new EnvJson())->load(__DIR__);

// Access variables
echo $env->DATABASE_URL;
echo getenv('DATABASE_URL');

Configuration Files

env.schema.json

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["DATABASE_URL", "API_KEY"],
    "properties": {
        "DATABASE_URL": {
            "description": "Database connection string",
            "pattern": "^mysql://.*"
        },
        "API_KEY": {
            "description": "API authentication key",
            "minLength": 32
        },
        "DEBUG_MODE": {
            "description": "Enable debug output",
            "enum": ["true", "false"],
            "default": "false"
        }
    }
}

env.json

{
    "$schema": "./env.schema.json",
    "DATABASE_URL": "mysql://user:pass@localhost/mydb",
    "API_KEY": "1234567890abcdef1234567890abcdef",
    "DEBUG_MODE": "true"
}

⚠️ Important: Environment variables are always strings

Do not use "type": "boolean" or "type": "integer" in your schema. Use "enum": ["true", "false"] for booleans and "pattern": "^[0-9]+$" for numbers.

Converting from .env

bin/ini2json .env

This generates both env.schema.json and env.json files.

CLI Tool

# Load variables into current shell
source <(bin/envjson)

# Specify custom directory
source <(bin/envjson -d ./config)

# Output formats
bin/envjson --output=shell   # export FOO="bar"
bin/envjson --output=fpm     # env[FOO] = "bar"
bin/envjson --output=ini     # FOO="bar"

Try the Demo

# Run all demos
php demo/run.php

# Or run individual demos
php demo/env-json-1/run.php     # Basic usage
php demo/convert/run.php         # Convert .env to JSON
php demo/error-handling/run.php  # Error handling examples
php demo/validation/run.php      # Schema validation examples
php demo/envjson-cli/run.php     # CLI tool usage examples

Links

Configuration deserves more than plaintext. Structure it. Validate it. Understand it—with env.json!

统计信息

  • 总下载量: 170.79k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-25