Skip to content

hasnat/guzzle-mirror-middleware

Repository files navigation

Guzzle Mirror Middleware

Latest Version Build Status Total Downloads

This package provides middleware for guzzle for mirroring request to multiple clients:

Installation

To install, use composer:

composer require hasnat/guzzle-mirror-middleware

Usage

To use this middleware, you need to initialize it like:

Setup Mirror Clients:

$mirrorsMiddleware = new \GuzzleMirror\GuzzleMirrorMiddleware([
    'mirrors' => [
        [
            'client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror1.com/']),
            'ignore_mirror_failures'    => false, // default=false -- failures will be sent on ignored_failures_callback, on true MirrorFailedException is thrown
            'ignore_mirror_methods'     => ['GET'] // default=[] -- all the methods you'd like to ignore for this mirror
        ],
        [
            'client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror2.com/'])
        ],
        [
            'client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror3.com/'])
        ]
    ],
    'mirrors_concurrency'       => 4,  // default=4 -- send to 4 mirrors at a time
    'no_mirror_on_failure'      => true, // default=true -- if there's a failure in main request don't send to mirrors
    'mirror_responses'          => function(array $responses) {}, // default=null -- all mirror responses
    'ignored_failures_callback' => function(array $failures) {}, // default=null -- when ignoring failures no MirrorFailedException is thrown but you can get failures here
])

And inject it to Guzzle with something like:

$handlerStack = HandlerStack::create();
$handlerStack->push(new \GuzzleMirror\GuzzleMirrorMiddleware([
    'mirrors' => [
        ['client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror1.com/'])],
        ['client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror2.com/'])],
        ['client' => new \GuzzleHttp\Client(['base_uri' => 'http://mirror3.com/'])]
    ]
]));
$this->client = new GuzzleHttp\Client([
    'base_uri' => 'base_uri' => 'http://example.com/',
    'handler' => $handlerStack
]);

From now on every request sent with $guzzleClient will be replicated on mirrors.

Testing

hasnat/guzzle-mirror-middleware has a PHPUnit test suite and a coding style compliance test suite using PHP CS Fixer.

To run the tests, run the following command from the project folder.

$ docker-compose run test

License

MIT