|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Runtime\Psr17; |
| 4 | + |
| 5 | +use Psr\Http\Message\ResponseInterface; |
| 6 | +use Psr\Http\Message\ServerRequestInterface; |
| 7 | +use Psr\Http\Server\RequestHandlerInterface; |
| 8 | +use Symfony\Component\Runtime\RunnerInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Tobias Nyholm <[email protected]> |
| 12 | + */ |
| 13 | +class Emitter implements RunnerInterface |
| 14 | +{ |
| 15 | + private $requestHandler; |
| 16 | + private $response; |
| 17 | + private $request; |
| 18 | + |
| 19 | + private function __construct() |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public static function createForResponse(ResponseInterface $response): self |
| 24 | + { |
| 25 | + $self = new self(); |
| 26 | + $self->response = $response; |
| 27 | + |
| 28 | + return $self; |
| 29 | + } |
| 30 | + |
| 31 | + public static function createForRequestHandler(RequestHandlerInterface $handler, ServerRequestInterface $request): self |
| 32 | + { |
| 33 | + $self = new self(); |
| 34 | + $self->requestHandler = $handler; |
| 35 | + $self->request = $request; |
| 36 | + |
| 37 | + return $self; |
| 38 | + } |
| 39 | + |
| 40 | + public function run(): int |
| 41 | + { |
| 42 | + if (null === $this->response) { |
| 43 | + $this->response = $this->requestHandler->handle($this->request); |
| 44 | + } |
| 45 | + |
| 46 | + $this->emit($this->response); |
| 47 | + |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Emits a response for a PHP SAPI environment. |
| 53 | + * |
| 54 | + * Emits the status line and headers via the header() function, and the |
| 55 | + * body content via the output buffer. |
| 56 | + * |
| 57 | + * @Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. |
| 58 | + */ |
| 59 | + public function emit(ResponseInterface $response): void |
| 60 | + { |
| 61 | + if (headers_sent()) { |
| 62 | + throw EmitterException::forHeadersSent(); |
| 63 | + } |
| 64 | + |
| 65 | + if (ob_get_level() > 0 && ob_get_length() > 0) { |
| 66 | + throw EmitterException::forOutputSent(); |
| 67 | + } |
| 68 | + |
| 69 | + $this->emitHeaders($response); |
| 70 | + $this->emitStatusLine($response); |
| 71 | + echo $response->getBody(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Emit the status line. |
| 76 | + * |
| 77 | + * Emits the status line using the protocol version and status code from |
| 78 | + * the response; if a reason phrase is available, it, too, is emitted. |
| 79 | + * |
| 80 | + * It is important to mention that this method should be called after |
| 81 | + * `emitHeaders()` in order to prevent PHP from changing the status code of |
| 82 | + * the emitted response. |
| 83 | + * |
| 84 | + * @Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. |
| 85 | + */ |
| 86 | + private function emitStatusLine(ResponseInterface $response): void |
| 87 | + { |
| 88 | + $reasonPhrase = $response->getReasonPhrase(); |
| 89 | + $statusCode = $response->getStatusCode(); |
| 90 | + |
| 91 | + header(sprintf( |
| 92 | + 'HTTP/%s %d%s', |
| 93 | + $response->getProtocolVersion(), |
| 94 | + $statusCode, |
| 95 | + ($reasonPhrase ? ' '.$reasonPhrase : '') |
| 96 | + ), true, $statusCode); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Emit response headers. |
| 101 | + * |
| 102 | + * Loops through each header, emitting each; if the header value |
| 103 | + * is an array with multiple values, ensures that each is sent |
| 104 | + * in such a way as to create aggregate headers (instead of replace |
| 105 | + * the previous). |
| 106 | + * |
| 107 | + * @Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. |
| 108 | + */ |
| 109 | + private function emitHeaders(ResponseInterface $response): void |
| 110 | + { |
| 111 | + $statusCode = $response->getStatusCode(); |
| 112 | + |
| 113 | + foreach ($response->getHeaders() as $header => $values) { |
| 114 | + $name = ucwords($header, '-'); |
| 115 | + $first = 'Set-Cookie' === $name ? false : true; |
| 116 | + foreach ($values as $value) { |
| 117 | + header(sprintf('%s: %s', $name, $value), $first, $statusCode); |
| 118 | + $first = false; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments