ionews/light-service-php
Composer 安装命令:
composer require ionews/light-service-php
包简介
Provides a simple interface to implement complex actions through a serie of simple actions
README 文档
README
Small piece of software intended to enforce SRP on PHP apps, thought to be "light" and not use any dependencies. Heavily based on the ideas proposed by two ruby gems:
Concept
Each action should have a single responsibility that must be implemented in the perform method. An action can access databases, send emails, call services and etc.
When an action is executed, it receives a context which can be read and modified.
To perform more complex operations you must use an organizer chaining multiple actions, which will share the same context during execution. In fact, an organizer is nothing more than an action with a specific implementation, meaning that an action and an organizer share the very same interface. This is useful so you can include an organizer as an action inside another organizer.
Action examples:
class GenerateRandomPassword extends Action { protected function perform() { $length = 8; $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $password = ''; for ($i = 0; $i < $length; $i++) { $password .= $chars[rand(0, strlen($chars) - 1)]; } $this->context->password = $password; } } class UpdateUserPassword extends Action { protected function perform() { $user_id = $this->context->user_id; $password = $this->context->password; // access the database using the method of your choice and update the password } }
Organizer example:
class ResetUserPassword extends Organizer { protected $organize = ['GenerateRandomPassword', 'UpdateUserPassword', 'EmailUserWithPassword']; }
Call example (from a MVC controller):
class UserController extends BaseController { public function resetPassword() { $result = ResetUserPassword::execute(['user_id' => $this->request->id]); if ($result->success()) { // use $result->getContext() to access the results and redirect the app } else { // error, use $result->getFailureMessage() to access any failure message } } }
Keep in mind that you shouldn't use this approach everywhere in your app, but only in the really complex parts of it.
Fail, Halt and Exceptions
An action may fail, meaning that it couldn't achieve its goal. To make an action fail just call the fail method (optionally passing a message).
class SomeAction extends Action { protected function perform() { $this->fail('Oh noes'); } } $result = SomeAction::execute([]); $result->success(); // false $result->failure(); // true $result->halted(); // false $result->getFailureMessage(); // 'Oh noes'
If the action is executing inside an organizer and fails, it will prevent the execution of the subsequents actions.
If an action implements a rollback method, it will be called after a subsequent action fails. Example: if EmailUserWithPassword fails to send an e-mail to the user, we could implement an rollback method in the UpdateUserPassword to undo the update. Inside the rollback method you can access the context in the same way as in perform.
class UpdateUserPassword extends Action { protected function perform() { $user_id = $this->context->user_id; $password = $this->context->password; // access the database using the method of your choice and update the password } protected function rollback() { // undo the update password } }
You shouldn't re-implement the rollback method of an organizer, unless you really know what you're doing.
It's possible to stop the execution chain without fail: using halt. Basically it will prevent any subsequent actions of execute, but the result remains a success. You can test if an action/organizer was halted using the halted method.
class SomeAction extends Action { protected function perform() { $this->halt(); } } $result = SomeAction::execute([]); $result->success(); // true $result->failure(); // false $result->halted(); // true
All exceptions that occur when performing an action are caught automatically and passed to the caught method. By default, actions re-throw then, on the other hand, organizers first call the rollback method before re-throwing. This is done so all performed actions are rolled back before the exception propagation. You can change this behaviour re-implementing the caught method.
Before and After
A before method can be implemented if you need to do any setup pre-execution. If the fail method is called inside the before, perform will never be called.
In the same way, an after method can be implemented so you can do any cleanup, but keep in mind that if before or perform fails it will never be called.
class SomeAction extends Action { protected function before() { // any setup } protected function perform() { // perform } protected function after() { // cleanup } }
Expects and Promises
Expectations and promises can be defined for each action. If an action has a set of expectations, it will automatically fails if these aren't met.
class UpdateUserPassword extends Action { protected $expects = ['user_id', 'password']; protected function perform() { $user_id = $this->context->user_id; $password = $this->context->password; // access the database using the method of your choice and update the password } } $result = UpdateUserPassword::execute(['user_id' => 1]); $result->success(); // false $result->getFailureMessage(); // 'Expectations were not met'
Similarly, an action will fail if a set of promises are defined and these are not present in the context at the end of execution.
class GenerateRandomPassword extends Action { protected $promises = ['password']; protected function perform() { $length = 8; $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $password = ''; for ($i = 0; $i < $length; $i++) { $password .= $chars[rand(0, strlen($chars) - 1)]; } //$this->context->password = $password; } } $result = GenerateRandomPassword::execute([]); $result->success(); // false $result->getFailureMessage(); // 'Promises were not met'
This feature is particularly useful so you can explicitly define the interface between the actions.
Iterator Action
It's an action that will be performed over an array.
class SomeAction extends IteratorAction { protected $over = 'key_of_the_array_in_context' protected function each($key, $value) { ... } }
Requirements
- PHP 5.3+
Installation and Usage
Contributing
You know the drill!
License
Released under GPLv2
ionews/light-service-php 适用场景与选型建议
ionews/light-service-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.59k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「service」 「rules」 「action」 「business」 「Organizer」 「interactor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ionews/light-service-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ionews/light-service-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ionews/light-service-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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.
A Laravel Nova asset.
Business rules engine tools.
A fast and intuitive dependency injection container.
Generate Laravel actions
统计信息
- 总下载量: 6.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2014-05-25