PHP code example of milon / fuse

1. Go to this page and download the library: Download milon/fuse library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

milon / fuse example snippets


use Milon\Fuse\Fuse;
use Milon\Fuse\CircuitBreakerConfig;
use Milon\Fuse\Stores\ArrayStore;

$fuse = Fuse::for(
    name: 'billing-sdk',
    store: new ArrayStore(),
    config: CircuitBreakerConfig::defaults(),
);

$result = $fuse->run(
    execute: fn () => $client->charge($payload),
);

use Milon\Fuse\Events\CallableCircuitEventDispatcher;
use Milon\Fuse\Events\CircuitOpened;
use Milon\Fuse\FuseFactory;

$factory = new FuseFactory(
    store: $store,
    settings: [
        'breakers' => [
            'billing-sdk' => ['failure_threshold' => 3],
        ],
    ],
    events: (new CallableCircuitEventDispatcher)
        ->listenFor(CircuitOpened::class, fn (CircuitOpened $e) => error_log($e->storageKey)),
);

$factory->for('billing-sdk')->run(fn () => $client->charge($payload));

use Milon\Fuse\Saloon\Traits\HasCircuitBreaker;
use Saloon\Http\Connector;

class ExampleConnector extends Connector
{
    use HasCircuitBreaker;

    public function resolveBaseUrl(): string
    {
        return 'https://api.example.com';
    }
}

use Milon\Fuse\Laravel\Facades\Fuse;

$result = Fuse::for('billing-sdk')->run(
    execute: fn () => $client->charge($payload),
);

// or: fuse('billing-sdk')->run(...)

'breakers' => [
    'billing-sdk' => [
        'failure_threshold' => 3,
        'open_seconds' => 15,
    ],
],

Event::listen(CircuitOpened::class, fn (CircuitOpened $e) => logger()->warning($e->storageKey));

use Milon\Fuse\Testing\FuseMockClient;
use Milon\Fuse\Testing\InteractsWithFuse;
use Saloon\Http\Faking\MockResponse;

FuseMockClient::mock($connector, [MockResponse::make(status: 503)]);
$this->assertCircuitOpen($fuse);
$this->forceCircuitOpenFor($factory, 'billing', operation: 'charge');
bash
php artisan vendor:publish --tag=fuse-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=fuse-config
bash
php artisan fuse:status billing --operation=charge
php artisan fuse:reset billing --force