Skip to content

Commit 0b59c83

Browse files
[10.x] Add documentation for custom pennant driver (#8553)
* Add documentation for custom pennant driver * remove conflict --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 075b0ce commit 0b59c83

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

pennant.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
- [Bulk Updates](#bulk-updates)
2222
- [Purging Features](#purging-features)
2323
- [Testing](#testing)
24+
- [Adding Custom Pennant Drivers](#adding-custom-pennant-drivers)
25+
- [Implementing The Driver](#implementing-the-driver)
26+
- [Registering The Driver](#registering-the-driver)
2427
- [Events](#events)
25-
- [Testing](#testing)
2628

2729
<a name="introduction"></a>
2830
## Introduction
@@ -675,6 +677,89 @@ public function test_it_can_control_feature_values()
675677

676678
If your feature is returning a `Lottery` instance, there are a handful of useful [testing helpers available](/docs/{{version}}/helpers#testing-lotteries).
677679

680+
<a name="adding-custom-pennant-drivers"></a>
681+
## Adding Custom Pennant Drivers
682+
683+
<a name="implementing-the-driver"></a>
684+
#### Implementing The Driver
685+
686+
If none of Pennant's existing storage drivers fit your application's needs, you may write your own storage driver. Your custom driver should implement the `Laravel\Pennant\Contracts\Driver` interface:
687+
688+
```php
689+
<?php
690+
691+
namespace App\Extensions;
692+
693+
use Laravel\Pennant\Contracts\Driver;
694+
695+
class RedisFeatureDriver implements Driver
696+
{
697+
public function define(string $feature, callable $resolver): void {}
698+
public function defined(): array {}
699+
public function getAll(array $features): array {}
700+
public function get(string $feature, mixed $scope): mixed {}
701+
public function set(string $feature, mixed $scope, mixed $value): void {}
702+
public function setForAllScopes(string $feature, mixed $value): void {}
703+
public function delete(string $feature, mixed $scope): void {}
704+
public function purge(array|null $features): void {}
705+
}
706+
```
707+
708+
Now, we just need to implement each of these methods using a Redis connection. For an example of how to implement each of these methods, take a look at the `Laravel\Pennant\Drivers\DatabaseDriver` in the [Pennant source code](https://github.com/laravel/pennant/blob/1.x/src/Drivers/DatabaseDriver.php)
709+
710+
> **Note**
711+
> Laravel does not ship with a directory to contain your extensions. You are free to place them anywhere you like. In this example, we have created an `Extensions` directory to house the `RedisFeatureDriver`.
712+
713+
<a name="registering-the-driver"></a>
714+
#### Registering The Driver
715+
716+
Once your driver has been implemented, you are ready to register it with Laravel. To add additional drivers to Pennant, you may use the `extend` method provided by the `Feature` facade. You should call the `extend` method from the `boot` method of one of your application's [service provider](/docs/{{version}}/providers):
717+
718+
```php
719+
<?php
720+
721+
namespace App\Providers;
722+
723+
use App\Extensions\RedisFeatureDriver;
724+
use Illuminate\Contracts\Foundation\Application;
725+
use Illuminate\Support\ServiceProvider;
726+
use Laravel\Pennant\Feature;
727+
728+
class AppServiceProvider extends ServiceProvider
729+
{
730+
/**
731+
* Register any application services.
732+
*/
733+
public function register(): void
734+
{
735+
// ...
736+
}
737+
738+
/**
739+
* Bootstrap any application services.
740+
*/
741+
public function boot(): void
742+
{
743+
Feature::extend('redis', function (Application $app) {
744+
return new RedisFeatureDriver($app->make('redis'), $app->make('events'), []);
745+
});
746+
}
747+
}
748+
```
749+
750+
Once the driver has been registered, you may use the `redis` driver in your application's `config/pennant.php` configuration file:
751+
752+
'stores' => [
753+
754+
'redis' => [
755+
'driver' => 'redis',
756+
'connection' => null,
757+
],
758+
759+
// ...
760+
761+
],
762+
678763
<a name="events"></a>
679764
## Events
680765

0 commit comments

Comments
 (0)