leettech / laravel-flagger by josimar-lemos

8,376
8
10
Package Data
Maintainer Username: josimar-lemos
Maintainer Contact: lemosxd01@gmail.com (Josimar Lemos)
Package Create Date: 2017-08-21
Package Last Update: 2018-04-20
Home Page:
Language: PHP
License: Unknown
Last Refreshed: 2025-02-06 03:01:25
Package Statistics
Total Downloads: 8,376
Monthly Downloads: 27
Daily Downloads: 0
Total Stars: 8
Total Watchers: 10
Total Forks: 0
Total Open Issues: 3

Laravel Flagger

Flagger is a package that has been designed to help you on enabling feature flags in Laravel projects.

Version Compatibility

Laravel | Flagger :---------|:---------- 5.3.x | 1.x.x 5.4.x | 2.x.x

Installation

To install through composer, simply add the following in your composer.json file:

{
    "require": {
        "leettech/laravel-flagger": "~2.0"
    }
}

And then run composer install.

Quick Installation

The above installation can also be simplified by using the following command:

composer require "leettech/laravel-flagger=~2.0"

Configuration

After installing the Flagger package, register the FlaggerServiceProvider in your config/app.php configuration file:

'providers' => [
    // Other service providers...
    Leet\Providers\FlaggerServiceProvider::class,
],

Also, add the Flagger facade to the aliases array in your app configuration file:

'aliases' => [
    // Other aliases...
    'Flagger' => Leet\Facades\Flagger::class,
],

Then run the migration script to create features and flaggables tables:

php artisan migrate

Publish the package configuration:

php artisan vendor:publish --provider="Leet\Providers\FlaggerServiceProvider"

And, in your config/flagger.php configuration file, specify which model will have feature flags associated to it (by default it's set to App\User::class).

Usage

First of all, make sure you have inserted your features in the features table in the database. You can use the model Leet\Models\Feature for this:

\Leet\Models\Feature::create([
    'name' => 'notifications',
    'description' => 'Notifications feature'
]);

flag

Use \Flagger::flag($flaggable, $feature) to attach a feature to a model:

$user = \App\User::first();
\Flagger::flag($user, 'notifications');

You can also add Leet\Models\FlaggerTrait to the model in order to make flagger methods available from it:

class User extends Model
{
    use \Leet\Models\FlaggerTrait;
}
$user = \App\User::first();
$user->flag('notifications');

flagMany

Use \Flagger::flagMany($flaggables, $feature) to attach a feature to a collection of models:

$users = \App\User::all();
\Flagger::flagMany($users, 'notifications');

hasFeatureEnabled

Anywhere in the application, you can check if a user has access to a feature:

if ($user->hasFeatureEnabled('notifications')) {
    doSomething();
}

FlaggerMiddleware

To use the FlaggerMiddleware, you have to declare it in the application kernel:

protected $routeMiddleware = [
    // Other middleware...
    'flagger' => \Leet\Middleware\FlaggerMiddleware::class,
];

And on any authenticated route:

Route::get('notifications', 'NotificationsController@index')->middleware('flagger:notifications');

or

Route::group(['middleware' => 'flagger:notifications'], function () {
    Route::get('notifications', 'NotificationsController@index');
    Route::post('notifications', 'NotificationsController@store')
});

Getting enabled features for a model

By adding Leet\Models\FlaggerTrait to your model, you are able to access its enabled features:

// returns the features a user have access to
$user->features;

Flagger command

The flagger command accepts an integer, array, or a path to a csv containing a list of integers and adds a flag to each of them:

php artisan flagger notifications 1
// OR
php artisan flagger notifications 1 2 3
// OR
php artisan flagger notifications users.csv
// OR
php artisan flagger notifications users.csv --chunk=100

Be sure to create the flag before attempting to add it to any entity.