|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Emitter.php |
| 4 | + * |
| 5 | + * This file is part of InitPHP. |
| 6 | + * |
| 7 | + * @author Muhammet ŞAFAK <[email protected]> |
| 8 | + * @copyright Copyright © 2022 InitPHP |
| 9 | + * @license http://initphp.github.com/license.txt MIT |
| 10 | + * @version 1.0 |
| 11 | + * @link https://www.muhammetsafak.com.tr |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace InitPHP\HTTP; |
| 17 | + |
| 18 | +use \Psr\Http\Message\{ResponseInterface, StreamInterface}; |
| 19 | + |
| 20 | +use function assert; |
| 21 | +use function is_string; |
| 22 | +use function ucfirst; |
| 23 | +use function header; |
| 24 | +use function sprintf; |
| 25 | +use function headers_sent; |
| 26 | +use function is_int; |
| 27 | +use function ob_get_level; |
| 28 | +use function ob_get_length; |
| 29 | +use function flush; |
| 30 | +use function strlen; |
| 31 | +use function preg_match; |
| 32 | + |
| 33 | +class Emitter |
| 34 | +{ |
| 35 | + |
| 36 | + protected ?int $bufferLength = null; |
| 37 | + |
| 38 | + public function emit(ResponseInterface $response, ?int $bufferLength = null) |
| 39 | + { |
| 40 | + if($bufferLength !== null && $bufferLength > 0){ |
| 41 | + $this->bufferLength = $bufferLength; |
| 42 | + } |
| 43 | + $this->assertNoPreviousOutput(); |
| 44 | + $this->emitHeaders($response); |
| 45 | + $this->emitStatusLine($response); |
| 46 | + $this->emitBody($response); |
| 47 | + } |
| 48 | + |
| 49 | + private function emitBody(ResponseInterface $response): void |
| 50 | + { |
| 51 | + if($this->bufferLength === null){ |
| 52 | + echo $response->getBody(); |
| 53 | + return; |
| 54 | + } |
| 55 | + flush(); |
| 56 | + $stream = $response->getBody(); |
| 57 | + $range = $this->parseHeaderContentRange($response->getHeaderLine('content-range')); |
| 58 | + if(isset($range['unit']) && $range['unit'] === 'bytes'){ |
| 59 | + $this->emitBodyRange($stream, $range['first'], $range['last']); |
| 60 | + return; |
| 61 | + } |
| 62 | + if($stream->isSeekable()){ |
| 63 | + $stream->rewind(); |
| 64 | + } |
| 65 | + while(!$stream->eof()){ |
| 66 | + echo $stream->read($this->bufferLength); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private function emitBodyRange(StreamInterface $body, int $first, int $last): void |
| 71 | + { |
| 72 | + $length = $last - ($first + 1); |
| 73 | + if($body->isSeekable()){ |
| 74 | + $body->seek($first); |
| 75 | + } |
| 76 | + while($length >= $this->bufferLength && !$body->eof()){ |
| 77 | + $content = $body->read($this->bufferLength); |
| 78 | + $length -= strlen($content); |
| 79 | + echo $content; |
| 80 | + } |
| 81 | + if($length > 0 && !$body->eof()){ |
| 82 | + echo $body->read($length); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private function assertNoPreviousOutput(): void |
| 87 | + { |
| 88 | + $filename = null; |
| 89 | + $line = null; |
| 90 | + if(headers_sent($filename, $line)){ |
| 91 | + assert(is_string($filename) && is_int($line)); |
| 92 | + throw new \RuntimeException(\sprintf('Unable to emit response; headers already sent in %s:%d', $filename, $line)); |
| 93 | + } |
| 94 | + if(ob_get_level() > 0 && ob_get_length() > 0){ |
| 95 | + throw new \RuntimeException('Output has been emitted previously; cannot emit response'); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private function emitStatusLine(ResponseInterface $response): void |
| 100 | + { |
| 101 | + $reasonPhrase = $response->getReasonPhrase(); |
| 102 | + $statusCode = $response->getStatusCode(); |
| 103 | + header(sprintf( |
| 104 | + 'HTTP/%s %d%s', |
| 105 | + $response->getProtocolVersion(), |
| 106 | + $statusCode, ($reasonPhrase ? ' ' . $reasonPhrase : '') |
| 107 | + ), true, $statusCode); |
| 108 | + } |
| 109 | + |
| 110 | + private function emitHeaders(ResponseInterface $response): void |
| 111 | + { |
| 112 | + $statusCode = $response->getStatusCode(); |
| 113 | + |
| 114 | + foreach ($response->getHeaders() as $header => $values){ |
| 115 | + assert(is_string($header)); |
| 116 | + $name = ucfirst($header); |
| 117 | + $first = ($name !== 'Set-Cookie'); |
| 118 | + foreach ($values as $value){ |
| 119 | + header(sprintf('%s: %s', $name, $value), $first, $statusCode); |
| 120 | + $first = false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private function parseHeaderContentRange(string $header): ?array |
| 126 | + { |
| 127 | + if (preg_match('/(?P<unit>[\w]+)\s+(?P<first>\d+)-(?P<last>\d+)\/(?P<length>\d+|\*)/', $header, $matches)) { |
| 128 | + return [ |
| 129 | + 'unit' => $matches['unit'], |
| 130 | + 'first' => (int)$matches['first'], |
| 131 | + 'last' => (int)$matches['last'], |
| 132 | + 'length' => ($matches['length'] === '*') ? '*' : (int)$matches['length'], |
| 133 | + ]; |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments