leettech/laravel-flagger

11,185 8
Install
composer require leettech/laravel-flagger
Latest Version:v2.1.0
Last Updated:Apr 20, 2018
Links: GitHub  ·  Packagist
Maintainer: josimar-lemos

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.

Related Packages

gabrieloliverio/laravel5-generators

Database metadata-based generators for Laravel 5

1
erirk/paypalpayment

laravel-paypalpayment is simple package help you process direct credit card paym...

0
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925
laravel/tinker

Powerful REPL for the Laravel framework.

489,754,436 7,444