diff options
author | Wolfy-J <[email protected]> | 2018-06-03 22:16:09 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-03 22:16:09 +0300 |
commit | e2996bcb8ef7409db7cfd54cb378dd260002e00f (patch) | |
tree | 4e540ab10683a6dd56d96e97e9fed460c2788f52 /php-src | |
parent | 1856b93ac024988322dec1760286de75158ae02d (diff) |
error handling
Diffstat (limited to 'php-src')
-rw-r--r-- | php-src/PSR7Worker.php | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/php-src/PSR7Worker.php b/php-src/PSR7Worker.php new file mode 100644 index 00000000..65225d85 --- /dev/null +++ b/php-src/PSR7Worker.php @@ -0,0 +1,117 @@ +<?php +/** + * High-performance PHP process supervisor and load balancer written in Go + * + * @author Wolfy-J + */ + + +/** + * Class PSR7Worker serves PSR-7 requests and consume responses. + */ +class PSR7Worker +{ + /** + * @var \Spiral\RoadRunner\Worker + */ + private $worker; + + /** + * @param \Spiral\RoadRunner\Worker $worker + */ + public function __construct(\Spiral\RoadRunner\Worker $worker) + { + $this->worker = $worker; + } + + /** + * @return \Spiral\RoadRunner\Worker + */ + public function getWorker(): \Spiral\RoadRunner\Worker + { + return $this->worker; + } + + /** + * @return \Psr\Http\Message\ServerRequestInterface|null + */ + public function acceptRequest() + { + $body = $this->worker->receive($ctx); + if (empty($body) && empty($ctx)) { + // termination request + return null; + } + + parse_str($ctx['rawQuery'], $query); + + $body = 'php://input'; + $parsedBody = null; + if ($ctx['parsed']) { + $parsedBody = json_decode($body, true); + } elseif ($body != null) { + $parsedBody = new \Zend\Diactoros\Stream("php://memory", "rwb"); + $parsedBody->write($body); + } + + return new \Zend\Diactoros\ServerRequest( + $_SERVER, + $this->wrapUploads($ctx['uploads']), + $ctx['uri'], + $ctx['method'], + $body, + $ctx['headers'], + $ctx['cookies'], + $query, + $parsedBody, + $ctx['protocol'] + ); + } + + /** + * Send response to the application server. + * + * @param \Psr\Http\Message\ResponseInterface $response + */ + public function respond(\Psr\Http\Message\ResponseInterface $response) + { + $this->worker->error("asd"); + + $this->worker->send($response->getBody(), json_encode([ + 'status' => $response->getStatusCode(), + 'headers' => $response->getHeaders() + ])); + } + + /** + * Wraps all uploaded files with UploadedFile. + * + * @param array $files + * + * @return array + */ + private function wrapUploads($files): array + { + if (empty($files)) { + return []; + } + + $result = []; + foreach ($files as $index => $file) { + if (isset($file['name'])) { + $result[$index] = new \Zend\Diactoros\UploadedFile( + $file['tmpName'], + $file['size'], + $file['error'], + $file['name'], + $file['type'] + ); + continue; + } + + $result[$index] = $this->wrapUploads($file); + } + + return $result; + } +}
\ No newline at end of file |