summaryrefslogtreecommitdiff
path: root/src/PSR7Client.php
diff options
context:
space:
mode:
authorAlex <[email protected]>2019-01-28 22:00:53 -0800
committerAlex <[email protected]>2019-01-28 22:00:53 -0800
commit55c365f8e8e63a3f4670160d49319dcd434f0fe3 (patch)
treee6a96e6e2abb5cb76b844838098c13aa0cfcecda /src/PSR7Client.php
parent7962c0b7cc01ac51ea6195dc5488da5ee408f513 (diff)
Raw HTTP client (HttpClient), PSR7Client now depends on HttpClient
Diffstat (limited to 'src/PSR7Client.php')
-rw-r--r--src/PSR7Client.php45
1 files changed, 12 insertions, 33 deletions
diff --git a/src/PSR7Client.php b/src/PSR7Client.php
index 6b280272..00261933 100644
--- a/src/PSR7Client.php
+++ b/src/PSR7Client.php
@@ -20,8 +20,8 @@ use Psr\Http\Message\UploadedFileFactoryInterface;
*/
class PSR7Client
{
- /** @var Worker */
- private $worker;
+ /** @var HttpClient */
+ private $httpClient;
/** @var ServerRequestFactoryInterface */
private $requestFactory;
@@ -38,18 +38,19 @@ class PSR7Client
private static $allowedVersions = ['1.0', '1.1', '2',];
/**
- * @param Worker $worker
+ * @param Worker $worker
* @param ServerRequestFactoryInterface|null $requestFactory
- * @param StreamFactoryInterface|null $streamFactory
- * @param UploadedFileFactoryInterface|null $uploadsFactory
+ * @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->httpClient = new HttpClient($worker);
$this->requestFactory = $requestFactory ?? new Diactoros\ServerRequestFactory();
$this->streamFactory = $streamFactory ?? new Diactoros\StreamFactory();
$this->uploadsFactory = $uploadsFactory ?? new Diactoros\UploadedFileFactory();
@@ -57,28 +58,15 @@ class PSR7Client
}
/**
- * @return Worker
- */
- public function getWorker(): Worker
- {
- return $this->worker;
- }
-
- /**
* @return ServerRequestInterface|null
*/
public function acceptRequest()
{
- $body = $this->worker->receive($ctx);
- if (empty($body) && empty($ctx)) {
- // termination request
+ $rawRequest = $this->httpClient->acceptRequest();
+ if ($rawRequest === null)
return null;
- }
- if (empty($ctx = json_decode($ctx, true))) {
- // invalid context
- return null;
- }
+ list($ctx, $body) = $rawRequest;
$_SERVER = $this->configureServer($ctx);
@@ -122,16 +110,7 @@ class PSR7Client
*/
public function respond(ResponseInterface $response)
{
- $headers = $response->getHeaders();
- if (empty($headers)) {
- // this is required to represent empty header set as map and not as array
- $headers = new \stdClass();
- }
-
- $this->worker->send(
- $response->getBody()->__toString(),
- json_encode(['status' => $response->getStatusCode(), 'headers' => $headers])
- );
+ $this->httpClient->respond($response->getBody()->__toString(), $response->getStatusCode(), $response->getHeaders());
}
/**