Skip to content

Commit 7603529

Browse files
committed
support automatic client and facotry discovery
1 parent 91f1fb4 commit 7603529

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ matrix:
2020
include:
2121
- php: 5.4
2222
env: deps="low"
23+
- php: 5.6
24+
env: PACKAGES="php-http/discovery:^1.0 php-http/guzzle6-adapter:^1.0 php-http/message:^1.0"
2325
- php: 7.0
2426
env: xdebug="yes"
2527

@@ -28,6 +30,7 @@ before_install:
2830
- composer self-update
2931

3032
install:
33+
- if [ "$PACKAGES" != "" ]; then composer require --no-update $PACKAGES; fi
3134
- if [ "$deps" = "low" ]; then composer update --prefer-lowest --prefer-stable --ignore-platform-reqs; fi
3235
- if [ "$deps" = "" ]; then composer install; fi
3336

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ CHANGELOG
4040
->build();
4141
```
4242

43+
You can avoid calling `setHttpClient()` and `setRequestFactory` by installing
44+
the [HTTP discovery](http://php-http.org/en/latest/discovery.html) package.
45+
4346
* Bumped the required versions of all `php-xapi` packages to the `1.x` release
4447
series.
4548

UPGRADE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Upgrading from 0.4 to 0.5
4040
->build();
4141
```
4242

43+
You can avoid calling `setHttpClient()` and `setRequestFactory` by installing
44+
the [HTTP discovery](http://php-http.org/en/latest/discovery.html) package.
45+
4346
Upgrading from 0.2 to 0.3
4447
-------------------------
4548

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"php-http/mock-client": "^0.3",
3131
"php-xapi/test-fixtures": "^1.0"
3232
},
33+
"suggest": {
34+
"php-http/discovery": "For automatic discovery of HTTP clients and request factories"
35+
},
3336
"conflict": {
3437
"xabbuh/xapi-client": "*"
3538
},

spec/XApiClientBuilderSpec.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace spec\Xabbuh\XApi\Client;
44

55
use Http\Client\HttpClient;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\MessageFactoryDiscovery;
68
use Http\Message\RequestFactory;
9+
use PhpSpec\Exception\Example\SkippingException;
710
use PhpSpec\ObjectBehavior;
811

912
class XApiClientBuilderSpec extends ObjectBehavior
@@ -33,6 +36,10 @@ function its_methods_can_be_chained(HttpClient $httpClient, RequestFactory $requ
3336

3437
function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFactory $requestFactory)
3538
{
39+
if ($this->isAbleToDiscoverHttpClient()) {
40+
throw new SkippingException('The builder does not throw an exception if it can automatically discover an HTTP client.');
41+
}
42+
3643
$this->setRequestFactory($requestFactory);
3744
$this->setBaseUrl('http://example.com/xapi/');
3845

@@ -41,17 +48,62 @@ function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFact
4148

4249
function it_throws_an_exception_if_the_request_factory_is_not_configured(HttpClient $httpClient)
4350
{
51+
if ($this->isAbleToDiscoverRequestFactory()) {
52+
throw new SkippingException('The builder does not throw an exception if it can automatically discover a request factory.');
53+
}
54+
4455
$this->setHttpClient($httpClient);
4556
$this->setBaseUrl('http://example.com/xapi/');
4657

4758
$this->shouldThrow('\LogicException')->during('build');
4859
}
4960

61+
function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and_the_request_factory_without_configuring_them_explicitly()
62+
{
63+
if (!class_exists('\Http\Discovery\HttpClientDiscovery')) {
64+
throw new SkippingException('The "\Http\Discovery\HttpClientDiscovery" class is required to let the builder auto discover the HTTP client and request factory.');
65+
}
66+
67+
if (!$this->isAbleToDiscoverHttpClient()) {
68+
throw new SkippingException('Unable to discover an HTTP client.');
69+
}
70+
71+
if (!$this->isAbleToDiscoverRequestFactory()) {
72+
throw new SkippingException('Unable to discover a request factory.');
73+
}
74+
75+
$this->setBaseUrl('http://example.com/xapi/');
76+
77+
$this->build()->shouldReturnAnInstanceOf('\Xabbuh\XApi\Client\XApiClientInterface');
78+
}
79+
5080
function it_throws_an_exception_if_the_base_uri_is_not_configured(HttpClient $httpClient, RequestFactory $requestFactory)
5181
{
5282
$this->setHttpClient($httpClient);
5383
$this->setRequestFactory($requestFactory);
5484

5585
$this->shouldThrow('\LogicException')->during('build');
5686
}
87+
88+
private function isAbleToDiscoverHttpClient()
89+
{
90+
try {
91+
HttpClientDiscovery::find();
92+
93+
return true;
94+
} catch (\Exception $e) {
95+
return false;
96+
}
97+
}
98+
99+
private function isAbleToDiscoverRequestFactory()
100+
{
101+
try {
102+
MessageFactoryDiscovery::find();
103+
104+
return true;
105+
} catch (\Exception $e) {
106+
return false;
107+
}
108+
}
57109
}

src/XApiClientBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Http\Client\Common\Plugin\AuthenticationPlugin;
1515
use Http\Client\Common\PluginClient;
1616
use Http\Client\HttpClient;
17+
use Http\Discovery\HttpClientDiscovery;
18+
use Http\Discovery\MessageFactoryDiscovery;
1719
use Http\Message\Authentication\BasicAuth;
1820
use Http\Message\RequestFactory;
1921
use Xabbuh\XApi\Client\Request\Handler;
@@ -122,10 +124,24 @@ public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $toke
122124
*/
123125
public function build()
124126
{
127+
if (null === $this->httpClient && class_exists('\Http\Discovery\HttpClientDiscovery')) {
128+
try {
129+
$this->httpClient = HttpClientDiscovery::find();
130+
} catch (\Exception $e) {
131+
}
132+
}
133+
125134
if (null === $httpClient = $this->httpClient) {
126135
throw new \LogicException('No HTTP client was configured.');
127136
}
128137

138+
if (null === $this->requestFactory && class_exists('\Http\Discovery\MessageFactoryDiscovery')) {
139+
try {
140+
$this->requestFactory = MessageFactoryDiscovery::find();
141+
} catch (\Exception $e) {
142+
}
143+
}
144+
129145
if (null === $this->requestFactory) {
130146
throw new \LogicException('No request factory was configured.');
131147
}

0 commit comments

Comments
 (0)