Skip to content

Commit bcabbfc

Browse files
committed
Use ClientInterface instead of HttpClient
1 parent f6d2f5d commit bcabbfc

23 files changed

+69
-103
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: 1 addition & 2 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,7 +43,7 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc
4443
}
4544

4645
/**
47-
* @param HttpClient|ClientInterface|HttpAsyncClient $client
46+
* @param ClientInterface|HttpAsyncClient $client
4847
* @param Plugin[] $plugins
4948
* @param array $options {
5049
*

src/Collector/ProfileClient.php

Lines changed: 5 additions & 6 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 <bourigaultfabien@gmail.com>
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
57+
* @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
5958
* 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
}]];

src/Discovery/ConfiguredClientsStrategyListener.php

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

55
namespace Http\HttplugBundle\Discovery;
66

7-
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\Psr18ClientDiscovery;
88
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
99

1010
/**
@@ -17,7 +17,7 @@ class ConfiguredClientsStrategyListener implements EventSubscriberInterface
1717
*/
1818
public function onEvent()
1919
{
20-
HttpClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class);
20+
Psr18ClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class);
2121
}
2222

2323
/**

src/Resources/config/services.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<tag name="kernel.event_subscriber"/>
1414
</service>
1515

16-
<service id="httplug.auto_discovery.auto_discovered_client" class="Http\Client\HttpClient">
17-
<factory class="Http\Discovery\HttpClientDiscovery" method="find" />
16+
<service id="httplug.auto_discovery.auto_discovered_client" class="Psr\Http\Client\ClientInterface">
17+
<factory class="Http\Discovery\Psr18ClientDiscovery" method="find" />
1818
</service>
1919

2020
<service id="httplug.auto_discovery.auto_discovered_async" class="Http\Client\HttpAsyncClient">
@@ -27,8 +27,8 @@
2727
</service>
2828
<service id="Http\Client\HttpAsyncClient" alias="httplug.async_client.default" public="false" />
2929

30-
<service id="httplug.client.default" class="Http\Client\HttpClient">
31-
<factory class="Http\Discovery\HttpClientDiscovery" method="find" />
30+
<service id="httplug.client.default" class="Psr\Http\Client\ClientInterface">
31+
<factory class="Http\Discovery\Psr18ClientDiscovery" method="find" />
3232
</service>
3333
<service id="Psr\Http\Client\ClientInterface" alias="httplug.client" public="false" />
3434

src/Resources/config/services_legacy.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7-
<service id="Http\Client\HttpClient" alias="httplug.client" public="false">
7+
<service id="Psr\Http\Client\ClientInterface" alias="httplug.client" public="false">
88
<deprecated package="php-http/httplug-bundle" version="1.28">The "%alias_id%" service is deprecated in favor of using PSR-7 Psr\Http\Client\ClientInterface</deprecated>
99
</service>
1010
</services>

src/Resources/config/services_legacy_sf4.xml

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

tests/Functional/DiscoveredClientsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
use Http\Adapter\Guzzle7\Client;
88
use Http\Client\HttpAsyncClient;
9-
use Http\Client\HttpClient;
109
use Http\Discovery\HttpAsyncClientDiscovery;
11-
use Http\Discovery\HttpClientDiscovery;
10+
use Http\Discovery\Psr18ClientDiscovery;
1211
use Http\Discovery\Strategy\CommonClassesStrategy;
1312
use Http\Discovery\Strategy\CommonPsr17ClassesStrategy;
1413
use Http\HttplugBundle\Collector\ProfileClient;
1514
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener;
1615
use Nyholm\NSA;
16+
use Psr\Http\Client\ClientInterface;
1717
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1818

1919
class DiscoveredClientsTest extends WebTestCase
@@ -26,7 +26,7 @@ public function testDiscoveredClient(): void
2626

2727
$service = $container->get('httplug.auto_discovery.auto_discovered_client');
2828

29-
$this->assertInstanceOf(HttpClient::class, $service);
29+
$this->assertInstanceOf(ClientInterface::class, $service);
3030
}
3131

3232
public function testDiscoveredAsyncClient(): void
@@ -49,7 +49,7 @@ public function testDiscoveredClientWithProfilingEnabled(): void
4949
$service = $container->get('httplug.auto_discovery.auto_discovered_client');
5050

5151
$this->assertInstanceOf(ProfileClient::class, $service);
52-
$this->assertInstanceOf(HttpClient::class, NSA::getProperty($service, 'client'));
52+
$this->assertInstanceOf(ClientInterface::class, NSA::getProperty($service, 'client'));
5353
}
5454

5555
public function testDiscoveredAsyncClientWithProfilingEnabled(): void
@@ -81,7 +81,7 @@ public function testDiscovery(): void
8181
$httpAsyncClient = $container->get('httplug.auto_discovery.auto_discovered_async');
8282

8383
$this->assertInstanceOf(ProfileClient::class, $httpClient);
84-
$this->assertSame(HttpClientDiscovery::find(), $httpClient);
84+
$this->assertSame(Psr18ClientDiscovery::find(), $httpClient);
8585
$this->assertInstanceOf(ProfileClient::class, $httpAsyncClient);
8686
$this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
8787
}
@@ -115,7 +115,7 @@ public function testForcedDiscovery(): void
115115

116116
$container->get('httplug.strategy');
117117

118-
$this->assertEquals($container->get('httplug.client.acme'), HttpClientDiscovery::find());
118+
$this->assertEquals($container->get('httplug.client.acme'), Psr18ClientDiscovery::find());
119119
$this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
120120
}
121121

@@ -132,7 +132,7 @@ protected function setUp(): void
132132

133133
// Reset values
134134
$strategy = new ConfiguredClientsStrategyListener(null, null);
135-
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class, CommonPsr17ClassesStrategy::class]);
135+
Psr18ClientDiscovery::setStrategies([CommonClassesStrategy::class, CommonPsr17ClassesStrategy::class]);
136136
$strategy->onEvent();
137137
}
138138
}

0 commit comments

Comments
 (0)