eatae / throwable-instruction
异常指令
1.0.0
2023-12-20 18:35 UTC
Requires
- php: >=7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: dev-master
- phpstan/phpstan: 1.11.x-dev
- phpunit/phpunit: 9.6.*
This package is auto-updated.
Last update: 2024-09-30 16:59:02 UTC
README
异常指令
一个用于创建异常指令的PHP库
描述
异常指令 允许在特定位置抛出异常时定义额外的操作。
翻译
简单示例
调用指定异常类型的指令
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); }
调用任何类型的异常指令
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); } }
用法
- 创建操作类
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}"; } }
- 调用Operator::followInstruction()方法
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); } } }
- 为异常提供指令
- 无指令
public function doSomething(object $entity): void { if (!$this->validator->validate($entity)) { /* * InvalidArgumentException without instructions */ throw new InvalidArgumentException('Entity is not valid object'); } }
- 带有Notice指令
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']) ); } }
- 带有重要和关键邮件指令
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']) ); } }
结论
代码中不同位置的try/catch或异常的逻辑层次结构的缺失可能会使其难以处理。异常指令不会解决架构问题,但它可能会提高对由异常引起的行为的理解,并添加重用这些行为的能力。