Skip to content

Commit 5ada868

Browse files
committed
document has features
1 parent fee9726 commit 5ada868

File tree

1 file changed

+77
-19
lines changed

1 file changed

+77
-19
lines changed

pennant.md

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Class Based Features](#class-based-features)
88
- [Checking Features](#checking-features)
99
- [Conditional Execution](#conditional-execution)
10+
- [The `HasFeatures` Trait](#the-has-features-trait)
1011
- [Blade Directive](#blade-directive)
1112
- [Middleware](#middleware)
1213
- [In-Memory Cache](#in-memory-cache)
@@ -178,6 +179,25 @@ return Feature::for($user)->active('new-api')
178179
: $this->resolveLegacyApiResponse($request);
179180
```
180181

182+
Pennant also offers some additional convenience methods that may prove useful when determining if a feature is active or not:
183+
184+
```php
185+
// Determine if all of the given features are active...
186+
Feature::allAreActive(['new-api', 'site-redesign']);
187+
188+
// Determine if any of the given features are active...
189+
Feature::someAreActive(['new-api', 'site-redesign']);
190+
191+
// Determine if a feature is inactive...
192+
Feature::inactive('new-api');
193+
194+
// Determine if all of the given features are inactive...
195+
Feature::allAreInactive(['new-api', 'site-redesign']);
196+
197+
// Determine if any of the given features are inactive...
198+
Feature::someAreInactive(['new-api', 'site-redesign']);
199+
```
200+
181201
> **Note**
182202
> 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.
183203
@@ -212,25 +232,6 @@ class PodcastController
212232
}
213233
```
214234

215-
Pennant also offers some additional convenience methods that may prove useful when determining if a feature is active or not:
216-
217-
```php
218-
// Determine if all of the given features are active...
219-
Feature::allAreActive(['new-api', 'site-redesign']);
220-
221-
// Determine if any of the given features are active...
222-
Feature::someAreActive(['new-api', 'site-redesign']);
223-
224-
// Determine if a feature is inactive...
225-
Feature::inactive('new-api');
226-
227-
// Determine if all of the given features are inactive...
228-
Feature::allAreInactive(['new-api', 'site-redesign']);
229-
230-
// Determine if any of the given features are inactive...
231-
Feature::someAreInactive(['new-api', 'site-redesign']);
232-
```
233-
234235
<a name="conditional-execution"></a>
235236
### Conditional Execution
236237

@@ -268,6 +269,63 @@ The `unless` method serves as the inverse of the `when` method, executing the fi
268269
fn () => $this->resolveNewApiResponse($request),
269270
);
270271

272+
<a name="the-has-features-trait"></a>
273+
### The `HasFeatures` Trait
274+
275+
Pennant's `HasFeatures` trait may be added to your application's `User` model (or any other model that has features) to provide a fluent, convenient way to check features directly from the model:
276+
277+
```php
278+
<?php
279+
280+
namespace App\Models;
281+
282+
use Illuminate\Foundation\Auth\User as Authenticatable;
283+
use Laravel\Pennant\Concerns\HasFeatures;
284+
285+
class User extends Authenticatable
286+
{
287+
use HasFeatures;
288+
289+
// ...
290+
}
291+
```
292+
293+
Once the trait has been added to your model, you may easily check features by invoking the `features` method:
294+
295+
```php
296+
if ($user->features()->active('new-api')) {
297+
// ...
298+
}
299+
```
300+
301+
Of course, the `features` method provides access to many other convenient methods for interacting with features:
302+
303+
```php
304+
// Values...
305+
$value = $user->features()->value('purchase-button')
306+
$values = $user->features()->values(['new-api', 'purchase-button']);
307+
308+
// State...
309+
$user->features()->active('new-api');
310+
$user->features()->allAreActive(['new-api', 'server-api']);
311+
$user->features()->someAreActive(['new-api', 'server-api']);
312+
313+
$user->features()->inactive('new-api');
314+
$user->features()->allAreInactive(['new-api', 'server-api']);
315+
$user->features()->someAreInactive(['new-api', 'server-api']);
316+
317+
// Conditional execution...
318+
$user->features()->when('new-api',
319+
fn () => /* ... */,
320+
fn () => /* ... */,
321+
);
322+
323+
$user->features()->unless('new-api',
324+
fn () => /* ... */,
325+
fn () => /* ... */,
326+
);
327+
```
328+
271329
<a name="blade-directive"></a>
272330
### Blade Directive
273331

0 commit comments

Comments
 (0)