Skip to content
Permalink
Browse files

feature #31980 [HttpClient] make Psr18Client implement relevant PSR-1…

…7 factories (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] make Psr18Client implement relevant PSR-17 factories

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This should help use the component with libs that consume only PSR-18.

Commits
-------

1c0baf6 [HttpClient] make Psr18Client implement relevant PSR-17 factories
  • Loading branch information...
fabpot committed Jun 11, 2019
2 parents cc1fc5b + 1c0baf6 commit 71731c634256ea5617ca3604efb7c738aedbda7e
Showing with 71 additions and 1 deletion.
  1. +1 −0 src/Symfony/Component/HttpClient/CHANGELOG.md
  2. +70 −1 src/Symfony/Component/HttpClient/Psr18Client.php
@@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* made `Psr18Client` implement relevant PSR-17 factories
* added `$response->cancel()`

4.3.0
@@ -12,16 +12,26 @@
namespace Symfony\Component\HttpClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Uri;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
if (!interface_exists(RequestFactoryInterface::class)) {
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".');
}
if (!interface_exists(ClientInterface::class)) {
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-client" package is not installed. Try running "composer require psr/http-client".');
}
@@ -37,7 +47,7 @@
*
* @experimental in 4.3
*/
final class Psr18Client implements ClientInterface
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface
{
private $client;
private $responseFactory;
@@ -62,6 +72,9 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
}
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
try {
@@ -88,6 +101,62 @@ public function sendRequest(RequestInterface $request): ResponseInterface
throw new Psr18NetworkException($e, $request);
}
}
/**
* {@inheritdoc}
*/
public function createRequest(string $method, $uri): RequestInterface
{
if ($this->responseFactory instanceof RequestFactoryInterface) {
return $this->responseFactory->createRequest($method, $uri);
}
if (!class_exists(Request::class)) {
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
}
return new Request($method, $uri);
}
/**
* {@inheritdoc}
*/
public function createStream(string $content = ''): StreamInterface
{
return $this->streamFactory->createStream($content);
}
/**
* {@inheritdoc}
*/
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return $this->streamFactory->createStreamFromFile($filename, $mode);
}
/**
* {@inheritdoc}
*/
public function createStreamFromResource($resource): StreamInterface
{
return $this->streamFactory->createStreamFromResource($resource);
}
/**
* {@inheritdoc}
*/
public function createUri(string $uri = ''): UriInterface
{
if ($this->responseFactory instanceof UriFactoryInterface) {
return $this->responseFactory->createUri($uri);
}
if (!class_exists(Uri::class)) {
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
}
return new Uri($uri);
}
}
/**

0 comments on commit 71731c6

Please sign in to comment.
You can’t perform that action at this time.