Package Data | |
---|---|
Maintainer Username: | jlis |
Maintainer Contact: | julius@ehrlich-bros.de (Julius Ehrlich) |
Package Create Date: | 2015-04-14 |
Package Last Update: | 2017-11-17 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-14 15:17:57 |
Package Statistics | |
---|---|
Total Downloads: | 1,035 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 4 |
The easy way to toggle/decide features and values.
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
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.
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'],
],
],
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)
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,
],
],
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.
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.
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();
}