encodia/laravel-health-env-vars
Composer 安装命令:
composer require encodia/laravel-health-env-vars
包简介
Custom check for Spatie's Laravel Health - Ensure every .env variable you need has been set
README 文档
README
Laravel Health by Spatie, in addition to providing some default checks, allows you to create your own.
This package checks if all variables you need have been set in your .env file.
Starting from v1.8.0, you can also ensure a variable has been set to a certain value.
Some variables are needed in every environment; others only in specific ones.
For example, you want to be sure that BUGSNAG_API_KEY has been set in your production
environment, but you don't need this while developing locally.
Did anyone say "it works on my machine"?
Who has never lost several minutes before realizing that, let's say in production,
something is not working because one or more variables have not been valued?
Requirements
encodia/laravel-health-env-vars requires PHP 8.0+, Laravel 8.0+.
PHP 8.1+ is required with Laravel 10.
PHP 8.2+ is required with Laravel 11 and Laravel 12.
PHP 8.3+ is required with Laravel 13.
Version compatibility
| Laravel | This package |
|---|---|
| 13.x | 1.11.0 |
| 12.x | 1.10.0 |
| 8.x - 11.x | 1.9.1 |
Installation
You can install the package via composer:
composer require encodia/laravel-health-env-vars
Usage
Register this Check just like the others:
// typically, in a service provider use Spatie\Health\Facades\Health; use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck; use Encodia\Health\Checks\EnvVars; Health::checks([ // From Spatie's examples UsedDiskSpaceCheck::new() ->warnWhenUsedSpaceIsAbovePercentage(70) ->failWhenUsedSpaceIsAbovePercentage(90), // Many other checks... /* * Check that SOME_API_KEY and MAIL_FROM_ADDRESS variables are * set (no matter in which environment) */ EnvVars::new() ->requireVars([ 'SOME_API_KEY', 'MAIL_FROM_ADDRESS', ]) ]);
Need to check only in a specific environment if a variable has been set?
No problem:
use Spatie\Health\Facades\Health; use Encodia\Health\Checks\EnvVars; Health::checks([ // ... // (other checks) // ... /* * Check that SOME_API_KEY and MAIL_FROM_ADDRESS variables are * set (no matter in which environment). * * Only in staging, ensure EXTENDED_DEBUG_MODE has been set. * * Additionally, only in production, * ensure BUGSNAG_API_KEY has been set. */ EnvVars::new() ->requireVars([ 'SOME_API_KEY', 'MAIL_FROM_ADDRESS', ]) ->requireVarsForEnvironment('staging', [ 'EXTENDED_DEBUG_MODE' ]) ->requireVarsForEnvironment('production', [ 'BUGSNAG_API_KEY' ]); ]);
It's very likely that you need some variables in multiple environments, but not in all of them.
For example, you need to set BUGSNAG_API_KEY only in these environments:
qaproduction
but not in local, staging, demo or whatever.
You could chain multiple requireVarsForEnvironment calls but, in this case, it's better to
use requireVarsForEnvironments:
use Spatie\Health\Facades\Health; use Encodia\Health\Checks\EnvVars; Health::checks([ // ... // (other checks) // ... /* * Check that SOME_API_KEY and MAIL_FROM_ADDRESS variables are * set (no matter in which environment). * * Only in staging, ensure EXTENDED_DEBUG_MODE has been set. * * Additionally, only in qa and production environments, * ensure BUGSNAG_API_KEY has been set. */ EnvVars::new() ->requireVars([ 'SOME_API_KEY', 'MAIL_FROM_ADDRESS', ]) ->requireVarsForEnvironment('staging', [ 'EXTENDED_DEBUG_MODE' ]) ->requireVarsForEnvironments(['qa', 'production'], [ 'BUGSNAG_API_KEY' ]); ]);
Need to check if a variable has been set to a specific value?
Starting from v1.8.0, you can use requireVarsMatchValues to perform this check, regardless of the current
environment.
If you need to run this check only if the current environment matches the given one(s), you can
use requireVarsForEnvironment or requireVarsForEnvironments.
Examples:
use Encodia\Health\Checks\EnvVars; use Spatie\Health\Facades\Health; Health::checks([ EnvVars::new() // ... other methods ... ->requireVarsMatchValues([ // Ensure that APP_LOCALE is set to 'en' (no matter which is the current environment) 'APP_LOCALE' => 'en', // Ensure that APP_TIMEZONE is set to 'UTC' (no matter which is the current environment) 'APP_TIMEZONE' => 'UTC', ]) ->requireVarsMatchValuesForEnvironment('staging', [ // Only if current environment is 'staging', we don't want to send e-mails to real customers 'MAIL_MAILER' => 'log', ]) ->requireVarsMatchValuesForEnvironments(['qa', 'production'], [ // Only if current environment is 'qa' or 'production, we want to log 'info' events or above 'LOG_LEVEL' => 'info', // Only if current environment is 'qa' or 'production, we want to store assets to S3 'FILESYSTEM_DISK' => 's3', ]); ]);
⚠️ When checking values, do not store personal data, keys, tokens, etc.!
Caveats
During your deployment process, be sure to run EnvVars checks before caching your configuration!
Why? After running php artisan config:cache, any env('WHATEVER_NAME') will return null, so
your EnvVars checks will fail.
Please check
Important
From version 1.9.0, when configuration is cached (e.g. via php artisan config:cache), these checks are bypassed
and they return OK.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
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.
encodia/laravel-health-env-vars 适用场景与选型建议
encodia/laravel-health-env-vars 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 164.4k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2022 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「health」 「laravel」 「environment」 「env」 「checks」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 encodia/laravel-health-env-vars 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 encodia/laravel-health-env-vars 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 encodia/laravel-health-env-vars 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
Is your Laravel app OK? Health checks running in production to ensure you can sleep well at night and be sure everything is still OK.
Provides healthcheck endpoints in accordance with IETF's healthcheck draft RFC
Provide status and health api endpoints
Multiple cascading environmental configuration files support for Laravel 5
统计信息
- 总下载量: 164.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-27