Skip to content

Commit 43d7aee

Browse files
Merge branch '4.4'
* 4.4: extend legacy exception for backwards compatibility [HttpClient] workaround curl_multi_select() issue [CI] fix building local packages Add show-deprecations option to lint:twig command [HttpClient] try using php-http/discovery when nyholm/psr7 is not installed [FrameworkBundle] Improve the sorting of tagged services [HttpClient] add HttpClient::createForBaseUri() Increase limits for flakey appveyor tests
2 parents 3b602b1 + 11e2eb6 commit 43d7aee

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `StreamWrapper`
7+
* added `HttpClient::createForBaseUri()`
88
* added `HttplugClient` with support for sync and async requests
99
* added `max_duration` option
1010
* added support for NTLM authentication
11+
* added `StreamWrapper` to cast any `ResponseInterface` instances to PHP streams.
1112
* added `$response->toStream()` to cast responses to regular PHP streams
1213
* made `Psr18Client` implement relevant PSR-17 factories and have streaming responses
1314
* added `TraceableHttpClient`, `HttpClientDataCollector` and `HttpClientPass` to integrate with the web profiler

HttpClient.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public static function create(array $defaultOptions = [], int $maxHostConnection
3939

4040
return new NativeHttpClient($defaultOptions, $maxHostConnections);
4141
}
42+
43+
/**
44+
* Creates a client that adds options (e.g. authentication headers) only when the request URL matches the provided base URI.
45+
*/
46+
public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
47+
{
48+
$client = self::create([], $maxHostConnections, $maxPendingPushes);
49+
50+
return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions);
51+
}
4252
}

HttplugClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Http\Client\Exception\RequestException;
1717
use Http\Client\HttpAsyncClient;
1818
use Http\Client\HttpClient as HttplugInterface;
19+
use Http\Discovery\Psr17FactoryDiscovery;
1920
use Http\Message\RequestFactory;
2021
use Http\Message\StreamFactory;
2122
use Http\Message\UriFactory;
@@ -70,13 +71,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
7071
$this->promisePool = \function_exists('GuzzleHttp\Promise\queue') ? new \SplObjectStorage() : null;
7172

7273
if (null === $this->responseFactory || null === $this->streamFactory) {
73-
if (!class_exists(Psr17Factory::class)) {
74+
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
7475
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
7576
}
7677

77-
$psr17Factory = new Psr17Factory();
78-
$this->responseFactory = $this->responseFactory ?? $psr17Factory;
79-
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
78+
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
79+
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
80+
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
8081
}
8182

8283
$this->waitLoop = new HttplugWaitLoop($this->client, $this->promisePool, $this->responseFactory, $this->streamFactory);
@@ -144,10 +145,12 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
144145
{
145146
if ($this->responseFactory instanceof RequestFactoryInterface) {
146147
$request = $this->responseFactory->createRequest($method, $uri);
147-
} elseif (!class_exists(Request::class)) {
148-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
149-
} else {
148+
} elseif (class_exists(Request::class)) {
150149
$request = new Request($method, $uri);
150+
} elseif (class_exists(Psr17FactoryDiscovery::class)) {
151+
$request = Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
152+
} else {
153+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
151154
}
152155

153156
$request = $request
@@ -199,11 +202,15 @@ public function createUri($uri): UriInterface
199202
return $this->responseFactory->createUri($uri);
200203
}
201204

202-
if (!class_exists(Uri::class)) {
203-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
205+
if (class_exists(Uri::class)) {
206+
return new Uri($uri);
207+
}
208+
209+
if (class_exists(Psr17FactoryDiscovery::class)) {
210+
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
204211
}
205212

206-
return new Uri($uri);
213+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
207214
}
208215

209216
public function __destruct()

Psr18Client.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Http\Discovery\Psr17FactoryDiscovery;
1415
use Nyholm\Psr7\Factory\Psr17Factory;
1516
use Nyholm\Psr7\Request;
1617
use Nyholm\Psr7\Uri;
@@ -63,13 +64,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
6364
return;
6465
}
6566

66-
if (!class_exists(Psr17Factory::class)) {
67+
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
6768
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
6869
}
6970

70-
$psr17Factory = new Psr17Factory();
71-
$this->responseFactory = $this->responseFactory ?? $psr17Factory;
72-
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
71+
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
72+
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
73+
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
7374
}
7475

7576
/**
@@ -124,11 +125,15 @@ public function createRequest(string $method, $uri): RequestInterface
124125
return $this->responseFactory->createRequest($method, $uri);
125126
}
126127

127-
if (!class_exists(Request::class)) {
128-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
128+
if (class_exists(Request::class)) {
129+
return new Request($method, $uri);
129130
}
130131

131-
return new Request($method, $uri);
132+
if (class_exists(Psr17FactoryDiscovery::class)) {
133+
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
134+
}
135+
136+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
132137
}
133138

134139
/**
@@ -170,11 +175,15 @@ public function createUri(string $uri = ''): UriInterface
170175
return $this->responseFactory->createUri($uri);
171176
}
172177

173-
if (!class_exists(Uri::class)) {
174-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
178+
if (class_exists(Uri::class)) {
179+
return new Uri($uri);
180+
}
181+
182+
if (class_exists(Psr17FactoryDiscovery::class)) {
183+
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
175184
}
176185

177-
return new Uri($uri);
186+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
178187
}
179188
}
180189

Response/CurlResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ private static function perform(CurlClientState $multi, array &$responses = null
286286
*/
287287
private static function select(CurlClientState $multi, float $timeout): int
288288
{
289+
if (\PHP_VERSION_ID < 70123 || (70200 <= \PHP_VERSION_ID && \PHP_VERSION_ID < 70211)) {
290+
// workaround https://bugs.php.net/76480
291+
$timeout = min($timeout, 0.01);
292+
}
293+
289294
return curl_multi_select($multi->handle, $timeout);
290295
}
291296

0 commit comments

Comments
 (0)