PHP code example of michael-lurquin / feature-limiter

1. Go to this page and download the library: Download michael-lurquin/feature-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michael-lurquin / feature-limiter example snippets


FeatureLimiter::plan('starter')
    ->name('Starter')
    ->description('To begin with...')
    ->sort(0)
    ->active(true)
    ->monthly(9.99)
    ->yearly(129)
    ->save();

FeatureLimiter::plan('starter')
    ->name('Starter') // Optional (ucfirst on 'key' column : starter => Starter)
    ->save();

FeatureLimiter::plans([
    'free' => ['sort' => 0],
    'starter' => ['sort' => 1],
    'comfort' => ['sort' => 2],
    'pro' => ['name' => 'Gold', 'sort' => 3, 'description' => 'To begin with...', 'price_monthly' => 9.99, 'price_yearly' => 129],
    'enterprise' => ['sort' => 4, 'active' => false],
])->save();

FeatureLimiter::plans([
    'free',
    'starter',
    'comfort',
    'pro',
    'enterprise',
])->save();

FeatureLimiter::feature('sites')
    ->name('Sites')
    ->description('Number of sites you can create')
    ->group('create-design')
    ->type(FeatureType::INTEGER)
    ->unit('sites')
    ->reset(ResetPeriod::NONE) // none|daily|weekly|monthly|yearly
    ->sort(0)
    ->active(true)
    ->save();

FeatureLimiter::feature('sites')
    ->name('Sites') // Optional (ucfirst on 'key' column : sites => Sites)
    ->type(FeatureType::INTEGER)
    ->save();

FeatureLimiter::features([
    'sites' => ['sort' => 0, 'type' => FeatureType::INTEGER],
    'storage' => ['sort' => 1, 'type' => FeatureType::STORAGE],
    'custom_code' => ['name' => 'Custom Code', 'type' => FeatureType::BOOLEAN, 'sort' => 2],
])->save();

FeatureLimiter::features([
    'sites' => ['type' => FeatureType::INTEGER],
    'storage' => ['type' => FeatureType::STORAGE],
    'custom_code' => ['name' => 'Custom Code', 'type' => FeatureType::BOOLEAN],
])->save();

FeatureLimiter::grant('starter')
    ->feature('sites')
    ->quota(3);

FeatureLimiter::grant('starter')->feature('custom_code'); // enabled by default
FeatureLimiter::grant('starter')->feature('custom_code')->enabled();
FeatureLimiter::grant('starter')->feature('library')->disabled();

FeatureLimiter::grant('pro')->feature('storage')->unlimited();

->value(null)
->value(-1)
->value('unlimited')

FeatureLimiter::grant('starter')->features([
    'sites' => 3,
    'page' => 30,
    'custom_code' => false,
    'storage' => '1GB',
]);

FeatureLimiter::grant('pro')->features([
    'storage' => 'unlimited',
]);

FeatureLimiter::viewPlan('starter')->quota('sites');
FeatureLimiter::viewPlan('starter')->enabled('custom_code');
FeatureLimiter::viewPlan('starter')->disabled('custom_code');
FeatureLimiter::viewPlan('pro')->unlimited('storage');
FeatureLimiter::viewPlan('pro')->value('storage');

FeatureLimiter::for($billable)->quota('sites');
FeatureLimiter::for($billable)->enabled('custom_code');
FeatureLimiter::for($billable)->disabled('custom_code');
FeatureLimiter::for($billable)->unlimited('storage');
FeatureLimiter::for($billable)->value('storage');

FeatureLimiter::for($billable)->usage('sites');
FeatureLimiter::for($billable)->incrementUsage('sites');
FeatureLimiter::for($billable)->decrementUsage('sites');
FeatureLimiter::for($billable)->setUsage('sites', 10);
FeatureLimiter::for($billable)->clearUsage('sites');

FeatureLimiter::for($billable)->consume('sites', 1);
// returns false if not enough quota

FeatureLimiter::for($billable)->consume('storage', '500MB', strict: true);
// throws QuotaExceededException if quota is exceeded

FeatureLimiter::for($billable)->consumeOrFail('sites', 1);
FeatureLimiter::for($billable)->consumeManyOrFail([
    'sites' => 1,
    'storage' => '500MB',
]);

FeatureLimiter::for($billable)->refund('sites', 1);
FeatureLimiter::for($billable)->refundMany([
    'sites' => 1,
    'storage' => '500MB',
]);

FeatureLimiter::for($billable)->canConsume('sites', 1);
FeatureLimiter::for($billable)->canConsume('storage', '500MB');

FeatureLimiter::for($billable)->remainingQuota('sites');

FeatureLimiter::for($billable)->exceededQuota('sites', 1);

FeatureLimiter::for($billable)->canConsumeMany([
    'sites' => 1,
    'storage' => '500MB',
]);

FeatureLimiter::for($billable)->remainingQuotaMany([
    'sites',
    'storage',
]);

interface BillingProvider {
    public function resolvePlanFor(mixed $billable): ?Plan;
    public function pricesFor(Plan $plan): array;
}

$billable = new class {
    public int $id = 1;
};

ResetPeriod::NONE     // lifetime
ResetPeriod::DAILY
ResetPeriod::WEEKLY
ResetPeriod::MONTHLY
ResetPeriod::YEARLY

$cards = FeatureLimiter::catalog()->plansCards(
    featured: ['sites', 'pages', 'storage', 'custom_code'],
    onlyActivePlans: true
);

$table = FeatureLimiter::catalog()->comparisonTable(
    onlyActivePlans: true,
    onlyActiveFeatures: true
);

FeatureLimiter::catalog()->plansCards(['site', 'page', 'storage', 'banner', 'cms', 'collection']);
FeatureLimiter::catalog()->comparisonTable();

FeatureLimiter::catalog()
    ->', 'storage', 'banner', 'cms', 'collection']);

FeatureLimiter::catalog()
    ->

FeatureLimiter::viewPlan('free');

FeatureLimiter::viewPlan('free')->prices();

FeatureLimiter::for($billable)->plan();

FeatureLimiter::for($billable)->plan()->prices();

$schedule->command('feature-limiter:prune-usages --months=12')->daily();
bash
php artisan vendor:publish --tag=feature-limiter-config
php artisan migrate
bash
php artisan feature-limiter:prune-usages --months=12
bash
php artisan feature-limiter:prune-usages --days=90 --dry-run