larapie/guard
Composer 安装命令:
composer require larapie/guard
包简介
Guard objects for conditional exception throwing.
README 文档
README
Installation
You can install the package via composer:
composer require larapie/guard
Have you ever…
… encountered a situation where you had countless if statements that needed to throw specific errors when conditions were met?
Here's an example:
public function foo() { if($user==null) throw new UserNotFoundException(); if($user->age < 18) throw new NotOldEnoughException(18); ... }
The goal of this package is to decouple these conditions and their exceptions in a guard object. This guard object can then be handled by the GuardHandler. By structuring this code in a guard object, we gain several advantages:
- We're able to reuse the same conditions in the code elsewhere.
- The code becomes a lot easier to read.
- More fine grained control of what exceptions will be thrown.
Let's rewrite the example from above with guards:
public function foo() { guard(new UserDoesNotExistsGuard($user), new UserInsufficientAgeGuard($user, 18)); }
Note that the order of the guards will determine what exception will be thrown first.
class UserDoesNotExistsGuard extends Guard { /** * The exception that will be thrown when the condition is met * * @var string */ protected $exception = UserNotFoundException::class; /** * @var User */ protected $user; /** * UserDoesNotExistsGuard constructor. * @param $user */ public function __construct(User $user) { $this->user = $user; } /** * The condition that needs to be satisfied in order to throw the exception. * * @return bool */ public function condition(): bool { return $this->user===null; } }
class UserInsufficientAgeGuard extends Guard { /** * @var User */ protected $user; /** * @var int */ protected $age; /** * UserDoesNotExistsGuard constructor. * @param $user */ public function __construct(User $user, int $age) { $this->user = $user; $this->age = $age; } /** * The condition that needs to be satisfied in order to throw the exception. * * @return bool */ public function condition(): bool { return $this->user->age < this->age; } /** * The exception that gets thrown when the condition is satisfied. * * @return \Throwable */ public function exception(): \Throwable { return new NotOldEnoughException(18); } }
Future
- Implement a feature that allows the guard handler to handle OR statements.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email anthony.vanc@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
larapie/guard 适用场景与选型建议
larapie/guard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 592 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「exceptions」 「laravel」 「guard」 「larapie」 「throwing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 larapie/guard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 larapie/guard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 larapie/guard 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
The CodenamePHP Platform core that contains basic interfaces, exceptions etc..
A simple package to better handle Exceptions.
Monitors errors and exceptions and reports them to Rollbar
Creates & updates issues on a GitLab repository from Symfony2 exceptions
Exception monitoring and debug email channel for Filament.
统计信息
- 总下载量: 592
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-30