Coder Social home page Coder Social logo

exolnet / laravel-bento Goto Github PK

View Code? Open in Web Editor NEW
7.0 6.0 1.0 235 KB

Organize feature launches by custom user segments.

License: MIT License

PHP 99.86% Blade 0.14%
php laravel laravel-package ab-testing feature-flags continuous-delivery rollout hacktoberfest

laravel-bento's Introduction

Laravel Bento

Latest Stable Version Software License Build Status Total Downloads

Bento helps you organize feature launches by custom user segments. Create and organize rules to make features available to certain users.

Define your features, define your segmentation strategies and let Bento launch each feature to the right people. Bento can also help you run A/B testing on your applications.

The core concepts of this library are inspired by Airbnb's Trebuchet project for Ruby.

Installation

Require this package with composer:

composer require eXolnet/laravel-bento

After installing Bento, publish its example service provider to hold your feature definitions:

php artisan vendor:publish --tag=bento-provider

Then, add it to the providers array in config/app.php:

App\Providers\BentoServiceProvider::class

Usage

Create Features

Define features and their launch segmentation strategies. You can define one strategy with the feature method:

Bento::feature('feature')->visitorPercent(10);

Or you can combine multiple strategies:

Bento::feature('feature')->visitorPercent(10)->hostname('example.com');

Your features could be grouped in the boot method of a service provider:

<?php

namespace App\Providers;

use Exolnet\Bento\Facades\Bento;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * @return void
     */
    public function boot(): void
    {
        Bento::feature('foo')->everyone();
        Bento::feature('bar')->everyone();
    }
}

Launch Your Features

You can check if a feature is launched for a visitor with the launch method:

if (Bento::launch('feature')) {
    //
}

Or check that a feature is awaiting launch:

if (Bento::await('feature')) {
    //
}

Blade

In Blade templates, handy macros are also available:

@launch('feature')
    Feature is launched!
@else
    Coming soon!
@endlaunch
@await('feature')
    Coming soon!
@else
    Feature is launched!
@endawait

Middleware

Since some strategy requires the request context to be evaluated, it's recommended to use middleware to limit a route:

  1. Add the following middleware in the $routeMiddleware of your application's HTTP Kernel:
    protected $routeMiddleware = [
        // ...
        'await' => \Exolnet\Bento\Middleware\Await::class,
        'launch' => \Exolnet\Bento\Middleware\Launch::class,
        // ...
    ];
  1. Then, you could use them to restrict your routes:
Route::middleware('launch:feature')->group(function () {
    //
});
Route::middleware('await:feature')->group(function () {
    //
});

Basic Segmentation Strategies

The following segmentation strategies are available to help quickly target your users:

  • Callback
  • Config
  • Date
  • Environment
  • Everyone
  • Guest
  • Hostname
  • Nobody
  • Stub
  • User (authenticated or specific user IDs)
  • User Percent (a fraction of all connected visitors)
  • Visitor Percent (a fraction of all your visitors)

Logic Segmentation Strategies

Additional logic segmentation strategies are available to help target your users with more complex rules.

Not

Bento::feature('feature')->not->everyone();

All

use \Exolnet\Bento\Strategy\AimsStrategies;

Bento::feature('feature')->all(function (AimsStrategies $aims) {
    $aims
        ->environment('production')
        ->visitorPercent(20);
});

Any

use \Exolnet\Bento\Strategy\AimsStrategies;

Bento::feature('feature')->any(function (AimsStrategies $aims) {
    $aims
        ->environment('staging')
        ->user([1, 2]);
});

Custom Segmentation Strategies

You can create custom strategies with dependency injection support similarly to Laravel Controllers' method injection. A common use-case for method injection is injecting the Illuminate\Contracts\Auth\Guard instance into your strategy to target users by property:

Callback

use Illuminate\Contracts\Auth\Guard;

Bento::feature('feature')->custom(function (Guard $guard, $role) {
    return $guard->user() && $guard->user()->role === 'admin';
});

Class

use Illuminate\Contracts\Auth\Guard;

class RoleStrategy {
    /**
     * @var \Illuminate\Contracts\Auth\Guard
     */
    protected $guard;
    
    /**
     * @var string 
     */
    protected $role;

    /**
     * @param \Illuminate\Contracts\Auth\Guard $guard
     */
    public function __construct(Guard $guard, string $role)
    {
        $this->guard = $guard;
        $this->role = $role;
    }

    /**
     * @return bool
     */
    public function launch(): bool
    {
        return $this->guard->user() && $this->guard->user()->role === $this->role;
    }
}

Bento::feature('feature')->aim(RoleStrategy::class, 'admin');

Testing

To run the PHPUnit tests, please use:

$ composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

This code is licensed under the MIT license. Please see the license file for more information.

laravel-bento's People

Contributors

christophetremblay avatar gandhi11 avatar xel1045 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ken8203

laravel-bento's Issues

Ability to inject classes with IoC to strategy with custom parameters

Example:

<?php namespace Exolnet\Bento\Strategy;

use Illuminate\Contracts\Auth\Guard;

class User extends Strategy
{
    /**
     * @var \Illuminate\Contracts\Auth\Guard
     */
    protected $guard;

    /**
     * @var array
     */
    protected $userIds;

    /**
     * @param \Illuminate\Contracts\Auth\Guard $guard
     * @param array|int $userIds
     */
    public function __construct(Guard $guard, $userIds)
    {
        $this->userIds = (array)$userIds;
        $this->guard = $guard;
    }

    /**
     * @return bool
     */
    public function launch()
    {
        $userId = $this->guard->id();

        return in_array($userId, $this->userIds);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.