定制 yammerjp/envgen 二次开发

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

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

yammerjp/envgen

最新稳定版本:v0.1.0

Composer 安装命令:

composer require yammerjp/envgen

包简介

Environment variable loader generator for PHP

README 文档

README

Environment variable loader generator for PHP.

Requirements

  • PHP 7.4 or higher

Installation

Install via Composer:

composer require --dev yammerjp/envgen

Usage

Create a YAML configuration file (e.g., envgen.yaml):

version: "1.0"
namespace: "App"
outputPath: "src/EnvironmentVariables.php"
className: "EnvironmentVariables"
variables:
  ENVIRONMENT:
    type: string
    required: true
    define: false
  DEBUG_MODE:
    type: bool
    required: false
    define: false
  API_PORT:
    type: int
    required: true
    define: false

Run the generator:

vendor/bin/envgen envgen.yaml

This will generate a PHP class that loads environment variables with type validation.

Using Generated Code

The generated class provides a static load() method that creates an instance with environment variables:

<?php
require 'vendor/autoload.php';

use App\EnvironmentVariables;

$env = EnvironmentVariables::load();
echo $env->getEnvironment(); // string
echo $env->getApiPort();     // int
var_dump($env->getDebugMode()); // bool or null

Testing with Custom Environment Variables

For testing purposes, the load() method accepts an optional array parameter:

<?php
// For testing - pass custom environment variables
$config = Config::load([
    'VAR_ONE' => 'test-value',
    'VAR_TWO' => '42',
]);

// In production - uses getenv() by default
$config = Config::load();

Configuration Options

Global Options

  • version: Configuration file version (currently supports "1.0" or "1")
  • namespace: PHP namespace for the generated class
  • outputPath: Path where the PHP file will be generated
  • className: Name of the generated class

Variable Options

  • type: Variable type (string, int, or bool)
  • required: Whether the variable is required (true/false)
  • define: Whether to define a constant for the variable (true/false)
  • one_of: (optional) Array of allowed values

Development

This project uses Docker for development with PHP 7.4.

Setup

Build the Docker image:

docker-compose build

Install dependencies:

docker-compose run --rm php composer install

Running Tests

Run all tests with PHPUnit:

docker-compose run --rm php vendor/bin/phpunit

Or using composer:

docker-compose run --rm php composer test

Running the Generator

You can test the generator using the example configuration:

docker-compose run --rm php bin/envgen example/envgen.yaml

License

MIT

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-30