degraciamathieu/php-easy-breaker

PHP implementation of circuit breaker pattern.

v1.0.1 2019-12-15 21:17 UTC

This package is auto-updated.

Last update: 2024-03-29 04:05:22 UTC


README

Build Status Code Coverage PHP range Latest Version on Packagist 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656772616369616d6174686965752f7068702d656173792d627265616b65722e7376673f7374796c653d666c61742d737175617265

degraciamathieu/php-easy-breaker

PHP implementation of circuit breaker pattern.

Installation

Run in console below command to download package to your project:

composer require degraciamathieu/php-easy-breaker

usage

require __DIR__.'/vendor/autoload.php';

use Exception;
use DeGraciaMathieu\EasyBreaker\Breaker;
use DeGraciaMathieu\EasyBreaker\CircuitBreaker;

$firstBreaker = (new Breaker)
    ->when(Exception::class)
    ->do(function(Exception $e){
        return "it's broken.";
    });

$secondBreaker = (new Breaker)
    ->when(Exception::class)
    ->do(function(Exception $e){
        return "really broken.";
    });

$thirdBreaker = (new Breaker)
    ->when(AnotherException::class)
    ->do(function(AnotherException $e){
        return "boom.";
    });

$results = (new CircuitBreaker())
    ->addBreaker($firstBreaker)
    ->addBreaker($secondBreaker)
    ->addBreaker($thirdBreaker)
    ->closure(function(){
        throw new Exception();
    });

var_dump($results);

// array(2) {
//   [0]=>
//   string(12) "it's broken."
//   [1]=>
//   string(18) "really broken."
// }