Skip to content

Commit d735f73

Browse files
committed
#22: Cannot create the client using HttpClientDiscovery
1 parent a31fb78 commit d735f73

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
"php-http/httplug": "^1.0",
1919
"php-http/message-factory": "^1.0"
2020
},
21+
"suggest": {
22+
"php-http/discovery": "Allow automatically discover needed HTTPlug implementations"
23+
},
2124
"require-dev": {
2225
"guzzlehttp/psr7": "^1.0",
2326
"php-http/adapter-integration-tests": "0.4.*",
2427
"php-http/message": "^1.2",
2528
"php-http/discovery": "~0.8.0",
2629
"phpunit/phpunit": "^4.8",
27-
"puli/composer-plugin": "1.0.0-beta9",
30+
"puli/composer-plugin": "^1.0@beta,>=1.0.0-beta9",
2831
"zendframework/zend-diactoros": "^1.0"
2932
},
3033
"autoload": {

puli.json

+5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
"installer": "composer",
189189
"env": "dev"
190190
},
191+
"symfony/filesystem": {
192+
"install-path": "vendor/symfony/filesystem",
193+
"installer": "composer",
194+
"env": "dev"
195+
},
191196
"symfony/process": {
192197
"install-path": "vendor/symfony/process",
193198
"installer": "composer",

src/Client.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Http\Client\Exception\RequestException;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
8+
use Http\Discovery\MessageFactoryDiscovery;
9+
use Http\Discovery\StreamFactoryDiscovery;
810
use Http\Message\MessageFactory;
911
use Http\Message\StreamFactory;
1012
use Http\Promise\Promise;
@@ -24,6 +26,11 @@
2426
*/
2527
class Client implements HttpClient, HttpAsyncClient
2628
{
29+
/**
30+
* @access private
31+
*/
32+
const DEPENDENCY_MSG = 'You should either provide $%s argument or install "php-http/discovery"';
33+
2734
/**
2835
* cURL options
2936
*
@@ -62,19 +69,35 @@ class Client implements HttpClient, HttpAsyncClient
6269
/**
6370
* Create new client
6471
*
65-
* @param MessageFactory $messageFactory HTTP Message factory
66-
* @param StreamFactory $streamFactory HTTP Stream factory
67-
* @param array $options cURL options (see http://php.net/curl_setopt)
72+
* @param MessageFactory|null $messageFactory HTTP Message factory
73+
* @param StreamFactory|null $streamFactory HTTP Stream factory
74+
* @param array $options cURL options (see http://php.net/curl_setopt)
75+
*
76+
* @throws \LogicException If some factory not provided and php-http/discovery not installed
6877
*
6978
* @since 1.0
7079
*/
7180
public function __construct(
72-
MessageFactory $messageFactory,
73-
StreamFactory $streamFactory,
81+
MessageFactory $messageFactory = null,
82+
StreamFactory $streamFactory = null,
7483
array $options = []
7584
) {
85+
if (null === $messageFactory) {
86+
if (!class_exists(MessageFactoryDiscovery::class)) {
87+
throw new \LogicException(sprintf(self::DEPENDENCY_MSG, 'messageFactory'));
88+
}
89+
$messageFactory = MessageFactoryDiscovery::find();
90+
}
7691
$this->messageFactory = $messageFactory;
92+
93+
if (null === $streamFactory) {
94+
if (!class_exists(StreamFactoryDiscovery::class)) {
95+
throw new \LogicException(sprintf(self::DEPENDENCY_MSG, 'streamFactory'));
96+
}
97+
$streamFactory = StreamFactoryDiscovery::find();
98+
}
7799
$this->streamFactory = $streamFactory;
100+
78101
$this->options = $options;
79102
}
80103

tests/ClientTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public function testExpectHeader()
3030

3131
static::assertContains('Expect:', $headers);
3232
}
33+
34+
/**
35+
* Discovery should be used if no factory given.
36+
*/
37+
public function testFactoryDiscovery()
38+
{
39+
$client = new Client;
40+
41+
static::assertInstanceOf(Client::class, $client);
42+
}
3343
}

0 commit comments

Comments
 (0)