From 957c306092ee59123f453e22847fc86e426517b5 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 6 Dec 2018 19:36:29 +0100 Subject: [PATCH] Add PSR-17 compatibility layer --- composer.json | 5 ++-- src/Psr17/MessageFactory.php | 50 +++++++++++++++++++++++++++++++++++ src/Psr17/RequestFactory.php | 48 +++++++++++++++++++++++++++++++++ src/Psr17/ResponseFactory.php | 48 +++++++++++++++++++++++++++++++++ src/Psr17/StreamFactory.php | 40 ++++++++++++++++++++++++++++ src/Psr17/UriFactory.php | 31 ++++++++++++++++++++++ 6 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 src/Psr17/MessageFactory.php create mode 100644 src/Psr17/RequestFactory.php create mode 100644 src/Psr17/ResponseFactory.php create mode 100644 src/Psr17/StreamFactory.php create mode 100644 src/Psr17/UriFactory.php diff --git a/composer.json b/composer.json index 7c72feb..c19798c 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,9 @@ } ], "require": { - "php": ">=5.4", - "psr/http-message": "^1.0" + "php": "^7.2", + "psr/http-message": "^1.0", + "psr/http-factory": "^1.0" }, "autoload": { "psr-4": { diff --git a/src/Psr17/MessageFactory.php b/src/Psr17/MessageFactory.php new file mode 100644 index 0000000..ea5a453 --- /dev/null +++ b/src/Psr17/MessageFactory.php @@ -0,0 +1,50 @@ + + */ +final class MessageFactory implements \Http\Message\MessageFactory +{ + /** + * @var \Http\Message\ResponseFactory + */ + private $requestFactory; + + /** + * @var \Http\Message\ResponseFactory + */ + private $responseFactory; + + public function __construct( + RequestFactoryInterface $requestFactory, + ResponseFactoryInterface $responseFactory, + StreamFactoryInterface $streamFactory + ) { + $this->requestFactory = new RequestFactory($requestFactory, $streamFactory); + $this->responseFactory = new ResponseFactory($responseFactory, $streamFactory); + } + + /** + * {@inheritdoc} + */ + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') + { + return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion); + } + + /** + * {@inheritdoc} + */ + public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1') + { + return $this->responseFactory->createResponse($statusCode, $reasonPhrase, $headers, $body, $protocolVersion); + } +} diff --git a/src/Psr17/RequestFactory.php b/src/Psr17/RequestFactory.php new file mode 100644 index 0000000..be3e6b1 --- /dev/null +++ b/src/Psr17/RequestFactory.php @@ -0,0 +1,48 @@ + + */ +final class RequestFactory implements \Http\Message\RequestFactory +{ + /** + * @var RequestFactoryInterface + */ + private $requestFactory; + + /** + * @var \Http\Message\StreamFactory + */ + private $streamFactory; + + public function __construct(RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory) + { + $this->requestFactory = $requestFactory; + $this->streamFactory = new StreamFactory($streamFactory); + } + + /** + * {@inheritdoc} + */ + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') + { + $request = $this->requestFactory + ->createRequest($method, $uri) + ->withBody($this->streamFactory->createStream($body)) + ->withProtocolVersion($protocolVersion) + ; + + foreach ($headers as $name => $value) { + $request = $request->withHeader($name, $value); + } + + return $request; + } +} diff --git a/src/Psr17/ResponseFactory.php b/src/Psr17/ResponseFactory.php new file mode 100644 index 0000000..adae92b --- /dev/null +++ b/src/Psr17/ResponseFactory.php @@ -0,0 +1,48 @@ + + */ +final class ResponseFactory implements \Http\Message\ResponseFactory +{ + /** + * @var ResponseFactoryInterface + */ + private $responseFactory; + + /** + * @var \Http\Message\StreamFactory + */ + private $streamFactory; + + public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) + { + $this->responseFactory = $responseFactory; + $this->streamFactory = new StreamFactory($streamFactory); + } + + /** + * {@inheritdoc} + */ + public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1') + { + $response = $this->responseFactory + ->createResponse($statusCode, (string) $reasonPhrase) + ->withBody($this->streamFactory->createStream($body)) + ->withProtocolVersion($protocolVersion) + ; + + foreach ($headers as $name => $value) { + $response = $response->withHeader($name, $value); + } + + return $response; + } +} diff --git a/src/Psr17/StreamFactory.php b/src/Psr17/StreamFactory.php new file mode 100644 index 0000000..2338723 --- /dev/null +++ b/src/Psr17/StreamFactory.php @@ -0,0 +1,40 @@ + + */ +final class StreamFactory implements \Http\Message\StreamFactory +{ + /** + * @var StreamFactoryInterface + */ + private $streamFactory; + + public function __construct(StreamFactoryInterface $streamFactory) + { + $this->streamFactory = $streamFactory; + } + + /** + * {@inheritdoc} + */ + public function createStream($body = null) + { + if ($body instanceof StreamInterface) { + return $body; + } + + if (is_resource($body)) { + return $this->streamFactory->createStreamFromResource($body); + } + + return $this->streamFactory->createStream((string) $body); + } +} diff --git a/src/Psr17/UriFactory.php b/src/Psr17/UriFactory.php new file mode 100644 index 0000000..8a1f5bb --- /dev/null +++ b/src/Psr17/UriFactory.php @@ -0,0 +1,31 @@ + + */ +final class UriFactory implements \Http\Message\UriFactory +{ + /** + * @var UriFactoryInterface + */ + private $uriFactory; + + public function __construct(UriFactoryInterface $uriFactory) + { + $this->uriFactory = $uriFactory; + } + + /** + * {@inheritdoc} + */ + public function createUri($uri) + { + return $this->uriFactory->createUri($uri); + } +}