Skip to content

Commit d04f7d5

Browse files
committed
re-organise and rewrite some of the readme
1 parent b5e0b46 commit d04f7d5

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

README.md

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
[![Test](https://github.com/codinglabsau/laravel-feature-flags/actions/workflows/run-tests.yml/badge.svg)](https://github.com/codinglabsau/laravel-feature-flags/actions/workflows/run-tests.yml)
55
[![Total Downloads](https://img.shields.io/packagist/dt/codinglabsau/laravel-feature-flags.svg?style=flat-square)](https://packagist.org/packages/codinglabsau/laravel-feature-flags)
66

7-
This package allows instant, zero-deployment toggling of application features.
7+
Laravel Feature Flags allows instant, zero-deployment toggling of application features.
88

9-
Individual features can be in one of three states:
9+
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.
10+
11+
Each feature can be in one of three states:
1012
- On: enabled for everyone
1113
- Off: disabled for everyone
12-
- Dynamic: evaluated according to a custom or default callback
14+
- Dynamic: evaluated according to a feature-specific closure (with a fallback option)
1315

1416
___
1517
## Installation
@@ -40,48 +42,21 @@ If you wish to change to a different cache driver, update your `.env`:
4042
FEATURES_CACHE_STORE=file
4143
```
4244

43-
### Use Your Own Model
44-
To use your own model, update the config and replace the existing reference with your own model:
45-
46-
```php
47-
// app/config/feature-flags.php
48-
49-
'feature_model' => \App\Models\Feature::class,
50-
```
51-
52-
Make sure to also cast the state column to a feature state enum using the `FeatureStateCast`:
53-
54-
```php
55-
// app/Models/Feature.php
56-
57-
use Codinglabs\FeatureFlags\Casts\FeatureStateCast;
58-
59-
protected $casts = [
60-
'state' => FeatureStateCast::class
61-
];
62-
```
63-
6445
## Usage
65-
Create a new feature in the database and give it a default state:
46+
Create a new feature in the database and set the initial state:
6647
```php
48+
use Codinglabs\FeatureFlags\Models\Feature;
49+
use Codinglabs\FeatureFlags\Enums\FeatureState;
50+
6751
Feature::create([
6852
'name' => 'search-v2',
69-
'state' => Codinglabs\FeatureFlags\Enums\FeatureState::on()
53+
'state' => FeatureState::on()
7054
]);
7155
```
7256

7357
Its recommended that you seed the features to your database before a new deployment or as soon as possible after a deployment.
7458

75-
A feature can be in one of three states:
76-
```php
77-
use Codinglabs\FeatureFlags\Enums\FeatureState;
78-
79-
FeatureState::on()
80-
FeatureState::off()
81-
FeatureState::dynamic()
82-
```
8359
### Check If A Feature Is Enabled
84-
8560
#### Blade View
8661
```php
8762
@feature('search-v2')
@@ -103,7 +78,6 @@ if (FeatureFlag::isOn('search-v2')) {
10378
```
10479

10580
### Updating A Features State
106-
10781
To change a features state you can call the following methods:
10882
```php
10983
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
@@ -121,7 +95,7 @@ It is recommended that you only update a features state using the above methods
12195
```php
12296
\Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent::class
12397
```
124-
An example use case of the feature updated event would be if you were caching the result of a dynamic handler and need to clear that cache when a feature is updated.
98+
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.
12599

126100
___
127101
## Advanced Usage
@@ -131,12 +105,12 @@ A dynamic handler can be defined in the `boot()` method of your `AppServiceProvi
131105
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
132106

133107
FeatureFlag::registerDynamicHandler('search-v2', function ($feature, $request) {
134-
return $request->user() && $request->user()->hasRole('Tester')
108+
return $request->user() && $request->user()->hasRole('Tester');
135109
});
136110
```
137111
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.
138112

139-
Each handler is provided with the features name and current request as arguments and must return a bool value.
113+
Each handler is provided with the features name and current request as arguments and must return a boolean value.
140114

141115
### Default Handler For Dynamic Features
142116
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:
@@ -160,25 +134,38 @@ FeatureFlag::handleMissingFeaturesWith(function ($feature) {
160134

161135
If a handler for missing features has been defined then an exception will **not** be thrown and the feature will resolve to `off`.
162136

163-
### Sharing features with UI (Inertiajs example)
137+
### Using Your Own Model
138+
To use your own model, update the config and replace the existing reference with your own model:
139+
164140
```php
165-
// config/app.php
141+
// app/config/feature-flags.php
166142

167-
'features' => [
168-
[
169-
'name' => 'search-v2',
170-
'state' => \Codinglabs\FeatureFlags\Enums\FeatureState::dynamic()
171-
]
172-
],
143+
'feature_model' => \App\Models\Feature::class,
173144
```
174145

146+
Make sure to also cast the state column to a feature state enum using the `FeatureStateCast`:
147+
148+
```php
149+
// app/Models/Feature.php
150+
151+
use Codinglabs\FeatureFlags\Casts\FeatureStateCast;
152+
153+
protected $casts = [
154+
'state' => FeatureStateCast::class
155+
];
156+
```
157+
158+
### Sharing features with UI (Inertiajs example)
175159
```php
176160
// app/Middleware/HandleInertiaRequest.php
177161

162+
use Codinglabs\FeatureFlags\FeatureFlags;
163+
use Codinglabs\FeatureFlags\Models\Feature;
164+
178165
Inertia::share([
179166
'features' => function () {
180-
return collect(config('app.features'))
181-
->filter(fn ($feature) => FeatureFlag::isOn($feature['name']))
167+
return Feature::all()
168+
->filter(fn ($feature) => FeatureFlags::isOn($feature['name']))
182169
->pluck('name');
183170
}
184171
]);
@@ -195,6 +182,7 @@ Vue.mixin({
195182
}
196183
})
197184
```
185+
198186
```html
199187
<!-- SomeComponent.vue -->
200188

0 commit comments

Comments
 (0)