summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hernandez <[email protected]>2018-10-02 17:10:41 +0200
committerMarcel Hernandez <[email protected]>2018-10-02 17:10:41 +0200
commit2b08826ff3dd916c214ad8212824f5934e0ca3c2 (patch)
treeb077ce0cb7eedf418e279bcf9e69e59d7d5a8eb2
parentf9baa69aa34de7d71953dc75cc0310ee8958d2e0 (diff)
decoupled Diactoros via PSR-17 factories
-rw-r--r--composer.json4
-rw-r--r--src/PSR7Client.php95
2 files changed, 65 insertions, 34 deletions
diff --git a/composer.json b/composer.json
index 7be3ecfe..b01cf694 100644
--- a/composer.json
+++ b/composer.json
@@ -11,9 +11,9 @@
],
"require": {
"php": "^7.0",
- "spiral/goridge": "^2.0",
+ "http-interop/http-factory-diactoros": "^1.0",
"psr/http-message": "^1.0",
- "zendframework/zend-diactoros": "^1.7"
+ "spiral/goridge": "^2.0"
},
"autoload": {
"psr-4": {
diff --git a/src/PSR7Client.php b/src/PSR7Client.php
index e8d93fe8..111ab6ce 100644
--- a/src/PSR7Client.php
+++ b/src/PSR7Client.php
@@ -7,9 +7,12 @@
namespace Spiral\RoadRunner;
+use Http\Factory\Diactoros;
use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
-use Zend\Diactoros;
+use Psr\Http\Message\StreamFactoryInterface;
+use Psr\Http\Message\UploadedFileFactoryInterface;
/**
* Manages PSR-7 request and response.
@@ -17,16 +20,41 @@ use Zend\Diactoros;
class PSR7Client
{
/**
- * @varWorker
+ * @var Worker
*/
private $worker;
/**
- * @param Worker $worker
+ * @var ServerRequestFactoryInterface
*/
- public function __construct(Worker $worker)
- {
+ private $requestFactory;
+
+ /**
+ * @var StreamFactoryInterface
+ */
+ private $streamFactory;
+
+ /**
+ * @var UploadedFileFactoryInterface
+ */
+ private $uploadsFactory;
+
+ /**
+ * @param Worker $worker
+ * @param ServerRequestFactoryInterface|null $requestFactory
+ * @param StreamFactoryInterface|null $streamFactory
+ * @param UploadedFileFactoryInterface|null $uploadsFactory
+ */
+ public function __construct(
+ Worker $worker,
+ ServerRequestFactoryInterface $requestFactory = null,
+ StreamFactoryInterface $streamFactory = null,
+ UploadedFileFactoryInterface $uploadsFactory = null
+ ) {
$this->worker = $worker;
+ $this->requestFactory = $requestFactory ?? new Diactoros\ServerRequestFactory();
+ $this->streamFactory = $streamFactory ?? new Diactoros\StreamFactory();
+ $this->uploadsFactory = $uploadsFactory ?? new Diactoros\UploadedFileFactory();
}
/**
@@ -53,36 +81,39 @@ class PSR7Client
return null;
}
+ $_SERVER = $this->configureServer($ctx);
+
+ $request = $this->requestFactory->createServerRequest(
+ $ctx['method'],
+ $ctx['uri'],
+ $_SERVER
+ );
+
parse_str($ctx['rawQuery'], $query);
- $bodyStream = 'php://input';
- $parsedBody = null;
- if ($ctx['parsed']) {
- $parsedBody = json_decode($body, true);
- } elseif ($body != null) {
- $bodyStream = new Diactoros\Stream("php://memory", "rwb");
- $bodyStream->write($body);
+ $request = $request
+ ->withCookieParams($ctx['cookies'])
+ ->withProtocolVersion($ctx['protocol'])
+ ->withQueryParams($query)
+ ->withUploadedFiles($this->wrapUploads($ctx['uploads']));
+
+ foreach ($ctx['attributes'] as $name => $value) {
+ $request = $request->withAttribute($name, $value);
}
- $_SERVER = $this->configureServer($ctx);
+ foreach ($ctx['headers'] as $name => $value) {
+ $request = $request->withHeader($name, $value);
+ }
- $request = new Diactoros\ServerRequest(
- $_SERVER,
- $this->wrapUploads($ctx['uploads']),
- $ctx['uri'],
- $ctx['method'],
- $bodyStream,
- $ctx['headers'],
- $ctx['cookies'],
- $query,
- $parsedBody,
- $ctx['protocol']
- );
+ if ($body !== null) {
+ $bodyStream = $this->streamFactory->createStream($body);
+ $bodyStream->write($body);
- if (!empty($ctx['attributes'])) {
- foreach ($ctx['attributes'] as $key => $value) {
- $request = $request->withAttribute($key, $value);
- }
+ $request = $request->withBody($bodyStream);
+ }
+
+ if ($ctx['parsed']) {
+ $request = $request->withParsedBody(json_decode($body, true));
}
return $request;
@@ -144,8 +175,8 @@ class PSR7Client
continue;
}
- $result[$index] = new Diactoros\UploadedFile(
- $f['tmpName'],
+ $result[$index] = $this->uploadsFactory->createUploadedFile(
+ $this->streamFactory->createStreamFromFile($f['tmpName']),
$f['size'],
$f['error'],
$f['name'],
@@ -155,4 +186,4 @@ class PSR7Client
return $result;
}
-} \ No newline at end of file
+}