jlis/judge 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jlis/judge

最新稳定版本:v1.0.4

Composer 安装命令:

composer require jlis/judge

包简介

Judge handles feature and value toggles/flaggings for Laravel.

README 文档

README

The easy way to toggle/decide features and values.

Build Status Scrutinizer Code Quality Code Coverage StyleCI SensioLabsInsight Total Downloads

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

统计信息

  • 总下载量: 1.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-09