open-laravel/env-guardian
Composer 安装命令:
composer require open-laravel/env-guardian
包简介
Keep your Laravel .env.example in sync with .env - safely and non-destructively
README 文档
README
Env Guardian helps Laravel teams keep .env.example in sync with .env files - safely and non-destructively.
Features
✅ Safe & Non-Destructive - Never removes or overwrites existing keys
✅ Git Hook Integration - Non-blocking pre-commit warnings
✅ Multiple Commands - Sync, diff, check, discover env keys
✅ CI/CD Ready - Perfect for automated checks
✅ Laravel 9+ - Supports Laravel 9, 10, 11, and 12
✅ PHP 8.1+ - Modern PHP support
Installation
You can install the package via composer:
composer require open-laravel/env-guardian
Publish the config file:
php artisan vendor:publish --tag=env-guardian-config
This will create a config/env-guardian.php file with the following options:
return [ 'required_keys' => [], // Keys that must be present in .env 'mask_values' => true, // Whether to mask values in .env.example 'mask_with' => '', // Mask value (empty or 'changeme') 'grouped' => false, // Group keys by prefix 'ci_fail_on_error' => true, // Fail in CI mode if required keys missing ];
Usage
1. Sync Missing Keys
Sync keys from .env to .env.example:
php artisan env:sync-example
Options:
--dry-run- Preview changes without writing--sort- Sort keys alphabetically--grouped- Group keys with comment headers--mask-value=- Custom mask value (default: empty)
Example:
# Preview changes php artisan env:sync-example --dry-run # Sync and sort keys php artisan env:sync-example --sort # Sync with grouped output php artisan env:sync-example --grouped # Use custom mask value php artisan env:sync-example --mask-value=changeme
2. Compare Env Files
Compare .env and .env.example to see differences:
php artisan env:diff
Options:
--quiet- Minimal output for CI/scripting
Example:
# Show full diff php artisan env:diff # Quiet mode for scripts php artisan env:diff --quiet
3. Check Required Keys
Check if required environment keys are present:
php artisan env:check
Options:
--ci- Exit with non-zero code if keys are missing (for CI/CD)
Example:
# Check required keys php artisan env:check # CI mode php artisan env:check --ci
Configure required keys in config/env-guardian.php:
'required_keys' => [ 'APP_KEY', 'APP_URL', 'DB_CONNECTION', 'DB_HOST', 'DB_DATABASE', ],
4. Install Git Hook
Install a non-blocking pre-commit hook:
php artisan env:install-hook
Options:
--force- Overwrite existing hook--uninstall- Remove the hook
The hook will:
- ✅ Warn about missing keys in
.env.example - ✅ NOT block commits (always exits 0)
- ✅ Suggest running
env:sync-example
Example:
# Install hook php artisan env:install-hook # Force overwrite existing hook php artisan env:install-hook --force # Uninstall hook php artisan env:install-hook --uninstall
5. Discover Env Usage
Scan your code for env() usage:
php artisan env:discover
Options:
--write- Write discovered keys to.env.example--mask-value=- Custom mask value
This command scans:
config/directoryapp/directoryroutes/directory
Example:
# Discover keys php artisan env:discover # Discover and write to .env.example php artisan env:discover --write
CI/CD Integration
GitHub Actions
name: Check Environment on: [push, pull_request] jobs: env-check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: shivammathur/setup-php@v2 with: php-version: 8.2 - run: composer install - run: php artisan env:check --ci - run: php artisan env:diff --quiet
GitLab CI
env-check: script: - composer install - php artisan env:check --ci - php artisan env:diff --quiet
Best Practices
- Always run
env:sync-examplebefore committing - Use
env:check --ciin your CI pipeline - Install the Git hook for automatic warnings
- Run
env:discoverperiodically to catch new env usage - Configure required keys for critical environment variables
How It Works
Parsing
- Reads
.envand.env.examplefiles directly from filesystem - Parses
KEY=VALUEpairs manually (does NOT useenv()helper) - Ignores comments (
#) and empty lines - Supports quoted values (
"value"or'value') - Handles
export KEY=valuesyntax
Syncing
- NEVER removes keys from
.env.example - NEVER overwrites existing keys
- Appends new keys at the bottom
- Preserves all comments and formatting
- Idempotent (running twice produces no changes)
Git Hook
- Installed in
.git/hooks/pre-commit - Non-blocking (always exits 0)
- Shows warning if keys are missing
- Suggests running
env:sync-example - Can be removed anytime
Example Workflow
# 1. Add new env var to .env echo "NEW_API_KEY=secret123" >> .env # 2. Check what's different php artisan env:diff # 3. Sync to .env.example php artisan env:sync-example # 4. Commit changes git add .env.example git commit -m "Add NEW_API_KEY to env.example"
Testing
composer test
Code Quality
# Format code composer format # Static analysis composer analyse
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
open-laravel/env-guardian 适用场景与选型建议
open-laravel/env-guardian 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「environment」 「env」 「dotenv」 「open-laravel」 「env-guardian」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 open-laravel/env-guardian 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 open-laravel/env-guardian 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 open-laravel/env-guardian 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
Multiple cascading environmental configuration files support for Laravel 5
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-26