diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 3c0ce56..6b9f40d 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -40,4 +40,4 @@ jobs: run: ./vendor/bin/phpunit tests --testdox - name: Run static analysis - run: ./vendor/bin/phpstan analyse src --level 6 --no-progress --no-interaction \ No newline at end of file + run: ./vendor/bin/phpstan analyse src --no-progress --no-interaction diff --git a/composer.json b/composer.json index 476fb3b..c96a64f 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "autoload": { "psr-4": { - "Borsch\\": "src/" + "Borsch\\RequestHandler\\": "src/" } }, "autoload-dev": { @@ -28,4 +28,4 @@ "BorschTest\\": "tests/" } } -} \ No newline at end of file +} diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..ad2c08d --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,3 @@ +parameters: + level: 6 + treatPhpDocTypesAsCertain: false diff --git a/src/Emitter.php b/src/Emitter.php new file mode 100644 index 0000000..f60c969 --- /dev/null +++ b/src/Emitter.php @@ -0,0 +1,62 @@ +emitHeaders($response); + $this->emitStatusLine($response); + $this->emitBody($response->getBody()); + } + + private function emitHeaders(ResponseInterface $response): void + { + foreach ($response->getHeaders() as $name => $values) { + foreach ($values as $value) { + header(sprintf( + '%s: %s', + ucwords($name, '-'), $value), + false + ); + } + } + } + + private function emitStatusLine(ResponseInterface $response): void + { + $http_line = sprintf( + 'HTTP/%s %s %s', + $response->getProtocolVersion(), + $response->getStatusCode(), + $response->getReasonPhrase() + ); + + header($http_line, true, $response->getStatusCode()); + } + + private function emitBody(StreamInterface $stream): void + { + if ($stream->isSeekable()) { + $stream->rewind(); + } + + if (!$stream->isReadable()) { + echo $stream; + return; + } + + $length = 1024 * 8; + while (!$stream->eof()) { + echo $stream->read($length); + } + } +} diff --git a/src/EmitterInterface.php b/src/EmitterInterface.php new file mode 100644 index 0000000..12ee33c --- /dev/null +++ b/src/EmitterInterface.php @@ -0,0 +1,16 @@ +stack->shift()->process($request, $this); + $middleware = $this->stack->shift(); + if (!$middleware instanceof MiddlewareInterface) { + throw RequestHandlerRuntimeException::invalidMiddleware( + (string)(is_object($middleware) ? get_class($middleware) : gettype($middleware)) + ); + } + + return $middleware->process($request, $this); } } diff --git a/src/RequestHandler/Emitter.php b/src/RequestHandler/Emitter.php deleted file mode 100644 index f55032f..0000000 --- a/src/RequestHandler/Emitter.php +++ /dev/null @@ -1,40 +0,0 @@ -getProtocolVersion(), - $response->getStatusCode(), - $response->getReasonPhrase() - ); - - header($http_line, true, $response->getStatusCode()); - - foreach ($response->getHeaders() as $name => $values) { - foreach ($values as $value) { - header(sprintf('%s: %s', $name, $value), false); - } - } - - $stream = $response->getBody(); - - if ($stream->isSeekable()) { - $stream->rewind(); - } - - $length = 1024 * 8; - while (!$stream->eof()) { - echo $stream->read($length); - } - } -} diff --git a/src/RequestHandler/Exception/RequestHandlerRuntimeException.php b/src/RequestHandler/Exception/RequestHandlerRuntimeException.php deleted file mode 100644 index 7caa458..0000000 --- a/src/RequestHandler/Exception/RequestHandlerRuntimeException.php +++ /dev/null @@ -1,18 +0,0 @@ -server_request_factory = $server_request_factory; + $this->error_response_factory = $error_response_factory; + } + + /** + * @inheritDoc + */ + public function run(): void + { + try { + $request = ($this->server_request_factory)(); + } catch (Throwable $e) { + $this->emitter->emit( + ($this->error_response_factory)($e) + ); + + return; + } + + $this->emitter->emit( + $this->handler->handle($request) + ); + } +} diff --git a/src/RequestHandlerRunnerInterface.php b/src/RequestHandlerRunnerInterface.php new file mode 100644 index 0000000..bcb5911 --- /dev/null +++ b/src/RequestHandlerRunnerInterface.php @@ -0,0 +1,13 @@ +