adhocore/env
Composer 安装命令:
composer require adhocore/env
包简介
Environment variable loader and retriever for PHP
关键字:
README 文档
README
- Environment variable loader and retriever for PHP.
- Sanitization/Filters can be applied on retrieval if
filterextension is loaded. - Using env to configure application is one of the 12 postulates.
Installation
composer require adhocore/env
Usage
Loading
use Ahc\Env\Loader; // Load env variables from .env file to `putenv` by default: (new Loader)->load('/project/root/.env'); // Pass in boolean second param to control if the env should be reloaded: (new Loader)->load('/project/root/.env', true); // Load to $_SERVER global: (new Loader)->load('/project/root/.env', true, Loader::SERVER); // Load to $_ENV global and putenv(): (new Loader)->load('/project/root/.env', true, Loader::ENV | Loader::PUTENV); // Load to all targets: (new Loader)->load('/project/root/.env', true, Loader::ALL);
Always wrap complex values within double quotes in
.envfile. Eg:APP_KEY="K&^¢*&D(?<µ}^(P\]X"
ENV Format
Supports # or ; comments. Literal double quote should be escaped like "". See more examples below:
# comment line
a=1
b="2"
c=$3#
; also comment line
d="lol"
# empty
e=
# f is `"6"`
f=""6""
1_2=one_two
# empty too
E=""
A_B=Apple Ball
x=Y
Reference is possible like so:
MAIN=1
REF=${MAIN}/2
REF2=${REF}/3
# below will not be parsed as INV is not resolved
REF3=${INV}
Retrieving
use Ahc\Env\Retriever; // Retrieve: echo Retriever::getEnv($key); // Default value: echo Retriever::getEnv('PAYMENT_GATEWAY', 'stripe'); // Sanitization (pass third and optionally fourth parameters): echo Retriever::getEnv('MYSQL_PORT', 3306, FILTER_VALIDATE_INT); // Or you can use `env()` which is alias of `Retriever::getEnv()`: echo env('THE_KEY');
See filter_var for more on sanitizing/filtering values!
Benchmark
If you are interested here is a simple benchmark.
Consideration
By default this library only loads env to putenv().
Be cautious exposing confidential credentials into $_ENV and $_SERVER which bug/error catchers may log.
Although this libray is already fast enough, in production you might want to boost performance a little by loading if only required:
if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) { // Override false :) (new Loader)->load('/project/root/.env', false); }
For example if your app last introduced FB_APP_ID env, but this value is not already hard set in the machine,
it would be loaded via .env file else you are already covered.
Credits
adhocore/env 适用场景与选型建议
adhocore/env 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.26k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2017 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「env」 「env-php」 「env-loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adhocore/env 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adhocore/env 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adhocore/env 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
.env editor library
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
Utils to load, parse and work with configuration on Mezzio projects
Env builder is a package which collects different env files within a specified directory and compiles them into one single .env file, useful when you have different .env files laying around in different folders and you want to have one single big file that you can load with Symfony's DotEnv loader o
.env file handler
统计信息
- 总下载量: 11.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-22