Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Apr 26, 2024
1 parent bc7696c commit 8d75bf6
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 45 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
build
composer.lock
coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
Expand Down
32 changes: 2 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,9 @@ if(AppFeature::multi_language->active()){
}
```

or be disabled
An extensive documentation is available at

```php
if(AppFeature::impersonate->inactive()){
throw(new Exception("Impersonate feature is not enabled"));
}
```

or enforce it

```php

AppFeature::impersonate->enforce(); //throws "Feature [impersonate] is not enabled"

```

### Blade directives

In blade files, a feature can be checked with `@feature` directive:

```html

@feature(AppFeature::multi_language)
<select name="language" xmlns="http://www.w3.org/1999/html">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select>
@endfeature

```
https://docs.defstudio.it/enum-features

## Testing

Expand Down
30 changes: 30 additions & 0 deletions docs/0.index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: 'Introduction'
description: 'A simple trait to enable Laravel Pennant features using Enums'
navigation.title: 'Introduction'
---

# Laravel Enum Features

<a href="https://packagist.org/packages/defstudio/enum-features" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/packagist/v/defstudio/enum-features.svg?style=flat&cacheSeconds=3600" alt="Latest Version on Packagist"></a>
<a href="https://github.com/defstudio/enum-features/actions/workflows/run-tests.yml" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/github/actions/workflow/status/defstudio/enum-features/run-tests.yml?branch=main&label=tests&cacheSeconds=3600&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMV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IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=" alt="Tests"></a>
<a href="https://github.com/defstudio/enum-features/actions/workflows/fix-php-code-style-issues.yml" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/github/actions/workflow/status/defstudio/enum-features/fix-php-code-style-issues.yml?branch=main&label=code%20style&cacheSeconds=3600" alt="Code Style"></a>
<a href="https://github.com/defstudio/enum-features/actions/workflows/phpstan.yml" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/github/actions/workflow/status/defstudio/enum-features/phpstan.yml?branch=main&label=phpstan&cacheSeconds=3600" alt="Static Analysis"></a>
<a href="https://packagist.org/packages/defstudio/enum-features" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/packagist/dt/defstudio/enum-features.svg?style=flat&cacheSeconds=3600" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/defstudio/enum-features" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" src="https://img.shields.io/packagist/l/defstudio/enum-features?style=flat&cacheSeconds=3600" alt="License"></a>
<a href="https://twitter.com/FabioIvona?ref_src=twsrc%5Etfw" target="_blank"><img style="display: inline-block; margin-top: 0.5em; margin-bottom: 0.5em" alt="Twitter Follow" src="https://img.shields.io/twitter/follow/FabioIvona?label=Follow&style=social"></a>


#### A simple trait to enable Laravel Pennant features using Enums


```php
if(AppFeature::welcome_email->active()){
Mail::to($newUser)->send(new WelcomeEmail($newUser));
}
```

[replace:full-source-code]


[replace:powered-by]
1 change: 1 addition & 0 deletions docs/1.about-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:about-us]
66 changes: 66 additions & 0 deletions docs/11.usage/1.feature-resolution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: 'Feature Resolution'
description: ''
---

## Feature Resolution

On the first check for each scope, Laravel Pennant requires you to resolve if the feature is enabled for that scope. This can be done in your enum code:

### Single Catch-All Method

The resolve method can be overridden in order to return `true`/`false` for that user

```php
use DefStudio\EnumFeatures\EnumFeatures;

enum AppFeature
{
use DefinesFeatures;

case multi_language;
case impersonate;
case welcome_email;

protected function resolve(Authenticatable $user = null) {
match($this){
case self::multi_language => true,
case self::impersonate => $user->isAdmin(),
default => false;
}
}
}
```

### A dedicated method for each feature

The `DefineFeatures` trait can check if a `resolve[FeatureName]` method exists, and will use it to determine if the feature is enabled.

> [!WARNING]
> The feature is considered to be disabled if no `resolve[FeatureName]` method is defined for it
```php
use DefStudio\EnumFeatures\EnumFeatures;

enum AppFeature
{
use DefinesFeatures;

case multi_language;
case impersonate;
case welcome_email;

protected function resolveImpersonate(Authenticatable $user = null){
return $user->isSuperAdmin();
}

protected function resolveWelcomeEmail(Authenticatable $user = null){
return true;
}
}
```

> [!NOTE]
> the trait will search for any method with these patterns:
> - `resolve[FeatureName]` (camel case)
> - `resolve_[feature_name]` (snake case)
96 changes: 96 additions & 0 deletions docs/11.usage/2.feature-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: 'Feature Check'
description: ''
---

## Feature Check

In code feature can be checked in multiple ways for the logged user:

### Check if enabled

```php
if(AppFeature::multi_language->active()){
//.. multi language specific code
}
```

### Check if disabled

```php
if(AppFeature::multi_language->active()){
//.. multi language specific code
}
```

### Enforced

```php

AppFeature::impersonate->enforce(); //throws a 403 exception if not active

```

## Feature Check for a given scope

Each check can be applied to a scope different from the logged user:

```php

$user = User::find(42);

if(AppFeature::multi_language->active($user)){
//.. multi language specific code
}

if(AppFeature::multi_language->active($user)){
//.. multi language specific code
}

AppFeature::impersonate->enforce($user); //throws a 403 exception if not active

```

## Multiple feature checks

This package can check for multiple feature at once as well:

### All Active

```php

if(AppFeature::areAllActive([AppFeature::multi_language, AppFeature::welcome_email])){
//..
}

```

### Some Active

```php

if(AppFeature::someAreActive([AppFeature::multi_language, AppFeature::welcome_email])){
//..
}

```

### All Inactive

```php

if(AppFeature::areAllInactive([AppFeature::multi_language, AppFeature::welcome_email])){
//..
}

```

### Some Inactive

```php

if(AppFeature::someAreInactive([AppFeature::multi_language, AppFeature::welcome_email])){
//..
}

```
41 changes: 41 additions & 0 deletions docs/11.usage/3.updating-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: 'Updating Values'
description: ''
---

## Updating Values

Laravel Pennant stores the value resolved for a given scope, in order to change the stored value this package offers some useful methods:

### Activate a feature

```php
AppFeature::multi_language->activate();

//also for a given scope
AppFeature::multi_language->activate($user);
```

### Deactivate a feature

```php
AppFeature::multi_language->deactivate();

//also for a given scope
AppFeature::multi_language->deactivate($user);
```

### Forget stored value

```php
AppFeature::multi_language->forget();

//also for a given scope
AppFeature::multi_language->forget($user);
```

### Forget for all scopes

```php
AppFeature::multi_language->purge();
```
20 changes: 20 additions & 0 deletions docs/11.usage/4.blade-directive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: 'Blade Directive'
description: ''
---

## Blade Directive

To help checking features in blade code, this package offers a `@feature` directive

```html

@feature(AppFeature::multi_language)
<select name="language" xmlns="http://www.w3.org/1999/html">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select>
@endfeature

```
16 changes: 16 additions & 0 deletions docs/11.usage/5.middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: 'Middleware'
description: ''
---

## Middleware

Features can be enforced using Laravel Pennants [middlewares](https://laravel.com/docs/11.x/pennant#middleware)

```php

Route::get('/users/{user}/impersonate', function(){
// ...
})->middleware(AppFeature::impersonate->middleware());

```
1 change: 1 addition & 0 deletions docs/17.community/1.contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:contributing]
1 change: 1 addition & 0 deletions docs/17.community/2.releases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:releases]
1 change: 1 addition & 0 deletions docs/17.community/3.changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:changelog]
57 changes: 57 additions & 0 deletions docs/2.installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Installation & Configuration
menuTitle: 'Installation'
---

You can install the package via composer:

``` bash
composer require defstudio/enum-features
```

## Usage

Features can be enabled on any enum by using the `DefinesFeatures` trait:

```php
use DefStudio\EnumFeatures\EnumFeatures;

enum AppFeature
{
use DefinesFeatures; // ← simply add this

case multi_language;
case impersonate;
case welcome_email;

/* And add your feature resolution code */

//with a single method:
protected function resolve(Authenticatable $user = null) {
match($this){
case self::multi_language => true,
case self::impersonate => $user->isAdmin(),
default => false;
}
}

//or with a dedicated method:

protected function resolveImpersonate(Authenticatable $user = null){
return $user->isSuperAdmin();
}
}
```

and just register the features in a ServiceProvider `boot()` method:

```php
class AppServiceProvider extends ServiceProvider
{
//..

public function boot(): void {
AppFeature::defineFeatures();
}
}
```
1 change: 1 addition & 0 deletions docs/3.issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:issues]
1 change: 1 addition & 0 deletions docs/4.support-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[replace:support-us]
Binary file added docs/img/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8d75bf6

Please sign in to comment.