josantonius/exception-handler
Composer 安装命令:
composer require josantonius/exception-handler
包简介
PHP library for handling exceptions.
README 文档
README
Translations: Español
PHP library for handling exceptions.
- Requirements
- Installation
- Available Classes
- Exceptions Used
- Usage
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
Requirements
-
Operating System: Linux | Windows.
-
PHP versions: 8.1 | 8.2.
Installation
The preferred way to install this extension is through Composer.
To install PHP ExceptionHandler library, simply:
composer require Josantonius/exception-handler
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/exception-handler --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-exception-handler.git
Available Classes
ExceptionHandler Class
Josantonius\ExceptionHandler\ExceptionHandler
Sets a exception handler:
/** * 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://www.php.net/manual/en/functions.first_class_callable_syntax.php */ public function __construct( private callable $callback, private array $runBeforeCallback = [], private array $runAfterCallback = [] );
Exceptions Used
use Josantonius\ExceptionHandler\Exceptions\NotCallableException; use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
Usage
Examples of use for this library:
Sets basic exception handler
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) */
Sets methods to execute before calling the callback
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) */
Sets methods to execute after calling the callback
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() */
Sets methods to execute before and after calling the callback
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. */
Tests
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-exception-handler.git
cd php-exception-handler
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
TODO
- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
Changelog
Detailed changes for each release are documented in the release notes.
Contribution
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
Sponsor
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
License
This repository is licensed under the MIT License.
Copyright © 2022-present, Josantonius
josantonius/exception-handler 适用场景与选型建议
josantonius/exception-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「exception」 「throw」 「exception-handler」 「throwable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 josantonius/exception-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 josantonius/exception-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 josantonius/exception-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Error and Exception override and observers.
Error handler based on Booboo with HTML and JSON support
Validate and check for disposable/temporary/throw away emails using multiple providers
Standard PHP Library Extension featuring an ExceptionInterface.
Alfabank REST API integration
The roots of an exception hierarchy based on contingencies and faults.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-04