jstewmc/detect-environment
Composer 安装命令:
composer require jstewmc/detect-environment
包简介
Detect an application's environment
关键字:
README 文档
README
Detect an application's environment.
namespace Jstewmc\DetectEnvironment; // define our possible environments $values = [ 'development' => 'foo', 'testing' => 'bar', 'staging' => 'baz', 'production' => 'qux' ]; // put the environment variable putenv('APP_ENV=foo'); // instantiate the service $service = new DetectEnvironment('APP_ENV', $values); // detect the environment $service->isDevelopment(); // returns true $service->isTesting(); // returns false $service->isStaging(); // returns false $service->isProduction(); // returns false // get the environment's name $service(); // returns "development"
Usage
To instantiate the service, you MUST pass the environment variable name and the environment variable values, indexed by application environment name:
namespace Jstewmc\DetectEnvironment; putenv('APP_ENV=foo'); $service = new DetectEnvironment( 'APP_ENV', [ 'development' => 'foo', 'testing' => 'bar', 'staging' => 'baz', 'production' => 'qux' ] );
In the example above, the service would detect the environment as development, testing, staging, and production when the APP_ENV environment was 'foo', 'bar', 'baz', and 'qux', respectively.
Keep in mind, the environment variable name, the environment variable values, and the application environment names MAY be any string. The environment variable values and the application environment names are case-insensitive.
You can check the application's environment using isX() methods, where X is any valid application environment name:
namespace Jstewmc\DetectEnvironment; putenv('APP_ENV=foo'); $service = new DetectEnvironment( 'APP_ENV', [ 'development' => 'foo', 'testing' => 'bar', 'staging' => 'baz', 'production' => 'qux' ] ); $service->isDevelopment(); // returns true $service->isTesting(); // returns false $service->isStaging(); // returns false $service->isProduction(); // returns false $service->foo(); // throws exception (MUST start with "is") $service->isFoo(); // throws exception (MUST be valid environment name)
Environment variable
In the examples above, the environment variable was set using the putenv() function. However, in the real world, you should define the environment variable in your server configuration (e.g., .htaccess, httpd.conf, etc).
No matter where you define the environment variable, it MUST be accessible to PHP's getenv function.
That's it!
License
Author
Version
2.0.0, October 2, 2016
- Add
__invoke()method to return environment name. - Rename class to
DetectEnvironment. The longer name seems more intentional.
1.0.0, August 13, 2016
- Major release
- Update
composer.json
0.2.0, August 13, 2016
- Refactor to support user-defined environments
0.1.0, June 25, 2016
- Initial release
jstewmc/detect-environment 适用场景与选型建议
jstewmc/detect-environment 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「detect」 「environment」 「jstewmc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jstewmc/detect-environment 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jstewmc/detect-environment 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jstewmc/detect-environment 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
A Laravel 5 Package Provider to Identify/detect a user's browser, device, operating system and Language
Multiple cascading environmental configuration files support for Laravel 5
An environment variable convenience library extension for vlucas/phpdotenv
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 65
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-07