Skip to content

Commit 6911039

Browse files
authored
Support for discovering psr-18 clients (#121)
* Support for discovering psr-18 clients * Make sure we can support non-httplug psr-18 clients * cs * Make sure one can find a PSR-18 client * minors * Created a PSR18ClientDiscovery * cs * s/PSR18/PSR-18
1 parent 5b62c39 commit 6911039

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": "^5.5 || ^7.0"
1515
},
1616
"require-dev": {
17-
"php-http/httplug": "^1.0|^2.0",
17+
"php-http/httplug": "^1.0 || ^2.0",
1818
"php-http/message-factory": "^1.0",
1919
"puli/composer-plugin": "1.0.0-beta10",
2020
"phpspec/phpspec": "^2.4",

src/Psr18ClientDiscovery.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Http\Discovery;
4+
5+
use Http\Discovery\Exception\DiscoveryFailedException;
6+
use Psr\Http\Client\ClientInterface;
7+
8+
/**
9+
* Finds a PSR-18 HTTP Client.
10+
*
11+
* @author Tobias Nyholm <[email protected]>
12+
*/
13+
final class Psr18ClientDiscovery extends ClassDiscovery
14+
{
15+
/**
16+
* Finds a PSR-18 HTTP Client.
17+
*
18+
* @return ClientInterface
19+
*
20+
* @throws Exception\NotFoundException
21+
*/
22+
public static function find()
23+
{
24+
try {
25+
$client = static::findOneByType(ClientInterface::class);
26+
} catch (DiscoveryFailedException $e) {
27+
throw new \Http\Discovery\Exception\NotFoundException(
28+
'No PSR-18 clients found. Make sure to install a package providing "psr/http-client-implementation". Example: "php-http/guzzle6-adapter".',
29+
0,
30+
$e
31+
);
32+
}
33+
34+
return static::instantiateClass($client);
35+
}
36+
}

src/Strategy/CommonClassesStrategy.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Http\Message\MessageFactory\DiactorosMessageFactory;
1616
use Http\Message\StreamFactory\DiactorosStreamFactory;
1717
use Http\Message\UriFactory\DiactorosUriFactory;
18+
use Psr\Http\Client\ClientInterface as Psr18Client;
1819
use Zend\Diactoros\Request as DiactorosRequest;
1920
use Http\Message\MessageFactory\SlimMessageFactory;
2021
use Http\Message\StreamFactory\SlimStreamFactory;
@@ -80,13 +81,27 @@ final class CommonClassesStrategy implements DiscoveryStrategy
8081
'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class],
8182
],
8283
],
84+
Psr18Client::class => [],
8385
];
8486

8587
/**
8688
* {@inheritdoc}
8789
*/
8890
public static function getCandidates($type)
8991
{
92+
if (Psr18Client::class === $type) {
93+
$candidates = self::$classes[PSR18Client::class];
94+
95+
// HTTPlug 2.0 clients implements PSR18Client too.
96+
foreach (self::$classes[HttpClient::class] as $c) {
97+
if (is_subclass_of($c['class'], Psr18Client::class)) {
98+
$candidates[] = $c;
99+
}
100+
}
101+
102+
return $candidates;
103+
}
104+
90105
if (isset(self::$classes[$type])) {
91106
return self::$classes[$type];
92107
}

0 commit comments

Comments
 (0)