|
7 | 7 | - [Class Based Features](#class-based-features)
|
8 | 8 | - [Checking Features](#checking-features)
|
9 | 9 | - [Conditional Execution](#conditional-execution)
|
| 10 | + - [The `HasFeatures` Trait](#the-has-features-trait) |
10 | 11 | - [Blade Directive](#blade-directive)
|
11 | 12 | - [Middleware](#middleware)
|
12 | 13 | - [In-Memory Cache](#in-memory-cache)
|
@@ -178,6 +179,25 @@ return Feature::for($user)->active('new-api')
|
178 | 179 | : $this->resolveLegacyApiResponse($request);
|
179 | 180 | ```
|
180 | 181 |
|
| 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 | + |
181 | 201 | > **Note**
|
182 | 202 | > 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.
|
183 | 203 |
|
@@ -212,25 +232,6 @@ class PodcastController
|
212 | 232 | }
|
213 | 233 | ```
|
214 | 234 |
|
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 |
| - |
234 | 235 | <a name="conditional-execution"></a>
|
235 | 236 | ### Conditional Execution
|
236 | 237 |
|
@@ -268,6 +269,63 @@ The `unless` method serves as the inverse of the `when` method, executing the fi
|
268 | 269 | fn () => $this->resolveNewApiResponse($request),
|
269 | 270 | );
|
270 | 271 |
|
| 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 | + |
271 | 329 | <a name="blade-directive"></a>
|
272 | 330 | ### Blade Directive
|
273 | 331 |
|
|
0 commit comments