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
Copy file name to clipboardExpand all lines: README.md
+97-86Lines changed: 97 additions & 86 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,97 +1,63 @@
1
1
# Dynamic feature flags for laravel.
2
2
3
3
[](https://packagist.org/packages/codinglabsau/laravel-feature-flags)
This package offers the ability to implement feature flags throughout your codebase allowing you to easily toggle parts of your application. Features are database driven which will allow you to easily configure them via a command or build a front end to manage their states. Here's an example of how they could be used:
9
-
10
-
```php
11
-
@feature('search-v2')
12
-
// new search goes here
13
-
@else
14
-
// legacy search here
15
-
@endfeature
16
-
```
17
-
And in your codebase:
18
-
```php
19
-
FeatureFlag::isEnabled('search-v2') // true
20
-
```
7
+
This package offers the ability to implement feature flags in your application which can be easily toggled on or off. You can also set a feature to a dynamic state where you can define custom rules around whether that feature is enabled or not.
Note that under the hood this package uses the `rememberForever()` method for caching and that if you are using the `Memcached` driver, items that are stored "forever" may be removed when the cache reaches its size limit.
73
37
74
-
##Usage
38
+
### Use Your Own Model
75
39
76
-
### Basic Setup
40
+
To use your own model, update the config and replace the existing reference with your own model:
77
41
78
-
###Migrations
79
-
Make sure you have published the migrations as the `features` table is required:
Each features state will be cached on access which means it won't be calling the database every time a feature is being checked. You can configure the cache store by publishing the config:
Make sure to also cast the state column to a feature state enum using the `FeatureStateCast`:
49
+
91
50
```php
92
-
FEATURES_CACHE_STORE=redis
51
+
// app/Models/Feature.php
52
+
53
+
use Codinglabs\FeatureFlags\Casts\FeatureStateCast;
54
+
55
+
protected $casts = [
56
+
'state' => FeatureStateCast::class
57
+
];
93
58
```
94
-
Note that this package uses the `rememberForever()` method and that if you are using the `Memcached` driver, items that are stored "forever" may be removed when the cache reaches its size limit.
59
+
60
+
## Usage
95
61
96
62
Create a new feature in the database and give it a default state:
It is recommended that you only update a features state using the above methods as it will take care of updating the cache.
157
+
It is recommended that you only update a features state using the above methods as it will take care of updating the cache and dispatching the feature updated event:
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.
163
+
164
+
___
165
+
## Advanced Usage
147
166
148
167
### Dynamic Features
149
168
150
-
When a features state is in the dynamic state it will look for a dynamic handler to determine whether that feature is enabled or not. A dynamic handler can be defined in the `boot()` method of your `AppServiceProvider`:
169
+
A dynamic handler can be defined in the `boot()` method of your `AppServiceProvider`:
151
170
```php
152
171
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
153
172
154
173
FeatureFlag::registerDynamicHandler('search-v2', function ($feature, $request) {
Each handler is given the feature name and the current request as arguments and must return a bool.
177
+
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.
178
+
179
+
Each handler is provided with the features name and current request as arguments and must return a bool value.
180
+
181
+
### Default Handler For Dynamic Features
182
+
183
+
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:
159
184
160
-
#### Default Handler For Dynamic Features
161
-
You may also define a default dynamic handler which will be the catch-all dynamic handler for features that don't have an explicit handler defined for them:
When a feature is in the dynamic state it will look for an explicit handler for that feature first. If it can't find a handler and a default handler has been defined it will use that instead. If it can't find any handlers the feature will resolve to `off` by default.
168
190
169
-
### Events
170
-
#### Updated
171
-
After a feature has been updated an event will be dispatched:
This can be used to create a listener that could for example handle clearing any custom cache data created by dynamic handlers.
191
+
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.
176
192
177
193
### Handle Missing Features
194
+
178
195
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:
0 commit comments