Skip to content

Commit

Permalink
fix pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Apr 19, 2024
1 parent bc94622 commit 8d51331
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
66 changes: 23 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,43 @@ enum AppFeature
case multi_language;
case impersonate;
case welcome_email;

/* Feature resolution */

//with a single method:
protected function resolve(?Authenticatable $user = null) {
$user ??= auth()->user();

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){
$user ??= auth()->user();

return $user->isSuperAdmin();
}
}
```

and each feature can then added to the Laravel application in its `configs/app.php` file:

```php
// config/app.php

return [
//..

'features' => [
AppFeature::multi_language,
AppFeature::welcome_email,
]
]

```

then, in code, a feature could be checked to be enabled:

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

or be disabled

```php
if(AppFeature::impersonate->disabled()){
if(AppFeature::impersonate->inactive()){
throw(new Exception("Impersonate feature is not enabled"));
}
```
Expand Down Expand Up @@ -96,32 +102,6 @@ In blade files, a feature can be checked with `@feature` directive:

```

### Customizing where and how to store enabled features

Enabled features are usually stored in config('app.features'), but this behaviour can be customized by
overriding the `enabledFeatures()` static method inside the enum class:


```php
use DefStudio\EnumFeatures\EnumFeatures;

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

case multi_language;
case impersonate;
case welcome_email;

public static function enabledFeatures(): array
{
return config('my_package.features', []); //or load from DB, or every other method
}
}

```
**note:** changing how enabled features are checked makes this package framework agnostic and it can be used in any php applicaiton

## Testing

```bash
Expand Down
4 changes: 4 additions & 0 deletions src/Concerns/DefinesFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ protected function resolve(?Authenticatable $scope = null): bool
$camelFeatureName = str($this->featureName())->camel()->ucfirst();

$try_methods = [
"resolve_$featureName",
"resolve_{$featureName}_feature",
"check_$featureName",
"check_{$featureName}_feature",
"has_$featureName",
"has_{$featureName}Feature",
"resolve$camelFeatureName",
"resolve{$camelFeatureName}Feature",
"check$camelFeatureName",
"check{$camelFeatureName}Feature",
"has$camelFeatureName",
Expand Down

0 comments on commit 8d51331

Please sign in to comment.