Skip to content

Commit cd98d08

Browse files
authored
Merge pull request #442 from glaubinix/client-interface
Use ClientInterface instead of HttpClient
2 parents f6d2f5d + 912a0d0 commit cd98d08

23 files changed

+72
-106
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
1212
- Removed `message_factory`, `uri_factory`, and `stream_factory` classes config option. You can configure your own factories via psr17_*_factory classes config
1313
- Removed support for guzzle5-adapter
1414
- Removed support for Symfony versions <5.4
15+
- Changed the return type of `ClientFactory` to return a `ClientInterface` instead of `ClientInterface|HttpClient`
16+
- Changed the type of `httplug.client.default` to `ClientInterface` instead of `HttpClient`
17+
- Removed the `DummyClient` interface
1518

1619
# Version 1
1720

src/ClientFactory/AutoDiscoveryFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Http\HttplugBundle\ClientFactory;
66

7-
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\Psr18ClientDiscovery;
88

99
/**
1010
* Use auto discovery to find a HTTP client.
@@ -18,6 +18,6 @@ class AutoDiscoveryFactory implements ClientFactory
1818
*/
1919
public function createClient(array $config = [])
2020
{
21-
return HttpClientDiscovery::find();
21+
return Psr18ClientDiscovery::find();
2222
}
2323
}

src/ClientFactory/ClientFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Http\HttplugBundle\ClientFactory;
66

7-
use Http\Client\HttpClient;
87
use Psr\Http\Client\ClientInterface;
98

109
/**
@@ -13,9 +12,9 @@
1312
interface ClientFactory
1413
{
1514
/**
16-
* Input an array of configuration to be able to create a HttpClient.
15+
* Input an array of configuration to be able to create a ClientInterface.
1716
*
18-
* @return HttpClient|ClientInterface
17+
* @return ClientInterface
1918
*/
2019
public function createClient(array $config = []);
2120
}

src/ClientFactory/DummyClient.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Collector/PluginClientFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Http\Client\Common\Plugin;
88
use Http\Client\Common\PluginClient;
99
use Http\Client\HttpAsyncClient;
10-
use Http\Client\HttpClient;
1110
use Psr\Http\Client\ClientInterface;
1211
use Symfony\Component\Stopwatch\Stopwatch;
1312

@@ -44,9 +43,9 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc
4443
}
4544

4645
/**
47-
* @param HttpClient|ClientInterface|HttpAsyncClient $client
48-
* @param Plugin[] $plugins
49-
* @param array $options {
46+
* @param ClientInterface|HttpAsyncClient $client
47+
* @param Plugin[] $plugins
48+
* @param array $options {
5049
*
5150
* @var string $client_name to give client a name which may be used when displaying client information like in
5251
* the HTTPlugBundle profiler.

src/Collector/ProfileClient.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
use Http\Client\Common\VersionBridgeClient;
99
use Http\Client\Exception\HttpException;
1010
use Http\Client\HttpAsyncClient;
11-
use Http\Client\HttpClient;
1211
use Psr\Http\Client\ClientInterface;
1312
use Psr\Http\Message\RequestInterface;
1413
use Psr\Http\Message\ResponseInterface;
1514
use Symfony\Component\Stopwatch\Stopwatch;
1615
use Symfony\Component\Stopwatch\StopwatchEvent;
1716

1817
/**
19-
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
18+
* The ProfileClient decorates any client that implement both ClientInterface and HttpAsyncClient interfaces to gather target
2019
* url and response status code.
2120
*
2221
* @author Fabien Bourigault <[email protected]>
2322
*
2423
* @internal
2524
*/
26-
class ProfileClient implements HttpClient, HttpAsyncClient
25+
class ProfileClient implements ClientInterface, HttpAsyncClient
2726
{
2827
use VersionBridgeClient;
2928

3029
/**
31-
* @var HttpClient|HttpAsyncClient
30+
* @var ClientInterface|HttpAsyncClient
3231
*/
3332
private $client;
3433

@@ -55,12 +54,12 @@ class ProfileClient implements HttpClient, HttpAsyncClient
5554
private const STOPWATCH_CATEGORY = 'httplug';
5655

5756
/**
58-
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
59-
* HttpAsyncClient interface.
57+
* @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
58+
* HttpAsyncClient interface.
6059
*/
6160
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
6261
{
63-
if (!(($client instanceof ClientInterface || $client instanceof HttpClient) && $client instanceof HttpAsyncClient)) {
62+
if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) {
6463
$client = new FlexibleHttpClient($client);
6564
}
6665

src/Collector/ProfileClientFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Http\Client\Common\FlexibleHttpClient;
88
use Http\Client\HttpAsyncClient;
9-
use Http\Client\HttpClient;
109
use Http\HttplugBundle\ClientFactory\ClientFactory;
1110
use Psr\Http\Client\ClientInterface;
1211
use Symfony\Component\Stopwatch\Stopwatch;
@@ -61,7 +60,7 @@ public function createClient(array $config = [])
6160
{
6261
$client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config);
6362

64-
if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) {
63+
if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) {
6564
$client = new FlexibleHttpClient($client);
6665
}
6766

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function getConfigTreeBuilder(): TreeBuilder
112112
->children()
113113
->booleanNode('default_client_autowiring')
114114
->defaultTrue()
115-
->info('Set to false to not autowire HttpClient and HttpAsyncClient.')
115+
->info('Set to false to not autowire ClientInterface and HttpAsyncClient.')
116116
->end()
117117
->arrayNode('main_alias')
118118
->addDefaultsIfNotSet()

src/DependencyInjection/HttplugExtension.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Http\Client\Common\PluginClient;
1414
use Http\Client\Common\PluginClientFactory;
1515
use Http\Client\HttpAsyncClient;
16-
use Http\Client\HttpClient;
1716
use Http\Client\Plugin\Vcr\RecordPlugin;
1817
use Http\Client\Plugin\Vcr\ReplayPlugin;
1918
use Http\Message\Authentication\BasicAuth;
@@ -116,7 +115,7 @@ public function load(array $configs, ContainerBuilder $container): void
116115

117116
if (!$config['default_client_autowiring']) {
118117
$container->removeAlias(HttpAsyncClient::class);
119-
$container->removeAlias(HttpClient::class);
118+
$container->removeAlias(ClientInterface::class);
120119
}
121120

122121
if ($this->useVcrPlugin) {
@@ -413,7 +412,6 @@ private function configureClient(ContainerBuilder $container, $clientName, array
413412
{
414413
$serviceId = 'httplug.client.'.$clientName;
415414

416-
$container->registerAliasForArgument($serviceId, HttpClient::class, $clientName);
417415
$container->registerAliasForArgument($serviceId, ClientInterface::class, $clientName);
418416
$container->registerAliasForArgument($serviceId, HttpAsyncClient::class, $clientName);
419417

@@ -440,7 +438,7 @@ private function configureClient(ContainerBuilder $container, $clientName, array
440438

441439
if (empty($arguments['service'])) {
442440
$container
443-
->register($serviceId.'.client', HttpClient::class)
441+
->register($serviceId.'.client', ClientInterface::class)
444442
->setFactory([new Reference($arguments['factory']), 'createClient'])
445443
->addArgument($arguments['config'])
446444
->setPublic(false);

src/Discovery/ConfiguredClientsStrategy.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Http\HttplugBundle\Discovery;
66

77
use Http\Client\HttpAsyncClient;
8-
use Http\Client\HttpClient;
9-
use Http\Discovery\HttpClientDiscovery;
8+
use Http\Discovery\Psr18ClientDiscovery;
109
use Http\Discovery\Strategy\DiscoveryStrategy;
10+
use Psr\Http\Client\ClientInterface;
1111

1212
/**
1313
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
@@ -18,7 +18,7 @@
1818
class ConfiguredClientsStrategy implements DiscoveryStrategy
1919
{
2020
/**
21-
* @var HttpClient
21+
* @var ClientInterface
2222
*/
2323
private static $client;
2424

@@ -28,22 +28,22 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy
2828
private static $asyncClient;
2929

3030
/**
31-
* @param HttpClient $httpClient
31+
* @param ClientInterface $httpClient
3232
* @param HttpAsyncClient $asyncClient
3333
*/
34-
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
34+
public function __construct(ClientInterface $httpClient = null, HttpAsyncClient $asyncClient = null)
3535
{
3636
self::$client = $httpClient;
3737
self::$asyncClient = $asyncClient;
38-
HttpClientDiscovery::clearCache();
38+
Psr18ClientDiscovery::clearCache();
3939
}
4040

4141
/**
4242
* {@inheritdoc}
4343
*/
4444
public static function getCandidates($type)
4545
{
46-
if (HttpClient::class === $type && null !== self::$client) {
46+
if (ClientInterface::class === $type && null !== self::$client) {
4747
return [['class' => function () {
4848
return self::$client;
4949
}]];

0 commit comments

Comments
 (0)