翻訳:スペイン語
例外を処理するための PHP ライブラリ。
例外を処理するには、例外ハンドラー ライブラリを使用できます。
オペレーティング システム: Linux |ウィンドウズ。
PHP バージョン: 8.1。
この拡張機能をインストールするには、Composer を使用することをお勧めします。
PHP ErrorHandler ライブラリをインストールするには、次のようにします。
composer require josantonius/error-handler
前のコマンドは必要なファイルのみをインストールしますが、使用できるソース コード全体をダウンロードしたい場合は、次のようにします。
composer require josantonius/error-handler --prefer-source
Git を使用して完全なリポジトリのクローンを作成することもできます。
git clone https://github.com/josantonius/php-error-handler.git
JosantoniusErrorHandlerErrorException
ErrorException を拡張します
エラーファイルを取得します:
public function getFile(): string ;
エラーレベルを取得します:
public function getLevel(): int ;
エラーファイル行を取得します:
public function getLine(): int ;
エラーメッセージを取得します:
public function getMessage(): string ;
エラー名を取得します:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandled
エラーファイルを取得します:
public function getFile(): string ;
エラーレベルを取得します:
public function getLevel(): int ;
エラーファイル行を取得します:
public function getLine(): int ;
エラーメッセージを取得します:
public function getMessage(): string ;
エラー名を取得します:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandler
エラーを例外に変換します。
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptions( int ... $ errorLevel ): ErrorHandler ;
一部を除き、エラーを例外に変換します。
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptionsExcept( int ... $ errorLevel ): ErrorHandler ;
エラーハンドラー関数を登録します。
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
public function register( callable $ callback ): ErrorHandler ;
エラー レポートを使用して、どのエラーが処理されるかを決定します。
/**
* If the setting value in error_reporting() is used to determine which errors are handled.
*
* If this method is not used, all errors will be sent to the handler.
*
* @see https://www.php.net/manual/en/function.error-reporting.php
*/
public function useErrorReportingLevel(): ErrorHandler ;
use Josantonius ErrorHandler Exceptions WrongErrorLevelException ;
このライブラリの使用例:
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ( E_USER_ERROR , E_USER_WARNING );
// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptionsExcept ( E_USER_DEPRECATED , E_USER_NOTICE );
// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
error_reporting ( E_USER_ERROR );
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be converted to exception.
use ErrorException ;
use Josantonius ErrorHandler ErrorHandler ;
set_exception_handler ( function ( ErrorException $ exception ) {
var_dump ([
' level ' => $ exception -> getLevel (),
' message ' => $ exception -> getMessage (),
' file ' => $ exception -> getFile (),
' line ' => $ exception -> getLine (),
' name ' => $ exception -> getName (),
]);
});
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
function handler ( Errorhandled $ errorHandled ): void {
var_dump ([
' level ' => $ errorHandled -> getLevel (),
' message ' => $ errorHandled -> getMessage (),
' file ' => $ errorHandled -> getFile (),
' line ' => $ errorHandled -> getLine (),
' name ' => $ errorHandled -> getName (),
]);
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: handler (...)
);
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
class Handler {
public static function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: Handler:: errors (...)
)-> convertToExceptions ();
// The error will be sent to the error handler and then throw the exception.
error_reporting ( E_USER_ERROR );
class Handler {
public function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ handler = new Handler ();
$ errorHandled -> register (
callback: $ handler -> errors (...),
)-> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be passed to the handler and converted to exception.
テストを実行するには、composer を使用して以下を実行するだけです。
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
PHPUnit を使用して単体テストを実行します。
composer phpunit
PHPCS を使用してコード標準テストを実行します。
composer phpcs
PHP Mess Detector テストを実行して、コード スタイルの不一致を検出します。
composer phpmd
以前のすべてのテストを実行します。
composer tests
各リリースの詳細な変更点はリリース ノートに記載されています。
プルリクエストを作成したり、ディスカッションを開始したり、問題を報告したりする前に、必ず貢献ガイドをお読みください。
すべての貢献者に感謝します! ❤️
このプロジェクトが開発時間の短縮に役立つ場合は、私のオープンソース作業をサポートするスポンサーになっていただけますか?
このリポジトリは、MIT ライセンスに基づいてライセンスされています。
著作権 © 2016 年現在、ヨサントニウス