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

Commit 957c306

Browse files
committed
Add PSR-17 compatibility layer
1 parent 597f30e commit 957c306

File tree

6 files changed

+220
-2
lines changed

6 files changed

+220
-2
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=5.4",
15-
"psr/http-message": "^1.0"
14+
"php": "^7.2",
15+
"psr/http-message": "^1.0",
16+
"psr/http-factory": "^1.0"
1617
},
1718
"autoload": {
1819
"psr-4": {

src/Psr17/MessageFactory.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Http\Message\Psr17;
4+
5+
use Psr\Http\Message\RequestFactoryInterface;
6+
use Psr\Http\Message\ResponseFactoryInterface;
7+
use Psr\Http\Message\StreamFactoryInterface;
8+
9+
/**
10+
* Compatibility layer for PSR-17 reqeust and response factory.
11+
*
12+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
13+
*/
14+
final class MessageFactory implements \Http\Message\MessageFactory
15+
{
16+
/**
17+
* @var \Http\Message\ResponseFactory
18+
*/
19+
private $requestFactory;
20+
21+
/**
22+
* @var \Http\Message\ResponseFactory
23+
*/
24+
private $responseFactory;
25+
26+
public function __construct(
27+
RequestFactoryInterface $requestFactory,
28+
ResponseFactoryInterface $responseFactory,
29+
StreamFactoryInterface $streamFactory
30+
) {
31+
$this->requestFactory = new RequestFactory($requestFactory, $streamFactory);
32+
$this->responseFactory = new ResponseFactory($responseFactory, $streamFactory);
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
39+
{
40+
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1')
47+
{
48+
return $this->responseFactory->createResponse($statusCode, $reasonPhrase, $headers, $body, $protocolVersion);
49+
}
50+
}

src/Psr17/RequestFactory.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Http\Message\Psr17;
4+
5+
use Psr\Http\Message\RequestFactoryInterface;
6+
use Psr\Http\Message\StreamFactoryInterface;
7+
8+
/**
9+
* Compatibility layer for PSR-17 request factory.
10+
*
11+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
12+
*/
13+
final class RequestFactory implements \Http\Message\RequestFactory
14+
{
15+
/**
16+
* @var RequestFactoryInterface
17+
*/
18+
private $requestFactory;
19+
20+
/**
21+
* @var \Http\Message\StreamFactory
22+
*/
23+
private $streamFactory;
24+
25+
public function __construct(RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory)
26+
{
27+
$this->requestFactory = $requestFactory;
28+
$this->streamFactory = new StreamFactory($streamFactory);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
35+
{
36+
$request = $this->requestFactory
37+
->createRequest($method, $uri)
38+
->withBody($this->streamFactory->createStream($body))
39+
->withProtocolVersion($protocolVersion)
40+
;
41+
42+
foreach ($headers as $name => $value) {
43+
$request = $request->withHeader($name, $value);
44+
}
45+
46+
return $request;
47+
}
48+
}

src/Psr17/ResponseFactory.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Http\Message\Psr17;
4+
5+
use Psr\Http\Message\ResponseFactoryInterface;
6+
use Psr\Http\Message\StreamFactoryInterface;
7+
8+
/**
9+
* Compatibility layer for PSR-17 response factory.
10+
*
11+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
12+
*/
13+
final class ResponseFactory implements \Http\Message\ResponseFactory
14+
{
15+
/**
16+
* @var ResponseFactoryInterface
17+
*/
18+
private $responseFactory;
19+
20+
/**
21+
* @var \Http\Message\StreamFactory
22+
*/
23+
private $streamFactory;
24+
25+
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
26+
{
27+
$this->responseFactory = $responseFactory;
28+
$this->streamFactory = new StreamFactory($streamFactory);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1')
35+
{
36+
$response = $this->responseFactory
37+
->createResponse($statusCode, (string) $reasonPhrase)
38+
->withBody($this->streamFactory->createStream($body))
39+
->withProtocolVersion($protocolVersion)
40+
;
41+
42+
foreach ($headers as $name => $value) {
43+
$response = $response->withHeader($name, $value);
44+
}
45+
46+
return $response;
47+
}
48+
}

src/Psr17/StreamFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Http\Message\Psr17;
4+
5+
use Psr\Http\Message\StreamFactoryInterface;
6+
use Psr\Http\Message\StreamInterface;
7+
8+
/**
9+
* Compatibility layer for PSR-17 stream factory.
10+
*
11+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
12+
*/
13+
final class StreamFactory implements \Http\Message\StreamFactory
14+
{
15+
/**
16+
* @var StreamFactoryInterface
17+
*/
18+
private $streamFactory;
19+
20+
public function __construct(StreamFactoryInterface $streamFactory)
21+
{
22+
$this->streamFactory = $streamFactory;
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function createStream($body = null)
29+
{
30+
if ($body instanceof StreamInterface) {
31+
return $body;
32+
}
33+
34+
if (is_resource($body)) {
35+
return $this->streamFactory->createStreamFromResource($body);
36+
}
37+
38+
return $this->streamFactory->createStream((string) $body);
39+
}
40+
}

src/Psr17/UriFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Http\Message\Psr17;
4+
5+
use Psr\Http\Message\UriFactoryInterface;
6+
7+
/**
8+
* Compatibility layer for PSR-17 URI factory.
9+
*
10+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
11+
*/
12+
final class UriFactory implements \Http\Message\UriFactory
13+
{
14+
/**
15+
* @var UriFactoryInterface
16+
*/
17+
private $uriFactory;
18+
19+
public function __construct(UriFactoryInterface $uriFactory)
20+
{
21+
$this->uriFactory = $uriFactory;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function createUri($uri)
28+
{
29+
return $this->uriFactory->createUri($uri);
30+
}
31+
}

0 commit comments

Comments
 (0)