DEF STUDIO srl

Feature Resolution

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

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

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)
Suggest a change
Last updated 26 April 2024