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
A modern approach to environment variables using JSON instead of .env files, with built-in validation via JSON Schema.
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.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04