trinetus/feature-flags

Simple Feature Flags implementation for Laravel project.

dev-main 2025-04-17 10:07 UTC

This package is auto-updated.

Last update: 2025-04-30 12:03:15 UTC


README

Simple Feature Flags implementation for Laravel applications. You can apply feature flag on:

  • combinations of scopes (users/roles/organizations/whatever you need...),
  • global (on/off).

Installation

composer require trinetus/feature-flags

Configuration

  1. Migrate database (creates 1 new database table feature_flags for storing FF configs)

     php artisan migrate
    
  2. Copy configuration file from vendor to your app.

     php artisan vendor:publish --provider="Trinetus\FeatureFlags\FeatureFlagsServiceProvider" --tag=config --force
    
  3. Update your config as you need for your app - create your own checking scopes and pair it with eloquent models.

     'scopes' => [
         'users' => \App\Models\User::class,
         'roles' => \App\Models\Role::class,
         'organizations' => \App\Models\Organization::class,
     ]
    
  4. Inject trait into specified models (model have to be assigned to one of the scopes in config).

     class YourModel extends Model {
         use \Trinetus\FeatureFlags\Traits\HasFeatureFlag;
            
         // ...
     }
    

Code usage

Using model trait methods

$result = $modelInstance->hasFeatureFlag('my-new-feature');  
// returns bool

$result = $modelInstance->hasAllFeatureFlags(['my-feature','my-second-feature']);  
// returns bool (x AND y)

$result = $modelInstance->featureFlags();  
// returns array of all enabled FF

Using model scope

MyModel::withFeatureFlag('my-feature')->get(); 
// returns all MyModel instances which have enabled this feature flag

MyModel::withoutFeatureFlag('my-feature')->get(); 
// returns all MyModel instances which don't have enabled this feature flag

Checking globally via service

    use Trinetus\FeatureFlags\Services\FeatureFlagsService as FF;
    
    // ...
    
    if(!FF::active('my-feature')) {
        // true if FF is turned "on" globally (for all)
    }
    
    if(!FF::activeAny(['my-feature', 'my-second-feature'])) { 
        // true if at least one feature is turned "on" globally (for all)
    }
    
    if(!FF::activeAll(['my-feature', 'my-second-feature'])) {
        // true if all features are turned "on" globally (for all)
    }

Managing (via console)

List feature flags

php artisan ff:list

Add new (interactive)

php artisan ff:add

Edit existing (interactive)

php artisan ff:edit {id}

Delete FF

php artisan ff:delete {id}

Export whole config (json)

php artisan ff:export

Import (and remove current) config in JSON format

php artisan ff:import {config}

License

MIT License

WIP (roadmap):

  • model
  • repository (crud operations)
  • command for listing (into console) current feature flags
  • command for adding/modifying/deleting feature flag by ID (interactive terminal)
  • check service
  • trait for specified model
  • config ? (define user, group models or define interface?)
  • phpunit tests
  • phpstan code quality checks (level 8)
  • hierarchical checks (user.hasFF || user->group->hasFF)