0xc / openfeature
dev-main
2022-10-10 03:11 UTC
Requires
- php: ^7.4 || ^8
- myclabs/php-enum: ^1.8
- psr/log: ^1.1
Requires (Dev)
- captainhook/captainhook: ^5.10
- captainhook/plugin-composer: ^5.3
- ergebnis/composer-normalize: ^2.25
- hamcrest/hamcrest-php: ^2.0
- mdwheele/zalgo: ^0.3.1
- mockery/mockery: ^1.5
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ~1.8.0
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- psalm/plugin-mockery: ^0.9.1
- psalm/plugin-phpunit: ^0.17.0
- ramsey/coding-standard: ^2.0.3
- ramsey/composer-repl: ^1.4
- ramsey/conventional-commits: ^1.3
- roave/security-advisories: dev-latest
- spatie/phpunit-snapshot-assertions: ^4.2
- vimeo/psalm: ~4.25.0
This package is auto-updated.
Last update: 2023-01-02 21:07:49 UTC
README
Alpha 检查清单
- 符合规范
- 包含测试套件,用于验证与规范一致的行为
- 包含合理的覆盖率测试套件
- 自动发布
- 全面的README
免责声明
我将这个项目作为一个 PHP OpenFeature 演示阶段的潜在项目,未来的工作将围绕 Split PHP 提供程序(可能使用他们现有的包)。它尚未完成,非常处于开发中。
概述
此包为 OpenFeature API 和客户端提供了一个功能性的 SDK。它还基于各种 PSR(PHP 标准建议)如日志接口(PSR-3)和基本和扩展编码标准(PSR-1 和 PSR-12)。
未来的开发可能旨在允许此库通过文件路径自动加载(PSR-4),并可选择通过全局 $_SESSION
访问与容器标准(PSR-11)集成。
安装
$ composer require 0xc/openfeature // installs the latest version
使用
虽然 Boolean
提供了最简单的介绍,但我们提供了各种标志类型。
use OpenFeature\OpenFeatureClient; class MyClass { private OpenFeatureClient $client; public function __construct() { $this->client = OpenFeatureAPI->getInstance()->getClient('MyClass'); } public function booleanExample(): UI { // Should we render the redesign? Or the default webpage? if ($this->client->getBooleanValue('redesign_enabled', false)) { return render_redesign(); } return render_normal(); } public function stringExample(): Template { // Get the template to load for the custom new homepage $template = $this->client->getStringValue('homepage_template', 'default-homepage.html'); return render_template($template); } public function numberExample(): List<HomepageModule> { // How many modules should we be fetching? $count = $this->client->getIntegerValue('module-fetch-count', 4); return fetch_modules($count); } public function structureExample(): HomepageModule { $obj = $this->client->getObjectValue('hero-module', $previouslyDefinedDefaultStructure); return HomepageModuleBuilder::new() ->title($obj->getValue('title')) ->body($obj->getValue('description')) ->build(); } }
配置
要配置 OpenFeature,您需要向全局单例 OpenFeatureAPI
添加提供程序。从那里,您可以生成一个可用于您的代码的 Client
。如果您未设置提供程序,则将使用简单地返回传入的默认值的 NoOpProvider
。
use OpenFeature\OpenFeatureAPI; use OpenFeature\Providers\Flagd\FlagdProvider; class MyApp { public function bootstrap(){ $api = OpenFeatureAPI.getInstance(); $api->setProvider(new FlagdProvider()); $client = $api->getClient(); // Now use your `$client` instance to evaluate some feature flags! } }
开发
PHP 版本
此库针对 PHP 7.4 及更高版本。只要您的系统上有任何兼容版本的 PHP,您就应该能够利用 OpenFeature SDK。
此包还有一个 .tool-versions
文件,用于与 PHP 版本管理器(如 asdf
)一起使用。
安装和依赖项
使用 composer install
安装依赖项。composer install
将使用最新兼容版本更新 composer.lock
。
我们重视尽可能少地使用运行时依赖项。添加任何依赖项都需要仔细考虑和审查。
测试
使用 composer run test
运行测试。