Skip to content

Instantly share code, notes, and snippets.

@paulredmond
Last active April 7, 2017 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulredmond/509fdbf46d97c6d09498d246082f439f to your computer and use it in GitHub Desktop.
Save paulredmond/509fdbf46d97c6d09498d246082f439f to your computer and use it in GitHub Desktop.
Example Laravel AppServiceProvider class that consumes tagged Guzzle 6 middleware
<?php
namespace App\Providers;
use App\HMACRequestHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(HMACRequestHandler::class, function () {
return new HMACRequestHandler('key', 'secret');
});
$this->app->tag([
HMACRequestHandler::class,
], 'example.client.middleware');
$this->app->singleton('example.client', function () {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
foreach ($this->app->tagged('example.client.middleware') as $middleware) {
$stack->push($middleware);
}
return new Client(['handler' => $stack]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment