Skip to content

Use ClientInterface instead of HttpClient #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Removed `message_factory`, `uri_factory`, and `stream_factory` classes config option. You can configure your own factories via psr17_*_factory classes config
- Removed support for guzzle5-adapter
- Removed support for Symfony versions <5.4
- Changed the return type of `ClientFactory` to return a `ClientInterface` instead of `ClientInterface|HttpClient`
- Changed the type of `httplug.client.default` to `ClientInterface` instead of `HttpClient`
- Removed the `DummyClient` interface

# Version 1

Expand Down
4 changes: 2 additions & 2 deletions src/ClientFactory/AutoDiscoveryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Http\HttplugBundle\ClientFactory;

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;

/**
* Use auto discovery to find a HTTP client.
Expand All @@ -18,6 +18,6 @@ class AutoDiscoveryFactory implements ClientFactory
*/
public function createClient(array $config = [])
{
return HttpClientDiscovery::find();
return Psr18ClientDiscovery::find();
}
}
5 changes: 2 additions & 3 deletions src/ClientFactory/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Http\HttplugBundle\ClientFactory;

use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
Expand All @@ -13,9 +12,9 @@
interface ClientFactory
{
/**
* Input an array of configuration to be able to create a HttpClient.
* Input an array of configuration to be able to create a ClientInterface.
*
* @return HttpClient|ClientInterface
* @return ClientInterface
*/
public function createClient(array $config = []);
}
19 changes: 0 additions & 19 deletions src/ClientFactory/DummyClient.php

This file was deleted.

7 changes: 3 additions & 4 deletions src/Collector/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\Stopwatch\Stopwatch;

Expand Down Expand Up @@ -44,9 +43,9 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc
}

/**
* @param HttpClient|ClientInterface|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param array $options {
* @param ClientInterface|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param array $options {
*
* @var string $client_name to give client a name which may be used when displaying client information like in
* the HTTPlugBundle profiler.
Expand Down
13 changes: 6 additions & 7 deletions src/Collector/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
use Http\Client\Common\VersionBridgeClient;
use Http\Client\Exception\HttpException;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;

/**
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
* The ProfileClient decorates any client that implement both ClientInterface and HttpAsyncClient interfaces to gather target
* url and response status code.
*
* @author Fabien Bourigault <[email protected]>
*
* @internal
*/
class ProfileClient implements HttpClient, HttpAsyncClient
class ProfileClient implements ClientInterface, HttpAsyncClient
{
use VersionBridgeClient;

/**
* @var HttpClient|HttpAsyncClient
* @var ClientInterface|HttpAsyncClient
*/
private $client;

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

/**
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
* HttpAsyncClient interface.
* @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
* HttpAsyncClient interface.
*/
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
{
if (!(($client instanceof ClientInterface || $client instanceof HttpClient) && $client instanceof HttpAsyncClient)) {
if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) {
$client = new FlexibleHttpClient($client);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Collector/ProfileClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Http\Client\Common\FlexibleHttpClient;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\ClientFactory\ClientFactory;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down Expand Up @@ -61,7 +60,7 @@ public function createClient(array $config = [])
{
$client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config);

if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) {
if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) {
$client = new FlexibleHttpClient($client);
}

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->booleanNode('default_client_autowiring')
->defaultTrue()
->info('Set to false to not autowire HttpClient and HttpAsyncClient.')
->info('Set to false to not autowire ClientInterface and HttpAsyncClient.')
->end()
->arrayNode('main_alias')
->addDefaultsIfNotSet()
Expand Down
6 changes: 2 additions & 4 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Plugin\Vcr\RecordPlugin;
use Http\Client\Plugin\Vcr\ReplayPlugin;
use Http\Message\Authentication\BasicAuth;
Expand Down Expand Up @@ -116,7 +115,7 @@ public function load(array $configs, ContainerBuilder $container): void

if (!$config['default_client_autowiring']) {
$container->removeAlias(HttpAsyncClient::class);
$container->removeAlias(HttpClient::class);
$container->removeAlias(ClientInterface::class);
}

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

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

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

if (empty($arguments['service'])) {
$container
->register($serviceId.'.client', HttpClient::class)
->register($serviceId.'.client', ClientInterface::class)
->setFactory([new Reference($arguments['factory']), 'createClient'])
->addArgument($arguments['config'])
->setPublic(false);
Expand Down
14 changes: 7 additions & 7 deletions src/Discovery/ConfiguredClientsStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Http\HttplugBundle\Discovery;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\Strategy\DiscoveryStrategy;
use Psr\Http\Client\ClientInterface;

/**
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
Expand All @@ -18,7 +18,7 @@
class ConfiguredClientsStrategy implements DiscoveryStrategy
{
/**
* @var HttpClient
* @var ClientInterface
*/
private static $client;

Expand All @@ -28,22 +28,22 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy
private static $asyncClient;

/**
* @param HttpClient $httpClient
* @param ClientInterface $httpClient
* @param HttpAsyncClient $asyncClient
*/
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
public function __construct(ClientInterface $httpClient = null, HttpAsyncClient $asyncClient = null)
{
self::$client = $httpClient;
self::$asyncClient = $asyncClient;
HttpClientDiscovery::clearCache();
Psr18ClientDiscovery::clearCache();
}

/**
* {@inheritdoc}
*/
public static function getCandidates($type)
{
if (HttpClient::class === $type && null !== self::$client) {
if (ClientInterface::class === $type && null !== self::$client) {
return [['class' => function () {
return self::$client;
}]];
Expand Down
4 changes: 2 additions & 2 deletions src/Discovery/ConfiguredClientsStrategyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Http\HttplugBundle\Discovery;

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -17,7 +17,7 @@ class ConfiguredClientsStrategyListener implements EventSubscriberInterface
*/
public function onEvent()
{
HttpClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class);
Psr18ClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<tag name="kernel.event_subscriber"/>
</service>

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

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

<service id="httplug.client.default" class="Http\Client\HttpClient">
<factory class="Http\Discovery\HttpClientDiscovery" method="find" />
<service id="httplug.client.default" class="Psr\Http\Client\ClientInterface">
<factory class="Http\Discovery\Psr18ClientDiscovery" method="find" />
</service>
<service id="Psr\Http\Client\ClientInterface" alias="httplug.client" public="false" />

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services_legacy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Http\Client\HttpClient" alias="httplug.client" public="false">
<service id="Psr\Http\Client\ClientInterface" alias="httplug.client" public="false">
<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>
</service>
</services>
Expand Down
11 changes: 0 additions & 11 deletions src/Resources/config/services_legacy_sf4.xml

This file was deleted.

14 changes: 7 additions & 7 deletions tests/Functional/DiscoveredClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use Http\Adapter\Guzzle7\Client;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\Strategy\CommonClassesStrategy;
use Http\Discovery\Strategy\CommonPsr17ClassesStrategy;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener;
use Nyholm\NSA;
use Psr\Http\Client\ClientInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

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

$this->assertInstanceOf(HttpClient::class, $service);
$this->assertInstanceOf(ClientInterface::class, $service);
}

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

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

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

$this->assertInstanceOf(ProfileClient::class, $httpClient);
$this->assertSame(HttpClientDiscovery::find(), $httpClient);
$this->assertSame(Psr18ClientDiscovery::find(), $httpClient);
$this->assertInstanceOf(ProfileClient::class, $httpAsyncClient);
$this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testForcedDiscovery(): void

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

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

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

// Reset values
$strategy = new ConfiguredClientsStrategyListener(null, null);
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class, CommonPsr17ClassesStrategy::class]);
Psr18ClientDiscovery::setStrategies([CommonClassesStrategy::class, CommonPsr17ClassesStrategy::class]);
$strategy->onEvent();
}
}
Loading