cekurte/environment
Composer 安装命令:
composer require cekurte/environment
包简介
A library to get the values from environment variables and process to php data types
README 文档
README
- A simple library (with all methods covered by php unit tests) to increase the power of your environment variables, contribute with this project!
Installation
- The package is available on Packagist.
- The source files is PSR-2 compatible.
- Autoloading is PSR-4 compatible.
composer require cekurte/environment
Documentation
Setup your variables to PHP with putenv("KEY=VALUE") or put your environment variables into OS directly. Additionally you can use this library to perform this task vlucas/phpdotenv (we recommend that you use this library).
And now, use our library to get values and increase the power of your environment variables.
<?php use function Cekurte\Environment\env; // To get values using a function (requires php >=5.6) use Cekurte\Environment\Environment; // To get values using a static class use Cekurte\Environment\EnvironmentVariable; // To get values using a object // ... $data = Environment::get("YOUR_ENVIRONMENT_KEY"); // Getting default data if your environment variable not exists or not is loaded. $data = Environment::get("APP_DEBUG", true); // ... // Other ways to get the values of environment variables. // Using a object... $data = (new EnvironmentVariable())->get("YOUR_ENVIRONMENT_KEY", "defaultValue"); // Using a function... @deprecated $data = env("YOUR_ENVIRONMENT_KEY", "defaultValue"); // Note that if the second parameter is omitted // then the return value (if your key not exists) will be null. // A new way was added to you get all environment variables as an array. $data = Environment::getAll(); // You can use a filter to get only the environment variables that you need. $data = Environment::getAll([ new Cekurte\Environment\Filter\KeyRegexFilter('/PROCESSOR/'), new Cekurte\Environment\Filter\ValueRegexFilter('/6/'), ]); // The method above will get all environment variables, where: // the environment variable name has the word PROCESSOR AND // the environment variable value has the number six. // And you can develop your own filters, contribute with this project!
This command will get the value of the environment variable and will convert values to respective php data type.
Actually are available the following resources to process your environment variables:
Examples
In this section you can see the examples to all resource types.
ArrayResource
The array resource parse a value from environment variable that is a string to the array php data type. Supposing that there exists an environment variable named "ENV_ARRAY" with the following value [1,2,3,"key"=>"value"].
export ENV_ARRAY=[1,2,3,"key"=>"value"]
When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to array) using the ArrayResource class.
<?php // array(4) { // [0]=> int(1) // [1]=> int(2) // [2]=> int(3) // ["key"]=> string(5) "value" // } var_dump(Cekurte\Environment\Environment::get("ENV_ARRAY"));
BooleanResource
The boolean resource parse a value from environment variable that is a string to the boolean php data type. Supposing that there exists an environment variable named "ENV_BOOL" with the following value true.
export ENV_BOOL=true
When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to boolean) using the BooleanResource class.
<?php // bool(true) var_dump(Cekurte\Environment\Environment::get("ENV_BOOL"));
JsonResource
The json resource parse a value from environment variable that is a string to the array php data type. Supposing that there exists an environment variable named "ENV_JSON" with the following value {"key":"value"}.
export ENV_JSON={"key":"value"}
When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to array) using the JsonResource class.
<?php // array(1) { // ["key"]=> string(5) "value" // } var_dump(Cekurte\Environment\Environment::get("ENV_JSON"));
NullResource
The null resource parse a value from environment variable that is a string to the null php data type. Supposing that there exists an environment variable named "ENV_NULL" with the following value null.
export ENV_NULL=null
When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to null) using the NullResource class.
<?php // NULL var_dump(Cekurte\Environment\Environment::get("ENV_NULL"));
NumericResource
The numeric resource parse a value from environment variable that is a string to the numeric php data type (an integer or a float). Supposing that there exists an environment variable named "ENV_NUMERIC" with the following value 123.
export ENV_NUMERIC=123
When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to int) using the NumericResource class.
<?php // int(123) var_dump(Cekurte\Environment\Environment::get("ENV_NUMERIC"));
UnknownResource
The unknown resource no parse the values from environment variable and is used to get values when all resource types can not parse the data. Supposing that there exists an environment variable named "ENV_UNKNOWN" with the following value "unknown".
export ENV_UNKNOWN="unknown"
When you read this environment variable with our library, then it will get the raw value using the UnknownResource class.
<?php // string(7) "unknown" var_dump(Cekurte\Environment\Environment::get("ENV_UNKNOWN"));
If you liked of this library, give me a star =).
Contributing
- Give me a star =)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Make your changes
- Run the tests, adding new ones for your own code if necessary (
vendor/bin/phpunit) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
cekurte/environment 适用场景与选型建议
cekurte/environment 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86.53k 次下载、GitHub Stars 达 58, 最近一次更新时间为 2015 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「process」 「config」 「environment」 「env」 「types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cekurte/environment 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cekurte/environment 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cekurte/environment 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
Shell command module for PHP.
Adds the EDTF data type to Wikibase
A Zend Framework module to quickly and easily set PHP settings.
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 86.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 58
- 点击次数: 28
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-28