sammakescode/actions
最新稳定版本:v1.0.0
Composer 安装命令:
composer require sammakescode/actions
包简介
A standardised way to perform actions
README 文档
README
Introduction
This library is designed to facilitate contained, repeatable actions you might use throughout your application and enforce business rules on those actions.
Example use case
A hypothetical application might want to - for various reasons and in various places - cancel an order. In order to prevent duplication of logic, a developer might create something like a class called "CancelOrder" that defines that logic.
This library formalises that process and allows you to attach business logic to it. For example, if it's never possible to cancel an order that's been dispatched, you can define that logic within your action.
Requirements
- Composer
- PHP 8.2 or later
Installation
composer require sammakescode/actions
Usage
You can have a look at the tests directory for some examples, but here's a quick on your can have a look at.
Example
Define your business rules
<?php namespace App; use \SamMakesCode\Actions\BusinessRules\BusinessRulesInterface; class OrderMustNotBeDispatched implements BusinessRulesInterface { public function __construct( private readonly Order $order, ) {} public function isSatisfied(): bool { return !in_array( $this->order->status, [ 'dispatched', 'delivered', 'complete', ] ); } public function getFailureMessage(): string { return 'Cannot perform action because the order has been dispatched!'; } }
Define your action
<?php namespace App; use \SamMakesCode\Actions\Actions\BaseAction; class CancelOrder extends BaseAction { public function __construct( private readonly Order $order, ) { $this->registerBusinessRules([ new OrderMustNotBeDispatched($this->order), ]); } public function handle() { $this->order->cancel(); } }
Perform your action
<?php $order = new \App\Order; $order->status = 'dispatched'; $action = new \App\CancelOrder($order); // Throws \SamMakesCode\Actions\Exceptions\BusinessRulesNotSatisfied because dispatched orders can't be cancelled $action->perform();
Note: When the perform() method is called on an action, the __call() magic method intercepts the request and calls the handle() method after any business rules have been run.
Shorthand actions
Instead of manually instantiating and performing you action, you can also use the ::do() shorthand.
CancelOrder::do($order);
Contributions
Contributions and issues are welcome. :)
FAQ
Isn't this just fancy conditional blocks?
Yes, but also no. The library allows you to wrap complex (or simple) sets of conditional statements into a block that can be reused in many places and standardises the exceptions that are thrown.
sammakescode/actions 适用场景与选型建议
sammakescode/actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rules」 「business」 「actions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sammakescode/actions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sammakescode/actions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sammakescode/actions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.
Magento 2 module which adds support for Speculation Rules API for instant page loads.
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
Business rules engine tools.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
A set of Laravel validation rules
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-20