Skip to content

Commit

Permalink
chore: readme redux
Browse files Browse the repository at this point in the history
  • Loading branch information
azuradara authored Feb 19, 2024
1 parent ee4603d commit 6567bfc
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Themes {
// meaning it can inject any dependencies it needs
}

// The feature defines its parameters here, can be anything
// The feature defines its context here, can be anything
// The return type is also customizable

public function resolve(Seller $seller): bool
Expand Down Expand Up @@ -51,4 +51,40 @@ Note that

#### Resolving features

Features are lazily resolved by default. To check whether a feature is active, you can use the Foggle feature manager like so:

```php
// Returns a boolean
make(Foggle::class)->for($seller)->active('themes');

// Returns the raw resolution
make(Foggle::class)->for($seller)->value('themes');

// you can alternatively use the helper function
foggle()->for($seller)->active('themes');
```

Note that `values()` will always return the result of the feature's `resolve()` method, but `active()` will return true if the value evaluates as truthy.

It is also possible to use a middleware to check a feature before the request reaches a controller:

```php
Route::name('themes.index')->get('/themes', ThemesIndexController::class)->middleware(['foggle:themes,themes.install']);
```

Depending on the configured driver, Foggle will also persist the result of every resolution and re-use it in future requests. This can be particularly useful when defining features that don't depend on a state, for example the themes feature would have a 50% chance of activating at random, instead of persisting that state manually and checking every time the feature resolves, Foggle does that for you.

#### Context resolvers

Instead of providing the context each time using `for()`, you can define custom context resolvers that are used when a context is not explicitly defined. For example, to use the current authenticated seller for any feature that uses a Seller entity as the context, you can do the following:

```php
// AppServiceProvider.php

foggle()->resolveContextUsing(Seller::class, fn () => auth()->user());
```





0 comments on commit 6567bfc

Please sign in to comment.