Getting Started

Installation

This is installed as standard on new laravel projects.

Give the application access to the package in the PHP Packages application. Install with composer

scripts/composer.sh require alberon/features

Add an Enum to define the features used in the application

<?php

namespace App\Enums;

use BenSampo\Enum\Contracts\LocalizedEnum;
use BenSampo\Enum\Enum;

final class Feature extends Enum implements LocalizedEnum
{
    //--------------------------------------
    // Feature definitions
    //--------------------------------------
    // The values correspond to the string used in .env to enable the feature
    public const ExampleFeature = 'example-feature';
    public const ExampleOverriddenFeature = 'example-overridden-feature';
}

Add a service provider to override any features that need special handling:

<?php

namespace App\Providers;

use Alberon\Features\Support\Features;
use App\Enums\Feature;
use Illuminate\Support\ServiceProvider;

class FeatureServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        // app(Features::class)->override(Feature::ExampleOverriddenFeature, fn($state) => $state && request('with-feature'));
    }
}

Register the service provider in config/app.php

<?php

return [
    ...,
    'providers' => [
        App\Providers\FeatureServiceProvider::class,
    ],
];

Usage

To check whether a feature is enabled, use the check method:

if (app(\Alberon\Features\Support\Features::class)->check(\App\Enums\Feature::ExampleFeature)) {
    // feature enabled
} else {
    // feature disabled
}

To implement logic for a feature (e.g. if it should only be enabled for admins), use the override method:

app(\Alberon\Features\Support\Features::class)->override(\App\Enums\Feature::ExampleOverriddenFeature, function($enabled) {
    return $enabled && user()->hasRole('Admin');
});