Last active
April 7, 2017 06:17
-
-
Save paulredmond/509fdbf46d97c6d09498d246082f439f to your computer and use it in GitHub Desktop.
Example Laravel AppServiceProvider class that consumes tagged Guzzle 6 middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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