codinglabsau/laravel-feature-flags
Composer 安装命令:
composer require codinglabsau/laravel-feature-flags
包简介
Dynamic feature flags for laravel.
README 文档
README
Laravel Feature Flags allows instant, zero-deployment toggling of application features.
The state of each feature flag can be checked from anywhere in the application code (including via a @feature('name') blade directive) to determine whether the conditions you set have been met to enable the feature.
Each feature can be in one of three states:
- On: enabled for everyone
- Off: disabled for everyone
- Dynamic: evaluated according to a feature-specific closure (with a fallback option)
Installation
Install With Composer
composer require codinglabsau/laravel-feature-flags
Database Migrations
php artisan vendor:publish --tag="feature-flags-migrations"
php artisan migrate
Publish Configuration
php artisan vendor:publish --tag="feature-flags-config"
Set Your Cache Store
This package caches the state of features to reduce redundant database queries. The cache is expired whenever the feature state changes.
By default, this package will use the default cache configured in your application.
If you wish to change to a different cache driver, update your .env:
FEATURES_CACHE_STORE=file
Usage
Create a new feature in the database and set the initial state:
use Codinglabs\FeatureFlags\Models\Feature; use Codinglabs\FeatureFlags\Enums\FeatureState; Feature::create([ 'name' => 'search-v2', 'state' => FeatureState::on() ]);
Its recommended that you seed the features to your database before a new deployment or as soon as possible after a deployment.
Check If A Feature Is Enabled
Blade View
Use the @feature blade directive anywhere in your view files.
@feature('search-v2') // new search goes here @else // legacy search here @endfeature
In Your Code
Use the FeatureFlag facade to conveniently check the state of a feature in your app code.
use Codinglabs\FeatureFlags\Facades\FeatureFlag; if (FeatureFlag::isOn('search-v2')) { // new feature code } else { // old code }
Middleware
Register feature as a route middleware in your HTTP Kernel to protect routes. A 404 response will be returned if the feature does not resolve to the on state.
// app/Http/Kernel.php protected $routeMiddleware = [ // ... 'feature' => \Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn::class, ]; // routes/web.php Route::get('search-v2', \App\Http\Controllers\SearchV2Controller::class)->middleware('feature:search-v2');
Check If A Feature Is Disabled
Blade View
@unlessfeature('search-v2') // no new features for you @endfeature
In Your Code
use Codinglabs\FeatureFlags\Facades\FeatureFlag; if (FeatureFlag::isOff('search-v2')) { // no new features for you }
Get The Underlying Current State
If you want to know what the underlying FeatureState value is:
use Codinglabs\FeatureFlags\Facades\FeatureFlag; // value from Codinglabs\FeatureFlags\Enums\FeatureState $featureState = FeatureFlag::getState('search-v2');
Updating Feature State
To change the state of a feature you can call the following methods:
use Codinglabs\FeatureFlags\Facades\FeatureFlag; FeatureFlag::turnOn('search-v2'); FeatureFlag::turnOff('search-v2'); FeatureFlag::makeDynamic('search-v2');
Alternatively you can set the state directly by passing a feature state enum:
FeatureFlag::updateFeatureState('search-v2', FeatureState::on())
It is recommended that you only update a features state using the above methods as it will take care of flushing the cache and dispatching the feature updated event:
\Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent::class
You should listen for the FeatureUpdatedEvent event if you have any downstream implications when a feature state is updated, such as invalidating any cached items that are referenced in dynamic handlers.
Advanced Usage
Dynamic Features
A dynamic handler can be defined in the boot() method of your AppServiceProvider:
use Codinglabs\FeatureFlags\Facades\FeatureFlag; FeatureFlag::registerDynamicHandler('search-v2', function ($feature, $request) { return $request->user() && $request->user()->hasRole('Tester'); });
Dynamic handlers will only be called when a feature is in the dynamic state. This will allow you to define custom rules around whether that feature is enabled like in the example above where the user can only access the feature if they have a tester role.
Each handler is provided with the features name and current request as arguments and must return a boolean value.
Default Handler For Dynamic Features
You may also define a default handler which will be the catch-all handler for features that don't have an explicit handler defined for them:
FeatureFlag::registerDefaultDynamicHandler(function ($feature, $request) { return $request->user() && $request->user()->hasRole('Tester'); });
An explicit handler defined using registerDynamicHandler() will take precedence over the default handler. If neither a default nor explicit handler has been defined then the feature will resolve to off by default.
Handle Missing Features
Features must exist in the database otherwise a MissingFeatureException will be thrown. This behaviour can be turned off by explicitly handling cases where a feature doesn't exist:
FeatureFlag::handleMissingFeaturesWith(function ($feature) { // log or report this somewhere... })
If a handler for missing features has been defined then an exception will not be thrown and the feature will resolve to off.
Using Your Own Model
To use your own model, update the config and replace the existing reference with your own model:
// app/config/feature-flags.php 'feature_model' => \App\Models\Feature::class,
Make sure to also cast the state column to a feature state enum using the FeatureStateCast:
// app/Models/Feature.php use Codinglabs\FeatureFlags\Casts\FeatureStateCast; protected $casts = [ 'state' => FeatureStateCast::class ];
Sharing features with UI (Inertiajs example)
// app/Middleware/HandleInertiaRequest.php use Codinglabs\FeatureFlags\FeatureFlags; use Codinglabs\FeatureFlags\Models\Feature; Inertia::share([ 'features' => function () { return Feature::all() ->filter(fn ($feature) => FeatureFlags::isOn($feature['name'])) ->pluck('name'); } ]);
// app.js Vue.mixin({ methods: { hasFeature: function(feature) { return this.$page.features.includes(feature) } } })
<!-- SomeComponent.vue --> <div v-if="hasFeature('search-v2')">Some cool new feature</div>
Testing
composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
codinglabsau/laravel-feature-flags 适用场景与选型建议
codinglabsau/laravel-feature-flags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 87.72k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2022 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「flags」 「feature」 「laravel」 「laravel-feature-flags」 「Codinglabs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codinglabsau/laravel-feature-flags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codinglabsau/laravel-feature-flags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codinglabsau/laravel-feature-flags 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
ZF2 module for the Opensoft Rollout library
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
A PHP trait to enable bitwise comparison of flags
Adds flags support to your model/entity
Rinvex Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
统计信息
- 总下载量: 87.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 38
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-30