Skip to content

josantonius/php-exception-handler

Repository files navigation

PHP ExceptionHandler library

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12

Translations: Español

PHP library for handling exceptions.



Requirements

This library is compatible with the PHP versions: 8.1.

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 Methods

Available methods in this library:

Sets a exception handler

new ExceptionHandler(
    callable $callback,
    string[] $runBeforeCallback = [],
    string[] $runAfterCallback = []
);

@param callable $callback Exception handler function.

@param array $runBeforeCallback Method names to call in the exception before run callback.

@param array $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 strings.

@see https://www.php.net/manual/en/functions.first_class_callable_syntax.php for more information about first class callable syntax.

Quick Start

To use this library:

use Josantonius\ExceptionHandler\ExceptionHandler;

new ExceptionHandler(/*...*/);

Usage

Examples of use for this library:

Sets basic exception handler

function handler(Throwable $exception) { /* do something */ }
new ExceptionHandler(
    callback: handler(...)
);

If an exception is thrown:

  • handler($exception) callback will be called

Sets methods to execute before calling the callback

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:

  • FooException->context() method will be called
  • Handler->exceptions($exception) callback will be called

Sets methods to execute after calling the callback

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:

  • Handler::exceptions($exception) callback will be called
  • FooException->report() method will be called
  • FooException->render() method will be called

Sets methods to execute before and after calling the callback

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:

  • FooException->context() method will be called
  • FooException->logger() method will be ignored, it does not exists in the exception
  • exceptionHandler($exception) callback will be called
  • FooException->report() method will be called
  • FooException->render() method will be called

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

About

PHP library for handling exceptions

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages