Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Add PSR-17 compatibility layer #38

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
50 changes: 50 additions & 0 deletions src/Psr17/MessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Http\Message\Psr17;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* Compatibility layer for PSR-17 reqeust and response factory.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class MessageFactory implements \Http\Message\MessageFactory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i usually use such a thing and specify an alias like BaseMessageFactory, or in this case maybe LegacyMessageFactory?

{
/**
* @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);
}
}
48 changes: 48 additions & 0 deletions src/Psr17/RequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Http\Message\Psr17;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* Compatibility layer for PSR-17 request factory.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is where PSR-17 and HTTPlug differ, right?

{
$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;
}
}
48 changes: 48 additions & 0 deletions src/Psr17/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Http\Message\Psr17;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* Compatibility layer for PSR-17 response factory.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
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;
}
}
40 changes: 40 additions & 0 deletions src/Psr17/StreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Http\Message\Psr17;

use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

/**
* Compatibility layer for PSR-17 stream factory.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
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);
}
}
31 changes: 31 additions & 0 deletions src/Psr17/UriFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Http\Message\Psr17;

use Psr\Http\Message\UriFactoryInterface;

/**
* Compatibility layer for PSR-17 URI factory.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
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);
}
}