jcfrane/policy 问题修复 & 功能扩展

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

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

jcfrane/policy

Composer 安装命令:

composer create-project jcfrane/policy

包简介

A simple and naive policy implementation

关键字:

README 文档

README

A naive and simple implementation for Policy classes.

Benefits

  • Avoid nested if's in your code.
  • Aggregate conditional logic in once place.

How to use?

Create a class that extends AbstractPolicy

use JcFrane\Policy\AbstractPolicy;

class BlogPolicy extends AbstractPolicy
{

}

Create methods inside the your Policy class that represent each conditional logic for the said policy.

use JcFrane\Policy\AbstractPolicy;

class BlogPolicy extends AbstractPolicy
{
    public function canCreate($subject)
    {
        $this->allow($subject->canCreate, 'Cannot create a subject.');
    }

    public function canView($subject)
    {
        $this->allow($subject->canView, 'Cannot view the subject.');
    }

    public function canDelete($subject)
    {
        $this->forbid($subject->canDelete, 'Cannot delete the subject.');
    }

    public function canUpdate($subject)
    {
        $this->forbid($subject->canUpdate, 'Cannot update the subject');
    }
}

Evaluate your Policy Class

use JcFrane\Polcy\PolicyChecker;

use Some\Namespace\BlogPolicy;

$checker = new PolicyChecker();

$subject = new \stdClass();
$subject->canCreate = true;
$subject->canView = false;
$subject->canDelete = true;
$subject->canUpdate = false;

$result = $checker->check(BlogPolicy::class, $subject, 'create'); // returns true

$result = $checker->check(BlogPolicy::class, $subject, 'view'); // throws JcFrane/PolicyViolationException

$result = $checker->check(BlogPolicy::class, $subject, 'delete'); // returns true

$result = $checker->check(BlogPolicy::class, $subject, 'update'); // throws JcFrane/PolicyViolationException

That's it! You can now create multiple policy classes that aggregates your conditional logic inside your code.

The allow() and forbid() methods

The allow() accepts a boolean and a violation message. If first argument is true, then the check() will return true. Otherwise, will throw a ViolationException.

The forbid() accepts a boolean and a violation message. If first argument is false, then the check() will throw a ViolationException. Otherwise, will return true.

Installation

$ composer install jcfrane/policy

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固