Skip to content

Commit

Permalink
Guzzle Config (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Horat1us authored Feb 18, 2019
1 parent ecb4e16 commit 54f38ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
],
];
```
Expand Down
18 changes: 16 additions & 2 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
]);
}
);
Expand Down
17 changes: 16 additions & 1 deletion tests/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wearesho\Yii\Guzzle\Tests\Unit;

use GuzzleHttp;
use Wearesho\Yii\Guzzle\Bootstrap;
use Wearesho\Yii\Guzzle\Tests\TestCase;

Expand Down Expand Up @@ -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')
);
}
}

0 comments on commit 54f38ab

Please sign in to comment.