eatae/throwable-instruction
Composer 安装命令:
composer require eatae/throwable-instruction
包简介
Exception instructions
README 文档
README
Throwable instruction
A PHP library for creating Exception Instructions
Description
Throwable Instruction makes it possible to define additional actions when an Exception is thrown in a specific place.
Translation
Simple example
Call а instruction for the specified Exception type
try { $instruction = new ThrowableInstruction(); $operation = new EchoOperation('Some output.'); /* * Throwing an exception with an instruction */ throw new LogicException('Some message.', 0, $instruction->add($operation)); } catch (LogicException $e) { echo $e->getMessage(); /* * Call instructions for a specified exception */ Operator::followInstruction($e); }
Calling an instruction for any type of Exception
try { $instruction = new ThrowableInstruction(); $operation = new EchoOperation('Some output.'); /* * Throwing an exception with an instruction */ throw new LogicException('Some message.', 0, $instruction->add($operation)); } catch (LogicException $e) { echo $e->getMessage(); } finally { /* * Call instructions for all exceptions */ if (isset($e) && is_subclass_of($e, Exception::class)) { Operator::followInstruction($e); } }
Usage
- Create an operation class
class ImportantMessageOperation implements OperationInterface { protected string $message; protected int $code; public function __construct(string $message, int $code) { $this->message = $message; $this->code = $code; } public function execute(): void { echo "Important: {$this->message}. Code: {$this->code}"; } }
- Call the Operator::followInstruction() method
public function whereExceptionsAreCaught(): void { $object = new \stdClass(); try { /* * Some code... */ $this->item->doSomethingElse($object, $this->validator, $this->instruction); } catch (Exception $e) { $this->logger->log($e->getMessage(), $e->getCode()); } finally { /* * Call instructions for all exceptions */ if (isset($e) && is_subclass_of($e, Exception::class)) { Operator::followInstruction($e); } } }
- Provide instructions for Exception
- No instructions
public function doSomething(object $entity): void { if (!$this->validator->validate($entity)) { /* * InvalidArgumentException without instructions */ throw new InvalidArgumentException('Entity is not valid object'); } }
- With Notice instructions
public function doSomethingElse(object $item): void { if (!$this->validator->validate($item)) { /* * InvalidArgumentException with Notice instructions */ throw new InvalidArgumentException( 'Item is not valid object', 0, $this->instruction->operation(NoticeMessageOperation::class, ['Invalid values passed']) ); } }
- With Important and CriticalEmail instructions
public function doSomethingImportant(object $importantItem): void { if (!$this->validator->validate($importantItem)) { /* * InvalidArgumentException with Important instructions */ throw new InvalidArgumentException( 'Important Item is not valid object', 0, $this->instruction ->operation(ImportantMessageOperation::class, ['Important values are not valid', 500]) ->operation(SendCriticalEmailOperation::class, ['Any message text']) ); } }
Conclusion
Try/catch in different places in the code or the lack of a logical hierarchy of Exceptions can make them difficult to work with. Throwable Instruction will not fix architectural problems, but it may improve understanding of the actions caused by Exceptions and add the ability to reuse these actions.
eatae/throwable-instruction 适用场景与选型建议
eatae/throwable-instruction 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 eatae/throwable-instruction 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eatae/throwable-instruction 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-only
- 更新时间: 2022-04-29