Skip to content

Commit

Permalink
feature #31980 [HttpClient] make Psr18Client implement relevant PSR-1…
Browse files Browse the repository at this point in the history
…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 71731c6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

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

4.3.0
Expand Down
71 changes: 70 additions & 1 deletion src/Symfony/Component/HttpClient/Psr18Client.php
Expand Up @@ -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".');
}
Expand All @@ -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;
Expand All @@ -62,6 +72,9 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
}

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
try {
Expand All @@ -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);
}
}

/**
Expand Down

0 comments on commit 71731c6

Please sign in to comment.