You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> When using Pennant outside of an HTTP context, such as in an Artisan command or a queued job, you should typically [explicitly specify the feature's scope](#specifying-the-scope). Alternatively, you may define a [default scope](#default-scope) that accounts for both authenticated HTTP contexts and unauthenticated contexts.
180
181
181
182
<aname="checking-class-based-features"></a>
@@ -254,7 +255,7 @@ The `when` method may be used to fluently execute a given closure if a feature i
> **Note** As the `feature` middleware will check against the currently authenticated user, you should ensure that any authentication related middleware is applied _before_ the `feature` middleware.
304
-
305
304
<aname="customizing-the-response"></a>
306
305
#### Customizing The Response
307
306
@@ -381,25 +380,10 @@ if (Feature::for($user->team)->active('billing-v2')) {
381
380
// ...
382
381
```
383
382
384
-
The `for` method also accepts an array of scopes, which will check that the feature is active for all of the provided scopes:
385
-
386
-
```php
387
-
Feature::define('improved-notifications', function (string $email) {
Sometimes, you may need to customize the default scope Pennant uses to check features. For example, maybe all of your features are checked against the currently authenticated user's team instead of the user. Instead of having to call `Feature::for($user->team)` every time you check a feature, you may instead specify the team as the default scope. Typically, this should be done in one of your application's service providers:
386
+
It is also possible to customize the default scope Pennant uses to check features. For example, maybe all of your features are checked against the currently authenticated user's team instead of the user. Instead of having to call `Feature::for($user->team)` every time you check a feature, you may instead specify the team as the default scope. Typically, this should be done in one of your application's service providers:
403
387
404
388
```php
405
389
<?php
@@ -524,17 +508,6 @@ Pennant's included Blade directive also makes it easy to conditionally render co
524
508
@endfeature
525
509
```
526
510
527
-
If you would like to retrieve multiple feature values at once for a single scope, you may use the `values` method:
528
-
529
-
```php
530
-
Feature::values(['new-api', 'purchase-button']);
531
-
532
-
// [
533
-
// 'new-api' => false,
534
-
// 'purchase-button' => 'tart-orange',
535
-
// ]
536
-
```
537
-
538
511
> **Note** When using rich values, it is important to know that a feature is considered "active" when it has any value other than `false`.
When testing code that interacts with feature flags, the easiest way to control the feature flag's returned value in your tests is to simply re-define the feature. For example, imagine you have the following feature defined in one of your application's service provider:
To modify the feature's returned value in your tests, you may re-define the feature at the beginning of the test. The following test will always pass, even though the `Arr::random()` implementation is still present in the service provider:
650
+
651
+
```php
652
+
use Laravel\Pennant\Feature;
653
+
654
+
public function test_it_can_control_feature_values()
The same approach may be used for class based features:
663
+
664
+
```php
665
+
use App\Features\NewApi;
666
+
use Laravel\Pennant\Feature;
667
+
668
+
public function test_it_can_control_feature_values()
669
+
{
670
+
Feature::define(NewApi::class, true);
671
+
672
+
$this->assertTrue(Feature::value(NewApi::class));
673
+
}
674
+
```
675
+
676
+
If your feature is returning a `Lottery` instance, there are a handful of useful [testing helpers available](/docs/{{version}}/helpers#testing-lotteries).
677
+
660
678
<aname="events"></a>
661
679
## Events
662
680
663
681
Pennant dispatches a variety of events that can be useful when tracking feature flags throughout your application.
664
682
665
-
### `Laravel\Pennant\Events\FeatureRetrieved`
666
-
667
-
This event is dispatched whenever a feature is retrieved and can be useful for creating and tracking metrics against a feature flag's usage throughout your application.
This event is dispatched the first time a feature's value is resolved for a specific scope from it's definition or feature class resolver.
685
+
This event is dispatched the first time a known featureis retrieved during a request for a specific scope. This event can be useful to create and track metrics against the feature flags that are being used throughout your application.
This event is dispatched the first time an unknown feature is resolved for a specific scope. This event can be useful if you have intended to remove a feature flag, but may have accidentally left some stray references to it throughout your application.
689
+
This event is dispatched the first time an unknown feature is retrieved during a request for a specific scope. This event can be useful if you have intended to remove a feature flag, but may have accidentally left some stray references to it throughout your application.
676
690
677
691
For example, you may find it useful to listen for this event and `report` or throw an exception when it occurs:
678
692
@@ -683,7 +697,7 @@ namespace App\Providers;
683
697
684
698
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
685
699
use Illuminate\Support\Facades\Event;
686
-
use Laravel\Pennant\Events\UnknownFeatureResolved;
700
+
use Laravel\Pennant\Events\RetrievingUnknownFeature;
687
701
688
702
class EventServiceProvider extends ServiceProvider
689
703
{
@@ -692,14 +706,14 @@ class EventServiceProvider extends ServiceProvider
0 commit comments