josantonius / exception-handler
用于处理异常的PHP库。
v1.0.3
2022-09-29 16:30 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
README
翻译: 西班牙语
用于处理异常的PHP库。
要求
-
操作系统:Linux | Windows。
-
PHP版本:8.1 | 8.2。
安装
安装此扩展的首选方式是通过 Composer。
要安装 PHP ExceptionHandler 库,只需
composer require Josantonius/exception-handler
上面的命令只会安装必要的文件,如果您想 下载整个源代码,可以使用
composer require josantonius/exception-handler --prefer-source
您也可以使用 Git 克隆完整的仓库
git clone https://github.com/josantonius/php-exception-handler.git
可用类
ExceptionHandler 类
Josantonius\ExceptionHandler\ExceptionHandler
设置异常处理器
/** * Sets a exception handler. * * @param callable $callback Exception handler function. * @param string[] $runBeforeCallback Method names to call in the exception before run callback. * @param string[] $runAfterCallback Method names to call in the exception after run callback. * * @throws NotCallableException if the callback is not callable. * @throws WrongMethodNameException if the method names are not string or are empty. * * @see https://php.ac.cn/manual/en/functions.first_class_callable_syntax.php */ public function __construct( private callable $callback, private array $runBeforeCallback = [], private array $runAfterCallback = [] );
使用的异常
use Josantonius\ExceptionHandler\Exceptions\NotCallableException; use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
用法
此库的使用示例
设置基本的异常处理器
use Josantonius\ExceptionHandler\ExceptionHandler; function handler(\Throwable $exception) { /* do something */ } new ExceptionHandler( callback: handler(...) ); /** * If an exception is thrown, the following is executed: * * handler($exception) */
设置在调用回调之前要执行的方法
use Josantonius\ExceptionHandler\ExceptionHandler; class FooException extends \Exception { public function context(): void { /* do something */ } } class Handler { public function exceptions(Throwable $exception): void { if ($exception instanceof FooException) { /* do something */ } } } new ExceptionHandler( callback: (new Handler())->exceptions(...), runBeforeCallback: ['context'] ); /** * If FooException() is thrown, the following is executed: * * FooException->context() * Handler->exceptions($exception) */
设置在调用回调之后要执行的方法
use Josantonius\ExceptionHandler\ExceptionHandler; class FooException extends \Exception { public function report(): void { /* do something */ } public function render(): void { /* do something */ } } class Handler { public static function exceptions(Throwable $exception): void { if ($exception instanceof FooException) { /* do something */ } } } new ExceptionHandler( callback: Handler::exceptions(...), runAfterCallback: ['report', 'render'] ); /** * If FooException() is thrown, the following is executed: * * Handler::exceptions($exception) * FooException->report() * FooException->render() */
设置在调用回调前后要执行的方法
use Josantonius\ExceptionHandler\ExceptionHandler; class FooException extends \Exception { public function context(): void { /* do something */ } public function report(): void { /* do something */ } public function render(): void { /* do something */ } } function exceptionHandler(Throwable $exception) { /* do something */ } new ExceptionHandler( callback: exceptionHandler(...), runBeforeCallback: ['context', 'logger'], runAfterCallback: ['report', 'render'] ); /** * If FooException() is thrown, the following is executed: * * FooException->context() * exceptionHandler($exception) * FooException->report() * FooException->render() * * FooException->logger() is ignored, does not exist in the exception. */
测试
git clone https://github.com/josantonius/php-exception-handler.git
cd php-exception-handler
composer install
使用 PHPUnit 运行单元测试
composer phpunit
使用 PHPCS 运行代码标准测试
composer phpcs
运行 PHP Mess Detector 测试以检测代码风格的不一致性
composer phpmd
运行所有前面的测试
composer tests
待办事项
- 添加新功能
- 改进测试
- 改进文档
- 改进README文件中的英语翻译
- 重构代码以禁用代码风格规则(请参阅phpmd.xml和phpcs.xml)
变更日志
每个版本的详细更改记录在 发行说明 中。
贡献
在提交拉取请求、开始讨论或报告问题之前,请务必阅读 贡献指南。
感谢所有 贡献者! ❤️
赞助
如果此项目帮助您减少了开发时间,您可以通过 赞助我 以支持我的开源工作 😊
许可证
此存储库受 MIT 许可证 许可。
版权所有 © 2022-至今,Josantonius