jlis / judge
Judge handles feature and value toggles/flaggings for Laravel.
Installs: 1 035
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 4
Requires
- php: >=5.6.0
- illuminate/support: 5.*
- symfony/expression-language: ~2.7|~3.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.3
Suggests
- predis/predis: Allows reading the configuration from Redis
README
The easy way to toggle/decide features and values.
- Installation.
- Feature configuration.
- Feature configuration examples.
- Value configuration.
- Value configuration examples.
- Voters.
- Adapters.
- Usage.
- TBD.
Installation
First, pull in the package through Composer.
composer require jlis/judge
And then include the service provider within app/config/app.php
.
'providers' => [ Jlis\Judge\JudgeServiceProvider::class, ];
And, for convenience, add a facade alias to this same file at the bottom:
'aliases' => [ 'Feature' => Jlis\Judge\Feature::class, 'Value' => Jlis\Judge\Value::class, ];
Copy the package configs to your local config with the publish command:
php artisan vendor:publish
Feature configuration
The feature configuration is stored in:
app/config/features.php
A feature is defined as something which is either on or off and should be only used for that kind of toggles. Lets see:
'show_memory_usage' => [ [ 'value' => true, 'filters' => ['debug:true'], ], ],
The name of the feature is show_memory_usage
and it should return true if the "debug" Voter returns true (it checks whether the debug mode is enabled or not, see DebugVoter.php)
Note that the default value of a feature, if not defined otherwise, is always false.
Feature configuration examples
A simple feature without any filters ( note that the value can also be a string like "true", "on" or "1", it will be converted into a boolean ):
'enable_captcha' => [ [ 'value' => true, ], ],
A feature with multiple filters chained in an AND condition:
'enable_captcha' => [ [ 'value' => true, 'filters' => ['env:production', 'expression_language:user==null'], ], ],
A feature with multiple filters chained in an OR condition:
'enable_captcha' => [ [ 'value' => true, 'filters' => ['expression_language:user==null'], ], [ 'value' => true, 'filters' => ['env:production'], ], ],
A feature with a negated filter:
'enable_debug_output' => [ [ 'value' => true, 'filters' => ['!env:production'], ], ],
Value configuration
The value configuration is stored in:
app/config/values.php
The value however is something which always returns a value (whoa) like a string or number for example:
'greeting' => [ [ 'value' => 'Hello my lady!', 'filters' => ['expression_language:user.getGender()=="female"'], ], [ 'value' => 'Hello sir.', ], ]
This name of the value is greeting
. It should return "Hello my lady!" if the expression voter return true (assuming the given user is not NULL and it's gender is female). Otherwise it should return "Hello sir.".
(Sorry for the gender guessing)
(ExpressionVoter.php uses the Symfony Expression Language to check if the given expression is true)
Value configuration examples
A simple value without any filters :
'package_price' => [ [ 'value' => 10.00, ], ],
A value with one filter and a default value:
'package_price' => [ [ 'value' => 00.00, 'filters' => ['expression_language:user.hasPlan("premium")'], ], [ 'value' => 10.00, ], ],
A value with multiple filters chained in an AND condition and a default value:
'package_price' => [ [ 'value' => 5.00, 'filters' => ['expression_language:user.getRegisterDays() >= 365', 'made_at_least_one_purchase'], ], [ 'value' => 10.00, ], ],
Voters
The actual voters can be registered here:
app/config/judge.php
The voters contain the logic to decide whether the given filter should return true or false. This decides if either a feature is on or off or what a value should return regarding to it's config.
Adapters
By default, Judge uses the Laravel config to read the features/values. You can choose between the config, redis and cache adapter.
app/config/judge.php
If you want to add you own adapter, go for it. Just implement the AdapterInterface.
Usage
Within your controllers, you can use this for example...
$greeting = Value::decide('greeting', $this->getUser()); echo $greeting;
Or this:
if (Feature::decide('show_memory_usage', Auth::user())) { echo 'Memory usage: ' . memory_get_usage(); }
tbd
- Breakers