From bc9462256d89609f12932d80d1c1ef34993b03b1 Mon Sep 17 00:00:00 2001 From: fabio-ivona Date: Fri, 19 Apr 2024 14:55:13 +0000 Subject: [PATCH 1/5] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3173ff5..5840d01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `enum-features` will be documented in this file. +## v2.0.2 - 2024-04-19 + +**Full Changelog**: https://github.com/defstudio/enum-features/compare/v2.0.1...v2.0.2 + ## v2.0.1 - 2024-04-19 **Full Changelog**: https://github.com/defstudio/enum-features/compare/v2.0.0...v2.0.1 From 8d51331def824c84c5d6010e2feef33c0913a648 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 19 Apr 2024 17:06:34 +0200 Subject: [PATCH 2/5] fix pipelines --- README.md | 66 +++++++++++--------------------- src/Concerns/DefinesFeatures.php | 4 ++ 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index d89fb4a..f4d2732 100644 --- a/README.md +++ b/README.md @@ -37,29 +37,35 @@ 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 } ``` @@ -67,7 +73,7 @@ if(AppFeature::multi_language->enabled()){ or be disabled ```php -if(AppFeature::impersonate->disabled()){ +if(AppFeature::impersonate->inactive()){ throw(new Exception("Impersonate feature is not enabled")); } ``` @@ -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 diff --git a/src/Concerns/DefinesFeatures.php b/src/Concerns/DefinesFeatures.php index 1f6af7e..2c43382 100644 --- a/src/Concerns/DefinesFeatures.php +++ b/src/Concerns/DefinesFeatures.php @@ -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", From 5469b545b24ecb29a25462491d5471870fecac28 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 19 Apr 2024 17:07:35 +0200 Subject: [PATCH 3/5] fix --- README.md | 8 ++------ src/Concerns/DefinesFeatures.php | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f4d2732..13f2947 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,7 @@ enum AppFeature /* Feature resolution */ //with a single method: - protected function resolve(?Authenticatable $user = null) { - $user ??= auth()->user(); - + protected function resolve(Authenticatable $user = null) { match($this){ case self::multi_language => true, case self::impersonate => $user->isAdmin(), @@ -53,9 +51,7 @@ enum AppFeature //or with a dedicated method: - protected function resolveImpersonate(?Authenticatable $user = null){ - $user ??= auth()->user(); - + protected function resolveImpersonate(Authenticatable $user = null){ return $user->isSuperAdmin(); } } diff --git a/src/Concerns/DefinesFeatures.php b/src/Concerns/DefinesFeatures.php index 2c43382..aa28038 100644 --- a/src/Concerns/DefinesFeatures.php +++ b/src/Concerns/DefinesFeatures.php @@ -57,7 +57,7 @@ public function purge(): void Pennant::purge($this->featureName()); } - protected function resolve(?Authenticatable $scope = null): bool + protected function resolve(Authenticatable $scope = null): bool { $featureName = $this->featureName(); $camelFeatureName = str($this->featureName())->camel()->ucfirst(); From b962de94687bb7c3c2726d12fc32285354e1a4cd Mon Sep 17 00:00:00 2001 From: fabio-ivona Date: Fri, 19 Apr 2024 15:08:01 +0000 Subject: [PATCH 4/5] Fix styling --- src/Concerns/DefinesFeatures.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/DefinesFeatures.php b/src/Concerns/DefinesFeatures.php index aa28038..2c43382 100644 --- a/src/Concerns/DefinesFeatures.php +++ b/src/Concerns/DefinesFeatures.php @@ -57,7 +57,7 @@ public function purge(): void Pennant::purge($this->featureName()); } - protected function resolve(Authenticatable $scope = null): bool + protected function resolve(?Authenticatable $scope = null): bool { $featureName = $this->featureName(); $camelFeatureName = str($this->featureName())->camel()->ucfirst(); From 418572ca5a6b5bd7d248e50a2697423c227c681d Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 19 Apr 2024 17:09:55 +0200 Subject: [PATCH 5/5] fix --- README.md | 13 +++++++++++++ src/Concerns/DefinesFeatures.php | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 13f2947..5ac3331 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,19 @@ enum AppFeature } ``` +and should be registered in your Provider + +```php +class AppServiceProvider extends ServiceProvider +{ + //.. + + public function boot(): void { + AppFeature::defineFeatures(); + } +} +``` + then, in code, a feature could be checked to be enabled: diff --git a/src/Concerns/DefinesFeatures.php b/src/Concerns/DefinesFeatures.php index 2c43382..b07508b 100644 --- a/src/Concerns/DefinesFeatures.php +++ b/src/Concerns/DefinesFeatures.php @@ -136,6 +136,11 @@ public function forget(?Authenticatable $scope = null): void Pennant::forget($this->featureName()); } + public static function defineFeatures(): void + { + collect(self::cases())->each->define(); + } + /** * @param array $features */