horat1us/environment-config
Composer 安装命令:
composer require horat1us/environment-config
包简介
Environment Config class
README 文档
README
Simple class to provide config using getenv function with prefix.
Compatibility: tested on PHP 7.1, PHP 8.1
Installation
Using composer:
composer require horat1us/environment-config
Usage
Implement your own config class:
<?php namespace App; use Horat1us\Environment; class Config extends Environment\Config { public function getTimeout(): int { return $this->getEnv($key = 'APP_TIMEOUT', $default = 10); } public function getSlow(): string { // default can be instance of \Closure or callable array, like [$this, 'calculate'] return $this->getEnv($key = 'APP_KEY', $default = function(): string { return 'some-string'; // slow operation, may be fetching from DB }); } public function getNullValue(): ?string { /** * if you want to return null instead of throwing exceptio * if no environment variable found */ return $this->getEnv('KEY', [$this, 'null']); } public function getName(): string { return $this->getEnv($key = 'APP_NAME'); } }
then use it:
<?php use App; $config = new App\Config("PREFIX_"); $config->getTimeout(); // 10 putenv("PREFIX_APP_TIMEOUT=5"); $config->getTimeout(); // 5 $config->getSlow(); // some-string // MissingEnvironmentException will be thrown because no default value provided $config->getName();
MagicTrait
You can define your config keys/methods using MagicTrait:
<?php use Horat1us\Environment; class Config { use Environment\MagicTrait { getEnvironment as public getHost; } protected function getEnvironmentKeyPrefix(): string { return 'TEST_'; } } $config = new Config; $config->getHost(); // TEST_HOST environment key will be used to get value
Note: your environment getters should be named with prefix get and have camel case name
Author
License
horat1us/environment-config 适用场景与选型建议
horat1us/environment-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.5k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 04 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 horat1us/environment-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 horat1us/environment-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 15.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 19
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-11