From 54f38abf8d46138811d3b4482e95a8a4a1095e09 Mon Sep 17 00:00:00 2001 From: Horat1us Date: Mon, 18 Feb 2019 16:03:46 +0200 Subject: [PATCH] Guzzle Config (#8) --- README.md | 14 +++++++++----- src/Bootstrap.php | 18 ++++++++++++++++-- tests/Unit/BootstrapTest.php | 17 ++++++++++++++++- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6fc8d19..e7dddb1 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,15 @@ use Wearesho\Yii\Guzzle; return [ 'bootstrap' => [ - 'http-log' => [ - 'class' => Guzzle\Bootstrap::class, - // Add regular expression if you need exclude their from logging - 'exclude' => ['/^.*(google).*$/iu'], - ], + 'http-log' => [ + 'class' => Guzzle\Bootstrap::class, + // Add regular expression if you need exclude their from logging + 'exclude' => ['/^.*(google).*$/iu'], + // Guzzle client configuration settings + 'config' => [ + 'timeout' => 10, + ], + ], ], ]; ``` diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 286609e..40e83f5 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -29,6 +29,16 @@ class Bootstrap extends base\BaseObject implements base\BootstrapInterface */ public $exclude = []; + /** + * Guzzle client configuration settings. + * @see \GuzzleHttp\Client::__construct() + * + * Note: you can't use handler key + * + * @var array + */ + public $config = []; + /** * @param base\Application $app * @@ -81,11 +91,15 @@ function ($reason) use ($logRequest) { \Yii::$container->set( GuzzleHttp\ClientInterface::class, - function ($container, $params, $config) use ($handlerStack) { + function ( + /** @noinspection PhpUnusedParameterInspection */ $container, + $params, + $config + ) use ($handlerStack) { return new GuzzleHttp\Client(...$params + [ 0 => $config + [ 'handler' => $handlerStack, - ] + ] + $this->config, ]); } ); diff --git a/tests/Unit/BootstrapTest.php b/tests/Unit/BootstrapTest.php index 07a2aed..8c2519a 100644 --- a/tests/Unit/BootstrapTest.php +++ b/tests/Unit/BootstrapTest.php @@ -2,6 +2,7 @@ namespace Wearesho\Yii\Guzzle\Tests\Unit; +use GuzzleHttp; use Wearesho\Yii\Guzzle\Bootstrap; use Wearesho\Yii\Guzzle\Tests\TestCase; @@ -32,12 +33,26 @@ protected function tearDown() public function testBootstrapApp(): void { $bootstrap = new Bootstrap([ - 'exclude' => ['/php.net/'] + 'exclude' => ['/php.net/'], + 'config' => [ + 'timeout' => 10, + ], ]); $bootstrap->bootstrap($this->app); $this->assertEquals( \Yii::getAlias('@vendor/wearesho-team/yii2-guzzle/src'), \Yii::getAlias('@Wearesho/Yii/Guzzle') ); + + /** @var GuzzleHttp\Client $client */ + $client = \Yii::$container->get(GuzzleHttp\ClientInterface::class); + $this->assertInstanceOf( + GuzzleHttp\Client::class, + $client + ); + $this->assertEquals( + 10, + $client->getConfig('timeout') + ); } }