### Install Package with Composer Source: https://github.com/defstudio/enum-features/blob/main/docs/2.installation.md Install the enum-features package using Composer. ```bash composer require defstudio/enum-features ``` -------------------------------- ### Run Package Tests Source: https://github.com/defstudio/enum-features/blob/main/README.md Execute the package's test suite using Composer. This command verifies the integrity and functionality of the library. ```bash composer test ``` -------------------------------- ### Apply Route Middleware for a Feature Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/5.middleware.md Apply the middleware generated by a feature flag to a specific route. Ensure the feature is correctly defined and accessible. ```php Route::get('/users/{user}/impersonate', function(){ // ... })->middleware(AppFeature::impersonate->middleware()); ``` -------------------------------- ### Activate a Feature Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/3.updating-values.md Use the `activate` method to set a feature to its active state. This can be done globally or for a specific scope, such as a user. ```php AppFeature::multi_language->activate(); ``` ```php AppFeature::multi_language->activate($user); ``` -------------------------------- ### Register Features in Service Provider Source: https://github.com/defstudio/enum-features/blob/main/docs/2.installation.md Register the defined enum features in your application's ServiceProvider's boot method. ```php class AppServiceProvider extends ServiceProvider { //.. public function boot(): void { AppFeature::defineFeatures(); } } ``` -------------------------------- ### Enforce a feature for the logged-in user Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Use the `enforce()` method to immediately throw a 403 exception if a feature is not active for the current user. This is useful for critical features. ```php AppFeature::impersonate->enforce(); //throws a 403 exception if not active ``` -------------------------------- ### Check if a feature is enabled Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Use the `active()` method to check if a feature is enabled for the currently logged-in user. This is useful for conditionally executing code. ```php if(AppFeature::multi_language->active()){ //.. multi language specific code } ``` -------------------------------- ### Check if all specified features are active Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Verify if all features in a given array are active for the current user using `areAllActive()`. Useful for ensuring all prerequisites are met. ```php if(AppFeature::areAllActive([AppFeature::multi_language, AppFeature::welcome_email])){ //.. } ``` -------------------------------- ### Enforce a feature for a specific user Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Enforce a feature for a specific user by passing the user object to the `enforce()` method. This will throw a 403 exception if the feature is not active for that user. ```php AppFeature::impersonate->enforce($user); //throws a 403 exception if not active ``` -------------------------------- ### Define Enum Features with Trait Source: https://github.com/defstudio/enum-features/blob/main/docs/2.installation.md Enable features on an enum by using the DefinesFeatures trait and implementing the resolve method. ```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(); } } ``` -------------------------------- ### Purge All Stored Feature Values Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/3.updating-values.md Use the `purge` method to remove all stored values for a feature across all scopes. This effectively resets the feature's stored state globally. ```php AppFeature::multi_language->purge(); ``` -------------------------------- ### Resolving Features with Dedicated Methods Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/1.feature-resolution.md Define specific `resolve[FeatureName]` methods for each feature to handle its enablement logic individually. The trait automatically uses these methods if they exist. If no dedicated method is found, the feature is considered disabled. ```php use DefStudio\EnumFeatures\EnumFeatures; use Illuminate\Contracts\Auth\Authenticatable; 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; } } ``` -------------------------------- ### Resolving Features with a Single Catch-All Method Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/1.feature-resolution.md Override the `resolve` method in your enum to provide a single logic for determining feature enablement based on the scope. This is useful when the resolution logic shares commonalities across features. ```php use DefStudio\EnumFeatures\EnumFeatures; use Illuminate\Contracts\Auth\Authenticatable; 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; } } } ``` -------------------------------- ### Check feature enablement for a specific user Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Check if a feature is active for a given user by passing the user object to the `active()` method. This allows for scope-specific feature checks. ```php $user = User::find(42); if(AppFeature::multi_language->active($user)){ //.. multi language specific code } ``` ```php if(AppFeature::multi_language->active($user)){ //.. multi language specific code } ``` -------------------------------- ### Check if some specified features are active Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Determine if at least one feature from a given array is active for the current user with `someAreActive()`. Useful for optional functionalities. ```php if(AppFeature::someAreActive([AppFeature::multi_language, AppFeature::welcome_email])){ //.. } ``` -------------------------------- ### Check if all specified features are inactive Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Ensure that none of the features in a given array are active for the current user using `areAllInactive()`. Useful for preventing conflicts. ```php if(AppFeature::areAllInactive([AppFeature::multi_language, AppFeature::welcome_email])){ //.. } ``` -------------------------------- ### Check if some specified features are inactive Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/2.feature-check.md Verify if at least one feature from a given array is inactive for the current user using `someAreInactive()`. Useful for fallback scenarios. ```php if(AppFeature::someAreInactive([AppFeature::multi_language, AppFeature::welcome_email])){ //.. } ``` -------------------------------- ### Check Feature Status with Enum Source: https://github.com/defstudio/enum-features/blob/main/docs/0.index.md Check if a feature defined by an Enum case is active using the `active()` method. This is useful for conditionally executing code blocks based on feature flags. ```php if(AppFeature::welcome_email->active()){ Mail::to($newUser)->send(new WelcomeEmail($newUser)); } ``` -------------------------------- ### Define Enum with Features Trait Source: https://github.com/defstudio/enum-features/blob/main/README.md Add the `DefinesFeatures` trait to your Enum to enable feature management. Features can be resolved using a single `resolve` method or dedicated `resolve` methods for specific features. ```php use DefStudio\EnumFeatures\EnumFeatures; enum AppFeature { use DefinesFeatures; // ← simply add this case multi_language; case impersonate; case welcome_email; /* Feature resolution */ //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(); } } ``` -------------------------------- ### Deactivate a Feature Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/3.updating-values.md Use the `deactivate` method to set a feature to its inactive state. This can be applied globally or to a specific scope. ```php AppFeature::multi_language->deactivate(); ``` ```php AppFeature::multi_language->deactivate($user); ``` -------------------------------- ### Forget Stored Feature Value Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/3.updating-values.md Use the `forget` method to remove the stored value for a feature for a specific scope. The feature will then resolve its value based on its definition. ```php AppFeature::multi_language->forget(); ``` ```php AppFeature::multi_language->forget($user); ``` -------------------------------- ### Conditional Rendering with @feature Directive Source: https://github.com/defstudio/enum-features/blob/main/docs/11.usage/4.blade-directive.md Use the @feature directive to conditionally render HTML content. The content within the directive will only be displayed if the specified feature is enabled. Ensure the feature is correctly defined in your application's feature configuration. ```blade @feature(AppFeature::multi_language) @endfeature ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.