Traductions : espagnol
Bibliothèque PHP pour gérer les exceptions.
Pour gérer les exceptions, vous pouvez utiliser la bibliothèque de gestionnaires d'exceptions.
Système d'exploitation : Linux | Fenêtres.
Versions PHP : 8.1.
La méthode préférée pour installer cette extension est via Composer.
Pour installer la bibliothèque PHP ErrorHandler , simplement :
composer require josantonius/error-handler
La commande précédente n'installera que les fichiers nécessaires, si vous préférez télécharger l'intégralité du code source, vous pouvez utiliser :
composer require josantonius/error-handler --prefer-source
Vous pouvez également cloner le dépôt complet avec Git :
git clone https://github.com/josantonius/php-error-handler.git
JosantoniusErrorHandlerErrorException
étend ErrorException
Obtient le fichier d'erreur :
public function getFile(): string ;
Obtient le niveau d'erreur :
public function getLevel(): int ;
Obtient la ligne du fichier d'erreur :
public function getLine(): int ;
Obtient un message d'erreur :
public function getMessage(): string ;
Obtient le nom de l'erreur :
public function getName(): string ;
JosantoniusErrorHandlerErrorHandled
Obtient le fichier d'erreur :
public function getFile(): string ;
Obtient le niveau d'erreur :
public function getLevel(): int ;
Obtient la ligne du fichier d'erreur :
public function getLine(): int ;
Obtient un message d'erreur :
public function getMessage(): string ;
Obtient le nom de l'erreur :
public function getName(): string ;
JosantoniusErrorHandlerErrorHandler
Convertissez les erreurs en exceptions :
/**
* 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 ;
Convertissez les erreurs en exceptions, à l'exception de certaines d'entre elles :
/**
* 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 ;
Fonction de gestion des erreurs d'enregistrement :
/**
* 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 ;
Utilisez le rapport d'erreurs pour déterminer quelles erreurs sont traitées :
/**
* 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 ;
Exemples d'utilisation de cette bibliothèque :
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.
Pour exécuter des tests, vous avez juste besoin de composer et d'exécuter ce qui suit :
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
Exécutez des tests unitaires avec PHPUnit :
composer phpunit
Exécutez des tests standards de code avec PHPCS :
composer phpcs
Exécutez les tests PHP Mess Detector pour détecter les incohérences dans le style de code :
composer phpmd
Exécutez tous les tests précédents :
composer tests
Les modifications détaillées pour chaque version sont documentées dans les notes de version.
Veuillez vous assurer de lire le guide de contribution avant de faire une pull request, de démarrer une discussion ou de signaler un problème.
Merci à tous les contributeurs ! ❤️
Si ce projet vous aide à réduire votre temps de développement, vous pouvez me sponsoriser pour soutenir mon travail open source ?
Ce référentiel est sous licence MIT.
Copyright © 2016-présent, Josantonius