Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 8b330d3

Browse files
committed
Merge pull request #31 from php-http/adapter-create-with-config
add factory method to adapter client
2 parents a6b3ef8 + be31b57 commit 8b330d3

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 1.1.0 (unreleased)
4+
5+
### Added
6+
7+
- Factory method Client::createWithConfig to create an adapter with custom
8+
configuration for the underlying guzzle client.
39

410
## 1.0.0 - 2016-01-26
511

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
},
4444
"extra": {
4545
"branch-alias": {
46-
"dev-master": "1.0-dev"
46+
"dev-master": "1.1-dev"
4747
}
4848
},
49-
"prefer-stable": true,
50-
"minimum-stability": "beta"
49+
"prefer-stable": true
5150
}

src/Client.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,24 @@ class Client implements HttpClient, HttpAsyncClient
2828
public function __construct(ClientInterface $client = null)
2929
{
3030
if (!$client) {
31-
$handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
32-
$handlerStack->push(Middleware::prepareBody(), 'prepare_body');
33-
$client = new GuzzleClient(['handler' => $handlerStack]);
31+
$client = static::buildClient();
3432
}
3533

3634
$this->client = $client;
3735
}
3836

37+
/**
38+
* Factory method to create the guzzle 6 adapter with custom configuration for guzzle.
39+
*
40+
* @param array $config Configuration to create guzzle with.
41+
*
42+
* @return Client
43+
*/
44+
public static function createWithConfig(array $config)
45+
{
46+
return new self(static::buildClient($config));
47+
}
48+
3949
/**
4050
* {@inheritdoc}
4151
*/
@@ -55,4 +65,20 @@ public function sendAsyncRequest(RequestInterface $request)
5565

5666
return new Promise($promise, $request);
5767
}
68+
69+
/**
70+
* Build the guzzle client instance.
71+
*
72+
* @param array $config Additional configuration
73+
*
74+
* @return GuzzleClient
75+
*/
76+
private static function buildClient(array $config)
77+
{
78+
$handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
79+
$handlerStack->push(Middleware::prepareBody(), 'prepare_body');
80+
$config = array_merge(['handler' => $handlerStack], $config);
81+
82+
return new GuzzleClient($config);
83+
}
5884
}

0 commit comments

Comments
 (0)