跳到內容

Laravel Pennant

簡介

Laravel Pennant 是一個簡單且輕量的功能旗標套件 - 沒有多餘的裝飾。功能旗標讓您能夠自信地逐步推出新的應用程式功能、A/B 測試新的介面設計、完善基於主幹的開發策略等等。

安裝

首先,使用 Composer 套件管理器將 Pennant 安裝到您的專案中

1composer require laravel/pennant

接下來,您應該使用 vendor:publish Artisan 命令發佈 Pennant 設定檔和遷移檔案

1php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"

最後,您應該執行應用程式的資料庫遷移。這將建立一個 features 資料表,Pennant 使用它來驅動其 database 驅動程式

1php artisan migrate

設定

發佈 Pennant 的資源後,其設定檔將位於 config/pennant.php。此設定檔允許您指定 Pennant 將用來儲存已解析功能旗標值的預設儲存機制。

Pennant 包含透過 array 驅動程式將已解析功能旗標值儲存在記憶體內陣列中的支援。或者,Pennant 可以透過 database 驅動程式將已解析的功能旗標值持久儲存在關聯式資料庫中,這是 Pennant 使用的預設儲存機制。

定義功能

若要定義功能,您可以使用 Feature facade 提供的 define 方法。您需要為功能提供名稱,以及將被調用以解析功能初始值的閉包。

通常,功能是在服務提供者中使用 Feature facade 定義的。閉包將接收功能檢查的「作用域」。最常見的情況是,作用域是目前已驗證的使用者。在此範例中,我們將定義一個功能,用於逐步向應用程式使用者推出新的 API

1<?php
2 
3namespace App\Providers;
4 
5use App\Models\User;
6use Illuminate\Support\Lottery;
7use Illuminate\Support\ServiceProvider;
8use Laravel\Pennant\Feature;
9 
10class AppServiceProvider extends ServiceProvider
11{
12 /**
13 * Bootstrap any application services.
14 */
15 public function boot(): void
16 {
17 Feature::define('new-api', fn (User $user) => match (true) {
18 $user->isInternalTeamMember() => true,
19 $user->isHighTrafficCustomer() => false,
20 default => Lottery::odds(1 / 100),
21 });
22 }
23}

如您所見,我們針對此功能制定了以下規則

  • 所有內部團隊成員都應該使用新的 API。
  • 任何高流量客戶都不應該使用新的 API。
  • 否則,該功能應隨機分配給使用者,啟用的機率為 1/100。

首次針對特定使用者檢查 new-api 功能時,閉包的結果將由儲存驅動程式儲存。下次針對同一使用者檢查該功能時,將從儲存中檢索該值,並且不會調用閉包。

為了方便起見,如果功能定義僅傳回彩券,您可以完全省略閉包

1Feature::define('site-redesign', Lottery::odds(1, 1000));

基於類別的功能

Pennant 也允許您定義基於類別的功能。與基於閉包的功能定義不同,無需在服務提供者中註冊基於類別的功能。若要建立基於類別的功能,您可以調用 pennant:feature Artisan 命令。預設情況下,功能類別將放置在應用程式的 app/Features 目錄中

1php artisan pennant:feature NewApi

在編寫功能類別時,您只需要定義一個 resolve 方法,該方法將被調用以解析給定作用域的功能初始值。同樣,作用域通常是目前已驗證的使用者

1<?php
2 
3namespace App\Features;
4 
5use App\Models\User;
6use Illuminate\Support\Lottery;
7 
8class NewApi
9{
10 /**
11 * Resolve the feature's initial value.
12 */
13 public function resolve(User $user): mixed
14 {
15 return match (true) {
16 $user->isInternalTeamMember() => true,
17 $user->isHighTrafficCustomer() => false,
18 default => Lottery::odds(1 / 100),
19 };
20 }
21}

如果您想手動解析基於類別的功能實例,您可以調用 Feature facade 上的 instance 方法

1use Illuminate\Support\Facades\Feature;
2 
3$instance = Feature::instance(NewApi::class);

功能類別透過 容器 解析,因此您可以在需要時將依賴項注入到功能類別的建構子中。

自訂儲存的功能名稱

預設情況下,Pennant 將儲存功能類別的完整類別名稱。如果您想將儲存的功能名稱與應用程式的內部結構分離,您可以在功能類別上指定 $name 屬性。此屬性的值將取代類別名稱儲存

1<?php
2 
3namespace App\Features;
4 
5class NewApi
6{
7 /**
8 * The stored name of the feature.
9 *
10 * @var string
11 */
12 public $name = 'new-api';
13 
14 // ...
15}

檢查功能

若要判斷功能是否處於活動狀態,您可以使用 Feature facade 上的 active 方法。預設情況下,功能是針對目前已驗證的使用者檢查的

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Illuminate\Http\Request;
6use Illuminate\Http\Response;
7use Laravel\Pennant\Feature;
8 
9class PodcastController
10{
11 /**
12 * Display a listing of the resource.
13 */
14 public function index(Request $request): Response
15 {
16 return Feature::active('new-api')
17 ? $this->resolveNewApiResponse($request)
18 : $this->resolveLegacyApiResponse($request);
19 }
20 
21 // ...
22}

雖然預設情況下功能是針對目前已驗證的使用者檢查的,但您可以輕鬆地針對另一個使用者或作用域檢查該功能。若要完成此操作,請使用 Feature facade 提供的 for 方法

1return Feature::for($user)->active('new-api')
2 ? $this->resolveNewApiResponse($request)
3 : $this->resolveLegacyApiResponse($request);

Pennant 還提供了一些額外的便利方法,這些方法在判斷功能是否處於活動狀態時可能會很有用

1// Determine if all of the given features are active...
2Feature::allAreActive(['new-api', 'site-redesign']);
3 
4// Determine if any of the given features are active...
5Feature::someAreActive(['new-api', 'site-redesign']);
6 
7// Determine if a feature is inactive...
8Feature::inactive('new-api');
9 
10// Determine if all of the given features are inactive...
11Feature::allAreInactive(['new-api', 'site-redesign']);
12 
13// Determine if any of the given features are inactive...
14Feature::someAreInactive(['new-api', 'site-redesign']);

在 HTTP 環境之外使用 Pennant 時,例如在 Artisan 命令或佇列任務中,您通常應明確指定功能的作用域。或者,您可以定義一個預設作用域,該作用域同時適用於已驗證的 HTTP 環境和未驗證的環境。

檢查基於類別的功能

對於基於類別的功能,您應該在檢查功能時提供類別名稱

1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Features\NewApi;
6use Illuminate\Http\Request;
7use Illuminate\Http\Response;
8use Laravel\Pennant\Feature;
9 
10class PodcastController
11{
12 /**
13 * Display a listing of the resource.
14 */
15 public function index(Request $request): Response
16 {
17 return Feature::active(NewApi::class)
18 ? $this->resolveNewApiResponse($request)
19 : $this->resolveLegacyApiResponse($request);
20 }
21 
22 // ...
23}

條件執行

when 方法可用於在功能處於活動狀態時流暢地執行給定的閉包。此外,還可以提供第二個閉包,如果功能處於非活動狀態,則將執行該閉包

1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Features\NewApi;
6use Illuminate\Http\Request;
7use Illuminate\Http\Response;
8use Laravel\Pennant\Feature;
9 
10class PodcastController
11{
12 /**
13 * Display a listing of the resource.
14 */
15 public function index(Request $request): Response
16 {
17 return Feature::when(NewApi::class,
18 fn () => $this->resolveNewApiResponse($request),
19 fn () => $this->resolveLegacyApiResponse($request),
20 );
21 }
22 
23 // ...
24}

unless 方法的作用與 when 方法相反,如果功能處於非活動狀態,則執行第一個閉包

1return Feature::unless(NewApi::class,
2 fn () => $this->resolveLegacyApiResponse($request),
3 fn () => $this->resolveNewApiResponse($request),
4);

HasFeatures Trait

Pennant 的 HasFeatures trait 可以新增到應用程式的 User 模型(或任何其他具有功能的模型),以提供一種流暢、方便的方式直接從模型檢查功能

1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Laravel\Pennant\Concerns\HasFeatures;
7 
8class User extends Authenticatable
9{
10 use HasFeatures;
11 
12 // ...
13}

將 trait 新增到模型後,您可以透過調用 features 方法輕鬆檢查功能

1if ($user->features()->active('new-api')) {
2 // ...
3}

當然,features 方法提供了許多其他方便的方法來與功能互動

1// Values...
2$value = $user->features()->value('purchase-button')
3$values = $user->features()->values(['new-api', 'purchase-button']);
4 
5// State...
6$user->features()->active('new-api');
7$user->features()->allAreActive(['new-api', 'server-api']);
8$user->features()->someAreActive(['new-api', 'server-api']);
9 
10$user->features()->inactive('new-api');
11$user->features()->allAreInactive(['new-api', 'server-api']);
12$user->features()->someAreInactive(['new-api', 'server-api']);
13 
14// Conditional execution...
15$user->features()->when('new-api',
16 fn () => /* ... */,
17 fn () => /* ... */,
18);
19 
20$user->features()->unless('new-api',
21 fn () => /* ... */,
22 fn () => /* ... */,
23);

Blade 指令

為了使在 Blade 中檢查功能成為無縫體驗,Pennant 提供了 @feature@featureany 指令

1@feature('site-redesign')
2 <!-- 'site-redesign' is active -->
3@else
4 <!-- 'site-redesign' is inactive -->
5@endfeature
6 
7@featureany(['site-redesign', 'beta'])
8 <!-- 'site-redesign' or `beta` is active -->
9@endfeatureany

中介層

Pennant 還包含一個中介層,可用於驗證目前已驗證的使用者是否有權存取某項功能,然後再調用路由。您可以將中介層分配給路由,並指定存取該路由所需的功能。如果任何指定的功能對於目前已驗證的使用者處於非活動狀態,則路由將傳回 400 Bad Request HTTP 回應。多個功能可以傳遞到靜態 using 方法。

1use Illuminate\Support\Facades\Route;
2use Laravel\Pennant\Middleware\EnsureFeaturesAreActive;
3 
4Route::get('/api/servers', function () {
5 // ...
6})->middleware(EnsureFeaturesAreActive::using('new-api', 'servers-api'));

自訂回應

如果您想自訂當中介層在列出的功能之一處於非活動狀態時傳回的回應,您可以使用 EnsureFeaturesAreActive 中介層提供的 whenInactive 方法。通常,此方法應在應用程式的其中一個服務提供者的 boot 方法中調用

1use Illuminate\Http\Request;
2use Illuminate\Http\Response;
3use Laravel\Pennant\Middleware\EnsureFeaturesAreActive;
4 
5/**
6 * Bootstrap any application services.
7 */
8public function boot(): void
9{
10 EnsureFeaturesAreActive::whenInactive(
11 function (Request $request, array $features) {
12 return new Response(status: 403);
13 }
14 );
15 
16 // ...
17}

攔截功能檢查

有時,在檢索給定功能的儲存值之前執行一些記憶體內檢查可能會很有用。假設您正在功能旗標後開發新的 API,並且希望能夠停用新的 API,而不會遺失儲存中的任何已解析功能值。如果您發現新 API 中存在錯誤,您可以輕鬆地為除內部團隊成員之外的所有人停用它、修復錯誤,然後為先前有權存取該功能的使用者重新啟用新的 API。

您可以使用基於類別的功能before 方法來實現此目的。存在時,before 方法始終在記憶體內執行,然後再從儲存中檢索值。如果從該方法傳回非 null 值,則在請求期間將使用該值來取代功能的儲存值

1<?php
2 
3namespace App\Features;
4 
5use App\Models\User;
6use Illuminate\Support\Facades\Config;
7use Illuminate\Support\Lottery;
8 
9class NewApi
10{
11 /**
12 * Run an always-in-memory check before the stored value is retrieved.
13 */
14 public function before(User $user): mixed
15 {
16 if (Config::get('features.new-api.disabled')) {
17 return $user->isInternalTeamMember();
18 }
19 }
20 
21 /**
22 * Resolve the feature's initial value.
23 */
24 public function resolve(User $user): mixed
25 {
26 return match (true) {
27 $user->isInternalTeamMember() => true,
28 $user->isHighTrafficCustomer() => false,
29 default => Lottery::odds(1 / 100),
30 };
31 }
32}

您也可以使用此功能來排程先前在功能旗標後面的功能的全域推出

1<?php
2 
3namespace App\Features;
4 
5use Illuminate\Support\Carbon;
6use Illuminate\Support\Facades\Config;
7 
8class NewApi
9{
10 /**
11 * Run an always-in-memory check before the stored value is retrieved.
12 */
13 public function before(User $user): mixed
14 {
15 if (Config::get('features.new-api.disabled')) {
16 return $user->isInternalTeamMember();
17 }
18 
19 if (Carbon::parse(Config::get('features.new-api.rollout-date'))->isPast()) {
20 return true;
21 }
22 }
23 
24 // ...
25}

記憶體內快取

檢查功能時,Pennant 將建立結果的記憶體內快取。如果您使用的是 database 驅動程式,這表示在單個請求中重新檢查同一個功能旗標不會觸發額外的資料庫查詢。這也確保了功能在請求期間具有一致的結果。

如果您需要手動刷新記憶體內快取,您可以使用 Feature facade 提供的 flushCache 方法

1Feature::flushCache();

作用域

指定作用域

如前所述,功能通常是針對目前已驗證的使用者檢查的。但是,這可能並不總是適合您的需求。因此,可以透過 Feature facade 的 for 方法指定您想要針對其檢查給定功能的作用域

1return Feature::for($user)->active('new-api')
2 ? $this->resolveNewApiResponse($request)
3 : $this->resolveLegacyApiResponse($request);

當然,功能作用域不僅限於「使用者」。假設您建立了一個新的計費體驗,您正在向整個團隊而不是個別使用者推出。也許您希望最舊的團隊比新的團隊推出速度更慢。您的功能解析閉包可能如下所示

1use App\Models\Team;
2use Carbon\Carbon;
3use Illuminate\Support\Lottery;
4use Laravel\Pennant\Feature;
5 
6Feature::define('billing-v2', function (Team $team) {
7 if ($team->created_at->isAfter(new Carbon('1st Jan, 2023'))) {
8 return true;
9 }
10 
11 if ($team->created_at->isAfter(new Carbon('1st Jan, 2019'))) {
12 return Lottery::odds(1 / 100);
13 }
14 
15 return Lottery::odds(1 / 1000);
16});

您會注意到,我們定義的閉包不是預期 User,而是預期 Team 模型。若要判斷此功能對於使用者的團隊是否處於活動狀態,您應該將團隊傳遞給 Feature facade 提供的 for 方法

1if (Feature::for($user->team)->active('billing-v2')) {
2 return redirect('/billing/v2');
3}
4 
5// ...

預設作用域

也可以自訂 Pennant 用於檢查功能的預設作用域。例如,也許您的所有功能都是針對目前已驗證使用者的團隊而不是使用者檢查的。您不必每次檢查功能時都調用 Feature::for($user->team),而是可以指定團隊作為預設作用域。通常,這應該在應用程式的其中一個服務提供者中完成

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\Facades\Auth;
6use Illuminate\Support\ServiceProvider;
7use Laravel\Pennant\Feature;
8 
9class AppServiceProvider extends ServiceProvider
10{
11 /**
12 * Bootstrap any application services.
13 */
14 public function boot(): void
15 {
16 Feature::resolveScopeUsing(fn ($driver) => Auth::user()?->team);
17 
18 // ...
19 }
20}

如果未透過 for 方法明確提供作用域,則功能檢查現在將使用目前已驗證使用者的團隊作為預設作用域

1Feature::active('billing-v2');
2 
3// Is now equivalent to...
4 
5Feature::for($user->team)->active('billing-v2');

可為 Null 的作用域

如果您在檢查功能時提供的作用域為 null,並且功能的定義透過可為 Null 的類型或在聯合類型中包含 null 來表示不支援 null,則 Pennant 將自動傳回 false 作為功能的結果值。

因此,如果您要傳遞給功能的作用域可能為 null,並且您希望調用功能的 value 解析器,則應在功能定義中考慮到這一點。如果您在 Artisan 命令、佇列任務或未驗證的路由中檢查功能,則可能會發生 null 作用域。由於在這些環境中通常沒有已驗證的使用者,因此預設作用域將為 null

如果您並非總是明確指定功能作用域,那麼您應確保作用域的類型為「可為 Null」,並在功能定義邏輯中處理 null 作用域值

1use App\Models\User;
2use Illuminate\Support\Lottery;
3use Laravel\Pennant\Feature;
4 
5Feature::define('new-api', fn (User $user) => match (true) {
6Feature::define('new-api', fn (User|null $user) => match (true) {
7 $user === null => true,
8 $user->isInternalTeamMember() => true,
9 $user->isHighTrafficCustomer() => false,
10 default => Lottery::odds(1 / 100),
11});

識別作用域

Pennant 的內建 arraydatabase 儲存驅動程式知道如何正確儲存所有 PHP 資料類型以及 Eloquent 模型的 作用域識別碼。但是,如果您的應用程式使用協力廠商 Pennant 驅動程式,則該驅動程式可能不知道如何正確儲存 Eloquent 模型或應用程式中其他自訂類型的識別碼。

有鑑於此,Pennant 允許您透過在應用程式中用作 Pennant 作用域的物件上實作 FeatureScopeable 契約來格式化要儲存的作用域值。

例如,假設您在單個應用程式中使用兩個不同的功能驅動程式:內建的 database 驅動程式和協力廠商「Flag Rocket」驅動程式。「Flag Rocket」驅動程式不知道如何正確儲存 Eloquent 模型。相反,它需要一個 FlagRocketUser 實例。透過實作 FeatureScopeable 契約定義的 toFeatureIdentifier,我們可以自訂提供給應用程式使用的每個驅動程式的可儲存作用域值

1<?php
2 
3namespace App\Models;
4 
5use FlagRocket\FlagRocketUser;
6use Illuminate\Database\Eloquent\Model;
7use Laravel\Pennant\Contracts\FeatureScopeable;
8 
9class User extends Model implements FeatureScopeable
10{
11 /**
12 * Cast the object to a feature scope identifier for the given driver.
13 */
14 public function toFeatureIdentifier(string $driver): mixed
15 {
16 return match($driver) {
17 'database' => $this,
18 'flag-rocket' => FlagRocketUser::fromId($this->flag_rocket_id),
19 };
20 }
21}

序列化作用域

預設情況下,Pennant 將在儲存與 Eloquent 模型關聯的功能時使用完整類別名稱。如果您已在使用Eloquent morph map,您可以選擇讓 Pennant 也使用 morph map 來將儲存的功能與您的應用程式結構分離。

若要實現此目的,在服務提供者中定義 Eloquent morph map 後,您可以調用 Feature facade 的 useMorphMap 方法

1use Illuminate\Database\Eloquent\Relations\Relation;
2use Laravel\Pennant\Feature;
3 
4Relation::enforceMorphMap([
5 'post' => 'App\Models\Post',
6 'video' => 'App\Models\Video',
7]);
8 
9Feature::useMorphMap();

豐富的功能值

到目前為止,我們主要展示了處於二進位狀態的功能,這表示它們處於「活動」或「非活動」狀態,但 Pennant 也允許您儲存豐富的值。

例如,假設您正在測試應用程式「立即購買」按鈕的三種新顏色。您可以傳回字串,而不是從功能定義傳回 truefalse

1use Illuminate\Support\Arr;
2use Laravel\Pennant\Feature;
3 
4Feature::define('purchase-button', fn (User $user) => Arr::random([
5 'blue-sapphire',
6 'seafoam-green',
7 'tart-orange',
8]));

您可以使用 value 方法檢索 purchase-button 功能的值

1$color = Feature::value('purchase-button');

Pennant 包含的 Blade 指令也讓您可以根據功能的目前值輕鬆地條件式呈現內容

1@feature('purchase-button', 'blue-sapphire')
2 <!-- 'blue-sapphire' is active -->
3@elsefeature('purchase-button', 'seafoam-green')
4 <!-- 'seafoam-green' is active -->
5@elsefeature('purchase-button', 'tart-orange')
6 <!-- 'tart-orange' is active -->
7@endfeature

使用豐富的值時,務必知道當功能具有除 false 之外的任何值時,該功能被視為「活動」狀態。

當調用條件式 when 方法時,功能的豐富值將提供給第一個閉包

1Feature::when('purchase-button',
2 fn ($color) => /* ... */,
3 fn () => /* ... */,
4);

同樣地,當調用條件式 unless 方法時,功能的豐富值將提供給可選的第二個閉包

1Feature::unless('purchase-button',
2 fn () => /* ... */,
3 fn ($color) => /* ... */,
4);

檢索多個功能

values 方法允許檢索給定作用域的多個功能

1Feature::values(['billing-v2', 'purchase-button']);
2 
3// [
4// 'billing-v2' => false,
5// 'purchase-button' => 'blue-sapphire',
6// ]

或者,您可以使用 all 方法檢索給定作用域的所有已定義功能的值

1Feature::all();
2 
3// [
4// 'billing-v2' => false,
5// 'purchase-button' => 'blue-sapphire',
6// 'site-redesign' => true,
7// ]

但是,基於類別的功能是動態註冊的,並且在明確檢查之前,Pennant 並不知道它們。這表示,如果您的應用程式的基於類別的功能在當前請求期間尚未檢查,則它們可能不會出現在 all 方法傳回的結果中。

如果您想確保在使用 all 方法時始終包含功能類別,您可以使用 Pennant 的功能探索功能。若要開始使用,請在應用程式的其中一個服務提供者中調用 discover 方法

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\ServiceProvider;
6use Laravel\Pennant\Feature;
7 
8class AppServiceProvider extends ServiceProvider
9{
10 /**
11 * Bootstrap any application services.
12 */
13 public function boot(): void
14 {
15 Feature::discover();
16 
17 // ...
18 }
19}

discover 方法將註冊應用程式 app/Features 目錄中的所有功能類別。現在,all 方法將在其結果中包含這些類別,無論它們是否已在當前請求期間檢查過

1Feature::all();
2 
3// [
4// 'App\Features\NewApi' => true,
5// 'billing-v2' => false,
6// 'purchase-button' => 'blue-sapphire',
7// 'site-redesign' => true,
8// ]

預先載入

雖然 Pennant 會將單個請求的所有已解析功能的記憶體內快取保留在記憶體中,但仍然可能遇到效能問題。為了緩解這個問題,Pennant 提供了預先載入功能值的功能。

為了說明這一點,假設我們正在迴圈中檢查功能是否處於活動狀態

1use Laravel\Pennant\Feature;
2 
3foreach ($users as $user) {
4 if (Feature::for($user)->active('notifications-beta')) {
5 $user->notify(new RegistrationSuccess);
6 }
7}

假設我們正在使用資料庫驅動程式,此程式碼將為迴圈中的每個使用者執行一個資料庫查詢 - 可能執行數百個查詢。但是,使用 Pennant 的 load 方法,我們可以透過預先載入使用者或作用域集合的功能值來消除這種潛在的效能瓶頸

1Feature::for($users)->load(['notifications-beta']);
2 
3foreach ($users as $user) {
4 if (Feature::for($user)->active('notifications-beta')) {
5 $user->notify(new RegistrationSuccess);
6 }
7}

若要在功能值尚未載入時才載入功能值,您可以使用 loadMissing 方法

1Feature::for($users)->loadMissing([
2 'new-api',
3 'purchase-button',
4 'notifications-beta',
5]);

您可以使用 loadAll 方法載入所有已定義的功能

1Feature::for($users)->loadAll();

更新值

首次解析功能的值時,基礎驅動程式會將結果儲存在儲存中。這通常是必要的,以確保使用者在跨請求時獲得一致的體驗。但是,有時您可能想要手動更新功能的儲存值。

若要完成此操作,您可以使用 activatedeactivate 方法來切換功能的「開啟」或「關閉」

1use Laravel\Pennant\Feature;
2 
3// Activate the feature for the default scope...
4Feature::activate('new-api');
5 
6// Deactivate the feature for the given scope...
7Feature::for($user->team)->deactivate('billing-v2');

也可以透過為 activate 方法提供第二個引數來手動設定功能的豐富值

1Feature::activate('purchase-button', 'seafoam-green');

若要指示 Pennant 忘記功能的儲存值,您可以使用 forget 方法。當再次檢查功能時,Pennant 將從其功能定義中解析功能的值

1Feature::forget('purchase-button');

批量更新

若要批量更新儲存的功能值,您可以使用 activateForEveryonedeactivateForEveryone 方法。

例如,假設您現在對 new-api 功能的穩定性充滿信心,並且已確定結帳流程的最佳 'purchase-button' 顏色 - 您可以相應地更新所有使用者的儲存值

1use Laravel\Pennant\Feature;
2 
3Feature::activateForEveryone('new-api');
4 
5Feature::activateForEveryone('purchase-button', 'seafoam-green');

或者,您可以為所有使用者停用該功能

1Feature::deactivateForEveryone('new-api');

這只會更新 Pennant 儲存驅動程式已儲存的已解析功能值。您還需要更新應用程式中的功能定義。

清除功能

有時,從儲存中清除整個功能可能會很有用。如果您已從應用程式中刪除該功能,或者您對功能定義進行了調整,而您想要將這些調整推出給所有使用者,則通常需要這樣做。

您可以使用 purge 方法移除功能的所有儲存值

1// Purging a single feature...
2Feature::purge('new-api');
3 
4// Purging multiple features...
5Feature::purge(['new-api', 'purchase-button']);

如果您想從儲存中清除所有功能,您可以調用不帶任何引數的 purge 方法

1Feature::purge();

由於在應用程式的部署管道中清除功能可能會很有用,因此 Pennant 包含一個 pennant:purge Artisan 命令,該命令將從儲存中清除提供的功能

1php artisan pennant:purge new-api
2 
3php artisan pennant:purge new-api purchase-button

也可以清除除給定功能清單中的功能之外的所有功能。例如,假設您想要清除所有功能,但保留「new-api」和「purchase-button」功能的儲存值。若要完成此操作,您可以將這些功能名稱傳遞給 --except 選項

1php artisan pennant:purge --except=new-api --except=purchase-button

為了方便起見,pennant:purge 命令也支援 --except-registered 旗標。此旗標表示應清除除在服務提供者中明確註冊的功能之外的所有功能

1php artisan pennant:purge --except-registered

測試

在測試與功能旗標互動的程式碼時,在測試中控制功能旗標的傳回值的最簡單方法是直接重新定義功能。例如,假設您在應用程式的其中一個服務提供者中定義了以下功能

1use Illuminate\Support\Arr;
2use Laravel\Pennant\Feature;
3 
4Feature::define('purchase-button', fn () => Arr::random([
5 'blue-sapphire',
6 'seafoam-green',
7 'tart-orange',
8]));

若要在測試中修改功能的傳回值,您可以在測試開始時重新定義功能。即使 Arr::random() 實作仍然存在於服務提供者中,以下測試也將始終通過

1use Laravel\Pennant\Feature;
2 
3test('it can control feature values', function () {
4 Feature::define('purchase-button', 'seafoam-green');
5 
6 expect(Feature::value('purchase-button'))->toBe('seafoam-green');
7});
1use Laravel\Pennant\Feature;
2 
3public function test_it_can_control_feature_values()
4{
5 Feature::define('purchase-button', 'seafoam-green');
6 
7 $this->assertSame('seafoam-green', Feature::value('purchase-button'));
8}

相同的方法可以用於基於類別的功能

1use Laravel\Pennant\Feature;
2 
3test('it can control feature values', function () {
4 Feature::define(NewApi::class, true);
5 
6 expect(Feature::value(NewApi::class))->toBeTrue();
7});
1use App\Features\NewApi;
2use Laravel\Pennant\Feature;
3 
4public function test_it_can_control_feature_values()
5{
6 Feature::define(NewApi::class, true);
7 
8 $this->assertTrue(Feature::value(NewApi::class));
9}

如果您的功能傳回 Lottery 實例,則有一些有用的測試輔助函式可用

儲存設定

您可以透過在應用程式的 phpunit.xml 檔案中定義 PENNANT_STORE 環境變數來設定 Pennant 在測試期間將使用的儲存區

1<?xml version="1.0" encoding="UTF-8"?>
2<phpunit colors="true">
3 <!-- ... -->
4 <php>
5 <env name="PENNANT_STORE" value="array"/>
6 <!-- ... -->
7 </php>
8</phpunit>

新增自訂 Pennant 驅動程式

實作驅動程式

如果 Pennant 現有的儲存驅動程式都不符合您的應用程式需求,您可以編寫自己的儲存驅動程式。您的自訂驅動程式應實作 Laravel\Pennant\Contracts\Driver 介面

1<?php
2 
3namespace App\Extensions;
4 
5use Laravel\Pennant\Contracts\Driver;
6 
7class RedisFeatureDriver implements Driver
8{
9 public function define(string $feature, callable $resolver): void {}
10 public function defined(): array {}
11 public function getAll(array $features): array {}
12 public function get(string $feature, mixed $scope): mixed {}
13 public function set(string $feature, mixed $scope, mixed $value): void {}
14 public function setForAllScopes(string $feature, mixed $value): void {}
15 public function delete(string $feature, mixed $scope): void {}
16 public function purge(array|null $features): void {}
17}

現在,我們只需要使用 Redis 連線來實作這些方法中的每一種。有關如何實作這些方法中的每一種的範例,請查看 Pennant 原始碼中的 Laravel\Pennant\Drivers\DatabaseDriver

Laravel 沒有附帶包含擴充功能的目錄。您可以將它們放置在您喜歡的任何位置。在此範例中,我們建立了一個 Extensions 目錄來存放 RedisFeatureDriver

註冊驅動程式

實作驅動程式後,您就可以向 Laravel 註冊它了。若要向 Pennant 新增其他驅動程式,您可以使用 Feature facade 提供的 extend 方法。您應該從應用程式的其中一個服務提供者boot 方法中調用 extend 方法

1<?php
2 
3namespace App\Providers;
4 
5use App\Extensions\RedisFeatureDriver;
6use Illuminate\Contracts\Foundation\Application;
7use Illuminate\Support\ServiceProvider;
8use Laravel\Pennant\Feature;
9 
10class AppServiceProvider extends ServiceProvider
11{
12 /**
13 * Register any application services.
14 */
15 public function register(): void
16 {
17 // ...
18 }
19 
20 /**
21 * Bootstrap any application services.
22 */
23 public function boot(): void
24 {
25 Feature::extend('redis', function (Application $app) {
26 return new RedisFeatureDriver($app->make('redis'), $app->make('events'), []);
27 });
28 }
29}

註冊驅動程式後,您可以在應用程式的 config/pennant.php 設定檔中使用 redis 驅動程式

1'stores' => [
2 
3 'redis' => [
4 'driver' => 'redis',
5 'connection' => null,
6 ],
7 
8 // ...
9 
10],

外部定義功能

如果您的驅動程式是協力廠商功能旗標平台的包裝函式,您可能會在平台上定義功能,而不是使用 Pennant 的 Feature::define 方法。在這種情況下,您的自訂驅動程式也應實作 Laravel\Pennant\Contracts\DefinesFeaturesExternally 介面

1<?php
2 
3namespace App\Extensions;
4 
5use Laravel\Pennant\Contracts\Driver;
6use Laravel\Pennant\Contracts\DefinesFeaturesExternally;
7 
8class FeatureFlagServiceDriver implements Driver, DefinesFeaturesExternally
9{
10 /**
11 * Get the features defined for the given scope.
12 */
13 public function definedFeaturesForScope(mixed $scope): array {}
14 
15 /* ... */
16}

definedFeaturesForScope 方法應傳回為提供的作用域定義的功能名稱清單。

事件

Pennant 分派各種事件,這些事件在追蹤整個應用程式中的功能旗標時可能很有用。

Laravel\Pennant\Events\FeatureRetrieved

每當檢查功能時,都會分派此事件。此事件可用於針對整個應用程式中的功能旗標使用情況建立和追蹤指標。

Laravel\Pennant\Events\FeatureResolved

首次為特定作用域解析功能的值時,會分派此事件。

Laravel\Pennant\Events\UnknownFeatureResolved

首次為特定作用域解析未知功能時,會分派此事件。如果您打算移除功能旗標,但意外地在整個應用程式中遺留了對它的無效參考,則監聽此事件可能會很有用

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\ServiceProvider;
6use Illuminate\Support\Facades\Event;
7use Illuminate\Support\Facades\Log;
8use Laravel\Pennant\Events\UnknownFeatureResolved;
9 
10class AppServiceProvider extends ServiceProvider
11{
12 /**
13 * Bootstrap any application services.
14 */
15 public function boot(): void
16 {
17 Event::listen(function (UnknownFeatureResolved $event) {
18 Log::error("Resolving unknown feature [{$event->feature}].");
19 });
20 }
21}

Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass

在請求期間首次動態檢查基於類別的功能時,會分派此事件。

Laravel\Pennant\Events\UnexpectedNullScopeEncountered

null 作用域傳遞給不支援 null 的功能定義時,會分派此事件。

這種情況會得到妥善處理,並且該功能將傳回 false。但是,如果您想選擇退出此功能的預設妥善處理行為,您可以在應用程式的 AppServiceProviderboot 方法中註冊此事件的監聽器

1use Illuminate\Support\Facades\Log;
2use Laravel\Pennant\Events\UnexpectedNullScopeEncountered;
3 
4/**
5 * Bootstrap any application services.
6 */
7public function boot(): void
8{
9 Event::listen(UnexpectedNullScopeEncountered::class, fn () => abort(500));
10}

Laravel\Pennant\Events\FeatureUpdated

通常透過調用 activatedeactivate 來更新作用域的功能時,會分派此事件。

Laravel\Pennant\Events\FeatureUpdatedForAllScopes

通常透過調用 activateForEveryonedeactivateForEveryone 來更新所有作用域的功能時,會分派此事件。

Laravel\Pennant\Events\FeatureDeleted

通常透過調用 forget 來刪除作用域的功能時,會分派此事件。

Laravel\Pennant\Events\FeaturesPurged

清除特定功能時,會分派此事件。

Laravel\Pennant\Events\AllFeaturesPurged

清除所有功能時,會分派此事件。