glhd / quartermaster

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

This package is auto-updated.

Last update: 2024-09-12 20:41:50 UTC


README

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

Quartermaster

Quartermaster 使使用 PHP 枚举Laravel Pennant 变得非常容易。

安装

composer require glhd/quartermaster

用法

EnumeratesFeatures 特性添加到任何枚举中,以便与 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');
    }
}

接下来,将您的枚举注册到 Pennant 中

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

然后,您可以直接从枚举中调用许多 Pennant 方法

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

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

对于许多检查,您可能需要一个作用域。您可以使用枚举上的 for() 方法来进行作用域检查

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();

与类特性一起使用

Pennant 已经提供了 基于类的特性。如果您想使用一些 Quartermaster 便利方法与这个 API 一起,您可以扩展 Glhd\Quartermaster\Feature

namespace App\Features;

use Glhd\Quartermaster\Feature;

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

然后,您可以从这个类静态调用大多数相同的方法

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

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