glhd/quartermaster

dev-main 2024-09-12 20:41 UTC

This package is auto-updated.

Last update: 2024-10-12 20:54:40 UTC


README

Build Status Coverage Status Latest Stable Release MIT Licensed Follow @inxilpro on Twitter

Quartermaster

Quartermaster makes it easy to use PHP enums with Laravel Pennant.

Installation

composer require glhd/quartermaster

Usage

Add the EnumeratesFeatures trait to any enum to use it with Pennant:

enum BillingFeatures
{
    use EnumeratesFeatures;
    
    case NextGenerationPortal;
    case LegacyPortal;
    
    // Define a "resolver" for each feature:
    
    public function resolveNextGenerationPortal(Team $team)
    {
        return $team->owner->isEnrolledInBetaFeatures();
    }
    
    public function resolveLegacyPortal(Team $team)
    {
        return $team->created_at->lt('2022-06-01');
    }
}

Next, register your enum with Pennant:

// in a service provider's boot method:
BillingFeatures::register();

Then, you can call many Pennant methods from the enum directly:

if (BillingFeatures::NextGenerationPortal->active()) {
    // Show next-gen billing portal
}

if (BillingFeatures::NextGenerationPortal->inactive()) {
    // Show opt-in for beta features
}

For many checks, you may need a scope. You can use the for() method on the enum to do scoped checks:

if (BillingFeatures::LegacyPortal->for($team)->active()) {
    // Show legacy billing portal
}

// Enable next-gen portal for team
BillingFeatures::NextGenerationPortal->for($team)->activate();

// Disable next-gen portal for team
BillingFeatures::NextGenerationPortal->for($team)->deactivate();

// Reset flag status for team
BillingFeatures::NextGenerationPortal->for($team)->forget();

Using with class-based features

Pennant already offers class-based features. If you would like to use some of the Quartermaster convenience methods with this API, you can extend the Glhd\Quartermaster\Feature class:

namespace App\Features;

use Glhd\Quartermaster\Feature;

class NextGenerationBillingPortal extends Feature
{
    public function resolve(Team $team)
    {
        return $team->owner->isEnrolledInBetaFeatures();
    }
}

Then you can call most of the same methods statically from this class:

if (NextGenerationBillingPortal::active()) {
    // Show next-gen portal
}

if (NextGenerationBillingPortal::for($team)->active()) {
    // ...
}