Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpClient] Add HttpClientInterface::withOptions() #40306

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
Feb 25, 2021
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
2 changes: 1 addition & 1 deletion 2 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"url": "src/Symfony/Contracts",
"options": {
"versions": {
"symfony/contracts": "2.3.x-dev"
"symfony/contracts": "2.4.x-dev"
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa

return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class));
}

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);

return $clone;
}
}
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3
---

* Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4

5.2.0
-----

Expand Down
28 changes: 3 additions & 25 deletions 28 src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,30 +341,8 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa

public function reset()
{
if ($this->logger) {
foreach ($this->multi->pushedResponses as $url => $response) {
$this->logger->debug(sprintf('Unused pushed response: "%s"', $url));
}
}

$this->multi->pushedResponses = [];
$this->multi->dnsCache->evictions = $this->multi->dnsCache->evictions ?: $this->multi->dnsCache->removals;
$this->multi->dnsCache->removals = $this->multi->dnsCache->hostnames = [];

if (\is_resource($this->multi->handle) || $this->multi->handle instanceof \CurlMultiHandle) {
if (\defined('CURLMOPT_PUSHFUNCTION')) {
curl_multi_setopt($this->multi->handle, \CURLMOPT_PUSHFUNCTION, null);
}

$active = 0;
while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi->handle, $active));
}

foreach ($this->multi->openHandles as [$ch]) {
if (\is_resource($ch) || $ch instanceof \CurlHandle) {
curl_setopt($ch, \CURLOPT_VERBOSE, false);
}
}
$this->multi->logger = $this->logger;
$this->multi->reset();
}

public function __sleep()
Expand All @@ -379,7 +357,7 @@ public function __wakeup()

public function __destruct()
{
$this->reset();
$this->multi->logger = $this->logger;
}

private function handlePush($parent, $pushed, array $requestHeaders, int $maxPendingPushes): int
Expand Down
5 changes: 3 additions & 2 deletions 5 src/Symfony/Component/HttpClient/EventSourceHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
*/
final class EventSourceHttpClient implements HttpClientInterface
{
use AsyncDecoratorTrait;
use HttpClientTrait;
use AsyncDecoratorTrait, HttpClientTrait {
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;
}

private $reconnectionTime;

Expand Down
13 changes: 12 additions & 1 deletion 13 src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@
/**
* Provides the common logic from writing HttpClientInterface implementations.
*
* All methods are static to prevent implementers from creating memory leaks via circular references.
* All private methods are static to prevent implementers from creating memory leaks via circular references.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait HttpClientTrait
{
private static $CHUNK_SIZE = 16372;

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions);

return $clone;
}

/**
* Validates and normalizes method, URL and options, and merges them with defaults.
*
Expand Down
47 changes: 47 additions & 0 deletions 47 src/Symfony/Component/HttpClient/Internal/CurlClientState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpClient\Internal;

use Psr\Log\LoggerInterface;

/**
* Internal representation of the cURL client's state.
*
Expand All @@ -29,10 +31,55 @@ final class CurlClientState extends ClientState
/** @var float[] */
public $pauseExpiries = [];
public $execCounter = \PHP_INT_MIN;
/** @var LoggerInterface|null */
public $logger;

public function __construct()
{
$this->handle = curl_multi_init();
$this->dnsCache = new DnsCache();
}

public function reset()
{
if ($this->logger) {
foreach ($this->pushedResponses as $url => $response) {
$this->logger->debug(sprintf('Unused pushed response: "%s"', $url));
}
}

$this->pushedResponses = [];
$this->dnsCache->evictions = $this->dnsCache->evictions ?: $this->dnsCache->removals;
$this->dnsCache->removals = $this->dnsCache->hostnames = [];

if (\is_resource($this->handle) || $this->handle instanceof \CurlMultiHandle) {
if (\defined('CURLMOPT_PUSHFUNCTION')) {
curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, null);
}

$active = 0;
while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->handle, $active));
}

foreach ($this->openHandles as [$ch]) {
if (\is_resource($ch) || $ch instanceof \CurlHandle) {
curl_setopt($ch, \CURLOPT_VERBOSE, false);
}
}
}

public function __sleep()
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
}

public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}

public function __destruct()
{
$this->reset();
}
}
17 changes: 14 additions & 3 deletions 17 src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class MockHttpClient implements HttpClientInterface
use HttpClientTrait;

private $responseFactory;
private $baseUri;
private $requestsCount = 0;
private $defaultOptions = [];

/**
* @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
Expand All @@ -47,15 +47,15 @@ public function __construct($responseFactory = null, string $baseUri = null)
}

$this->responseFactory = $responseFactory;
$this->baseUri = $baseUri;
$this->defaultOptions['base_uri'] = $baseUri;
}

/**
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = $this->prepareRequest($method, $url, $options, ['base_uri' => $this->baseUri], true);
[$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true);
$url = implode('', $url);

if (null === $this->responseFactory) {
Expand Down Expand Up @@ -96,4 +96,15 @@ public function getRequestsCount(): int
{
return $this->requestsCount;
}

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions, true);

return $clone;
}
}
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpClient/NoPrivateNetworkHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public function setLogger(LoggerInterface $logger): void
$this->client->setLogger($logger);
}
}

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);

return $clone;
}
}
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpClient/ScopingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public function setLogger(LoggerInterface $logger): void
$this->client->setLogger($logger);
}
}

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);

return $clone;
}
}
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpClient/TraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@ public function setLogger(LoggerInterface $logger): void
$this->client->setLogger($logger);
}
}

/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);

return $clone;
}
}
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"php-http/async-client-implementation": "*",
"php-http/client-implementation": "*",
"psr/http-client-implementation": "1.0",
"symfony/http-client-implementation": "2.2"
"symfony/http-client-implementation": "2.4"
},
"require": {
"php": ">=7.2.5",
"psr/log": "^1.0",
"symfony/http-client-contracts": "^2.2",
"symfony/http-client-contracts": "^2.4",
"symfony/polyfill-php73": "^1.11",
"symfony/polyfill-php80": "^1.15",
"symfony/service-contracts": "^1.0|^2"
Expand Down
5 changes: 5 additions & 0 deletions 5 src/Symfony/Contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.4
---

* Add `HttpClientInterface::withOptions()`

2.3.0
-----

Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/Deprecation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/EventDispatcher/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Contracts/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
* @see HttpClientTestCase for a reference test suite
*
* @method static withOptions(array $options) Returns a new instance of the client with new default options
chalasr marked this conversation as resolved.
Show resolved Hide resolved
*
* @author Nicolas Grekas <p@tchwork.com>
*/
interface HttpClientInterface
Expand Down
16 changes: 16 additions & 0 deletions 16 src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,20 @@ public function testMaxDuration()

$this->assertLessThan(10, $duration);
}

public function testWithOptions()
{
$client = $this->getHttpClient(__FUNCTION__);
if (!method_exists($client, 'withOptions')) {
$this->markTestSkipped(sprintf('Not implementing "%s::withOptions()" is deprecated.', get_debug_type($client)));
}

$client2 = $client->withOptions(['base_uri' => 'http://localhost:8057/']);

$this->assertNotSame($client, $client2);
$this->assertSame(\get_class($client), \get_class($client2));

$response = $client2->request('GET', '/');
$this->assertSame(200, $response->getStatusCode());
}
}
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/Service/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Contracts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "2.3-dev"
"dev-main": "2.4-dev"
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.