andriichuk/keepenv 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

andriichuk/keepenv

Composer 安装命令:

composer require andriichuk/keepenv

包简介

KeepEnv is a tool for checking and managing environment variables based on a specification file.

README 文档

README

SWUbanner

Track Your Environment Variable Changes Using Specification

Logo

License: MIT Tests Code coverage Psalm type coverage Psalm level Stars Quality Gate Status

Table Of Contents

About

KeepEnv is a CLI tool for checking and managing environment variables based on a specification file.

Motivations:

  • I want to have a way to describe all environment variables in one specification file.
  • I want to make sure that all required variables are filled in correctly before deploying the application.
  • I don't want to check variables in runtime.
  • I want to keep track of new environment variables when they are added by one of my colleagues.
  • I want to have a convenient and safe way to fill in new variables.
  • I want to check variables from different state providers (system $_ENV, from .env file + system or only from .env file).
  • I don't want to manually describe all 100+ existing environment variables.
  • I want to use a tool that will not be tied to a specific framework, because I work with several frameworks.

Features:

  • Environment specification generation based on current .env files.
  • Environment variables validation.
  • Split variable definition between environments.
  • Extend variables from particular environment e.g. local from common.
  • Split system ($_ENV) and regular variables from .env files.
  • Ability to fill missing variables through console command.

Supported dotenv file state loaders:

Installation

Install composer package:

composer require andriichuk/keepenv

Initialization

This command allows you to generate a new environment specification file based on your current .env structure.

Basic usage:

./vendor/bin/keepenv init

This will create a specification file (keepenv.yaml) in your root directory with common environment.

Using preset (available presets: laravel, symfony):

./vendor/bin/keepenv init --preset=laravel

For Laravel Sail:

./vendor/bin/sail php ./vendor/bin/keepenv init --preset=laravel

Using custom .env files for vlucas/dotenv (paths to the folders with .env file):

./vendor/bin/keepenv init --env-file=./ --env-file=./config/

Using custom .env files for symfony/dotenv (direct file paths):

./vendor/bin/keepenv init --env-file=./.env --env-file=./.env.local

Environment file reader will be detected automatically, but you can customize it:

./vendor/bin/keepenv init --env-reader=symfony/dotenv --env-file=./.env

Validation

Using this command you can check your environment variables according to the specification file keepenv.yaml.

Basic usage:

./vendor/bin/keepenv validate common

Check only system variables ($_ENV) without looking at the .env file:

./vendor/bin/keepenv validate common --env-provider=system

Use --help option to check other parameters.

Filling

This command allows you to fill in and validate missing variable values from your .env file (use --help for list of all options).

Command:

./vendor/bin/keepenv fill

For specific environment:

./vendor/bin/keepenv fill --env=common

Adding

The following command can help you to add a new variable definition to specification and dotenv files:

./vendor/bin/keepenv add

Dumping

Using this command you can export all your variables defined in keepenv.yaml file into the custom .env file.

Create a new .env file according to variables defined in the keepenv.yaml (same as cp .env.example .env). Variables will be filled in only with default values. Perhaps now you can delete the .env.example file:

./vendor/bin/keepenv dump

Dump system variables into the file:

./vendor/bin/keepenv dump --target-env-file=./.env.system --env-provider=system --with-values=true

Create a new .env.stage file based on production environment specification and current .env file:

./vendor/bin/keepenv dump --env=production --target-env-file=./.env.stage --env-file=./ --with-values=true

Syntax

Currently, only the YAML syntax format is supported.

Environments definition:

version: '1.0'
environments:
    common:
        variables:
        # ...
    local:
        extends: common
        variables:
        # ...
    testing:
        variables:
        # ...

Variables definition:

  • Describe the purpose of variables:
SESSION_LIFETIME:
    description: 'Session lifetime in minutes.'
  • Mark that variable should be followed by export keyword in .env file (export APP_LOCALE=en):
APP_LOCALE:
    export: true
  • Mark that variable should be set on the server-side ($_ENV or $_SERVER) not from .env file:
APP_TIMEZONE:
    system: true
  • Specify default value (please use this only for non-sensitive data):
REDIS_PORT:
    default: 6379
  • Describe validation rules:
    • Mark variable as required:
    APP_ENV:
        rules:
            required: true
    • Check that variable value is a string (can usually be omitted because all values in the .env file are read as strings by default):
    APP_ENV:
        rules:
            string: true
    • String with length range
    APP_KEY:
        rules:
            string:
                min: 32
                max: 60
    • Numeric
    REDIS_PORT:
        rules:
            numeric: true
    • Boolean (true/false, on/off, yes/no, 1/0)
    APP_DEBUG:
        rules:
            boolean: true
    • Boolean with custom options
    PAYMENT_FEATURE:
        rules:
            boolean:
                'true': Y
                'false': N
    • Email address
    MAIL_FROM_ADDRESS:
        rules:
            email: true
    • Enumeration:
    APP_ENV:
        rules:
            enum:
                - local
                - production
    • Means that the value of the variable must be equal (==) to a specific value.
    APP_ENV:
        rules:
            equals: local
    • IP address
    DB_HOST:
        rules:
            ip: true

Full example:

version: '1.0'
environments:
    common:
        variables:
            APP_NAME:
                description: 'Application name.'
            APP_ENV:
                description: 'Application environment name.'
                default: local
                rules:
                    required: true
                    enum:
                        - local
                        - production
            APP_DEBUG:
                rules:
                    boolean: true
            DB_HOST:
                description: 'Database host.'
                default: 127.0.0.1
                rules:
                    required: true
                    ip: true
            DB_PORT:
                description: 'Database port.'
                default: 3306
                rules:
                    required: true
                    numeric: true

    local:
        extends: common
        variables:
            APP_ENV:
                rules:
                    equals: local

    testing:
        variables:
            DB_DATABASE:
                description: 'Database name.'
                default: testing
                rules:
                    required: true
            DB_USERNAME:
                description: 'Database username.'
                rules:
                    required: true
            DB_PASSWORD:
                description: 'Database password.'
                rules:
                    required: true

Tips

Use equals rule to check for a specific value for the environment, e.g., a useful example for APP_ENV:

version: '1.0'
environments:
    common:
        variables:
            APP_ENV:
                rules:
                    required: true
                    enum:
                        - local
                        - production
            # ...
    production:
        extends: common
        variables:
            APP_ENV:
                rules:
                    equals: production

You can add a composer script for the new environment variables filling and validation:

"scripts": {
    "keepenv": "./vendor/bin/keepenv fill && ./vendor/bin/keepenv validate",
},

Then use:

composer keepenv common

You can also define keepenv common on post-update-cmd composer event, so environment filling and validation will be running after each composer update:

"scripts": {
    "post-update-cmd": [
        "@keepenv common"
    ]
},

Contributing

Contributions, issues and feature requests are welcome.
Feel free to check issues page if you want to contribute. Check the contributing guide.

Credits

License

Copyright © 2022 Serhii Andriichuk.
This project is MIT licensed.

andriichuk/keepenv 适用场景与选型建议

andriichuk/keepenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「env」 「dotenv」 「env-spec」 「env-manager」 「env-validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 andriichuk/keepenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andriichuk/keepenv 我们能提供哪些服务?
定制开发 / 二次开发

基于 andriichuk/keepenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-26