theroadbunch / bouncer
Bouncer 是一个易于使用的允许/拒绝列表界面。
v2.1
2023-03-16 19:24 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
README
什么是 Bouncer?
Bouncer 是一个易于使用的允许/拒绝列表界面。
为什么叫 Bouncer?
在现实世界中,保安负责维护允许进入和禁止进入的名单。
- 允许Bouncer:在一家私人俱乐部,保安有一份名单,如果你不在名单上,你就不能进来。
- 拒绝Bouncer:当地的酒吧几乎让每个人都进来,保安有一份麻烦制造者的名单,阻止他们在门口。
内容
使用 composer 安装[?]
composer require theroadbunch/bouncer
实现
标准用法
使用 Bouncer::allow()
或 Bouncer::deny()
创建你的允许/拒绝列表(保安)。
use RoadBunch\Bouncer\Bouncer; $subjectList = ['example', 'one', 'two', 'three']; $bouncer = Bouncer::allow($subjectList); $bouncer->isAllowed('example'); // true $bouncer = Bouncer::deny($subjectList); $bouncer->isAllowed('example'); // false // or $subjectList = 'example;one;two;three'; $bouncer = Bouncer::allow($subjectList); $bouncer->isAllowed('one'); // true $bouncer->isAllowed('not in list'); // false $bouncer = Bouncer::deny($subjectList); $bouncer->isAllowed('two'); // false $bouncer->isAllowed('not in list'); // true // or $subject = 'onlydomainiwanttosendemailto.com'; $bouncer = Bouncer::allow($subject); $bouncer->isAllowed($subject); // true $bouncer->isAllowed('example.com'); //false
实时更新保安
use RoadBunch\Bouncer\Bouncer; $subject = 'allowedAndThenDenied'; $bouncer = Bouncer::allow($subject); $bouncer->isAllowed($subject); // true $bouncer->deny($subject); $bouncer->isAllowed($subject); // false
使用 BouncerFactory::create()
这已被弃用,将在版本 2.4 中从文档中移除
use RoadBunch\Bouncer\Rule; use RoadBunch\Bouncer\BouncerFactory; $subjectList = ['example', 'one', 'two', 'three']; $bouncer = BouncerFactory::create(Rule::ALLOW, $subjectList); $bouncer->isAllowed('example'); // true $bouncer->isAllowed('not in list'); // false
现实世界用例示例
<?php declare(strict_types=1); use Psr\Log\LoggerInterface; use RoadBunch\Bouncer\Bouncer; use RoadBunch\Bouncer\BouncerInterface; class Mailer { public function __construct( /** ... config parameters ... */, private readonly LoggerInterface $logger, private readonly BouncerInterface $bouncer, ) {} public function send(string $address, string $message): void { if (!$this->bouncer->isAllowed($address)) { $this->logger->warning("Cannot send email to blocked email address: {$address}"); return; } // do work if (/* work fails */) { // deny this address in the next run $this->bouncer->deny($address); $this->logger->error("Sending failed, {$address} added to block list.") return; } $this->logger->info("Sending email to {$address}."); } } // Create a bouncer that has a DenyList $bouncer = Bouncer::deny(['someone@example.com', 'someone_else@example.com']); $mailer = new Mailer($bouncer); $mailer->send('someone@example.com', 'Welcome!'); // logs warning $mailer->send('someotheraddress@example.com', 'Welcome!'); // sends mail
保安
所有保安都实现了 RoadBunch\Bouncer\BouncerInterface
RoadBunch\Bouncer\AllowBouncer
RoadBunch\Bouncer\DenyBouncer
抽象保安
RoadBunch\Bouncer\AbstractBouncer
实现 RoadBunch\Bouncer\BouncerInterface
,并可用于扩展,如果你需要编写自己的逻辑来决定一个主题是否被允许。
许可
© Dan McAdams | 本库内容发布于 MIT 许可证
你可以在 LICENSE
或 https://open-source.org.cn/licenses/mit 找到此许可证的副本。