@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | // On demand to save some memory. |
| 57 | 57 | |
| 58 | 58 | // Disable buffering |
| 59 | - while (\ob_get_level() > 0) { |
|
| 59 | + while (\ob_get_level() > 0){ |
|
| 60 | 60 | \ob_end_flush(); |
| 61 | 61 | } |
| 62 | 62 | /** |
@@ -66,12 +66,12 @@ discard block |
||
| 66 | 66 | $http = $this->container->get(Http::class); |
| 67 | 67 | $emitter = $emitter ?? $this->container->get(EmitterInterface::class); |
| 68 | 68 | |
| 69 | - try { |
|
| 69 | + try{ |
|
| 70 | 70 | $response = $http->handle($this->initRequest()); |
| 71 | 71 | $emitter->emit($response); |
| 72 | - } catch (\Throwable $e) { |
|
| 72 | + }catch (\Throwable $e){ |
|
| 73 | 73 | $this->handleException($emitter, $e); |
| 74 | - } finally { |
|
| 74 | + }finally{ |
|
| 75 | 75 | $this->finalizer->finalize(false); |
| 76 | 76 | } |
| 77 | 77 | } |
@@ -92,16 +92,16 @@ discard block |
||
| 92 | 92 | { |
| 93 | 93 | $handler = new HtmlHandler(); |
| 94 | 94 | |
| 95 | - try { |
|
| 95 | + try{ |
|
| 96 | 96 | /** @var SnapshotInterface $snapshot */ |
| 97 | 97 | $this->container->get(SnapshotterInterface::class)->register($e); |
| 98 | 98 | |
| 99 | 99 | // on demand |
| 100 | 100 | $state = $this->container->get(StateInterface::class); |
| 101 | - if ($state !== null) { |
|
| 101 | + if ($state !== null){ |
|
| 102 | 102 | $handler = $handler->withState($state); |
| 103 | 103 | } |
| 104 | - } catch (\Throwable | ContainerExceptionInterface $se) { |
|
| 104 | + }catch (\Throwable | ContainerExceptionInterface $se){ |
|
| 105 | 105 | // nothing to report |
| 106 | 106 | } |
| 107 | 107 | |
@@ -56,7 +56,8 @@ discard block |
||
| 56 | 56 | // On demand to save some memory. |
| 57 | 57 | |
| 58 | 58 | // Disable buffering |
| 59 | - while (\ob_get_level() > 0) { |
|
| 59 | + while (\ob_get_level() > 0) |
|
| 60 | + { |
|
| 60 | 61 | \ob_end_flush(); |
| 61 | 62 | } |
| 62 | 63 | /** |
@@ -66,12 +67,17 @@ discard block |
||
| 66 | 67 | $http = $this->container->get(Http::class); |
| 67 | 68 | $emitter = $emitter ?? $this->container->get(EmitterInterface::class); |
| 68 | 69 | |
| 69 | - try { |
|
| 70 | + try |
|
| 71 | + { |
|
| 70 | 72 | $response = $http->handle($this->initRequest()); |
| 71 | 73 | $emitter->emit($response); |
| 72 | - } catch (\Throwable $e) { |
|
| 74 | + } |
|
| 75 | + catch (\Throwable $e) |
|
| 76 | + { |
|
| 73 | 77 | $this->handleException($emitter, $e); |
| 74 | - } finally { |
|
| 78 | + } |
|
| 79 | + finally |
|
| 80 | + { |
|
| 75 | 81 | $this->finalizer->finalize(false); |
| 76 | 82 | } |
| 77 | 83 | } |
@@ -92,16 +98,20 @@ discard block |
||
| 92 | 98 | { |
| 93 | 99 | $handler = new HtmlHandler(); |
| 94 | 100 | |
| 95 | - try { |
|
| 101 | + try |
|
| 102 | + { |
|
| 96 | 103 | /** @var SnapshotInterface $snapshot */ |
| 97 | 104 | $this->container->get(SnapshotterInterface::class)->register($e); |
| 98 | 105 | |
| 99 | 106 | // on demand |
| 100 | 107 | $state = $this->container->get(StateInterface::class); |
| 101 | - if ($state !== null) { |
|
| 108 | + if ($state !== null) |
|
| 109 | + { |
|
| 102 | 110 | $handler = $handler->withState($state); |
| 103 | 111 | } |
| 104 | - } catch (\Throwable | ContainerExceptionInterface $se) { |
|
| 112 | + } |
|
| 113 | + catch (\Throwable | ContainerExceptionInterface $se) |
|
| 114 | + { |
|
| 105 | 115 | // nothing to report |
| 106 | 116 | } |
| 107 | 117 | |
@@ -54,13 +54,13 @@ discard block |
||
| 54 | 54 | private function emitBody(ResponseInterface $response): void |
| 55 | 55 | { |
| 56 | 56 | $body = $response->getBody(); |
| 57 | - if ($body->isSeekable()) { |
|
| 57 | + if ($body->isSeekable()){ |
|
| 58 | 58 | $body->rewind(); |
| 59 | 59 | } |
| 60 | - if (!$body->isReadable()) { |
|
| 60 | + if (!$body->isReadable()){ |
|
| 61 | 61 | return; |
| 62 | 62 | } |
| 63 | - while (!$body->eof()) { |
|
| 63 | + while (!$body->eof()){ |
|
| 64 | 64 | echo $body->read($this->bufferSize); |
| 65 | 65 | flush(); |
| 66 | 66 | } |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | private function assertNoPreviousOutput(): void |
| 79 | 79 | { |
| 80 | - if (headers_sent()) { |
|
| 80 | + if (headers_sent()){ |
|
| 81 | 81 | throw new EmitterException('Unable to emit response, headers already send.'); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if (ob_get_level() > 0 && ob_get_length() > 0) { |
|
| 84 | + if (ob_get_level() > 0 && ob_get_length() > 0){ |
|
| 85 | 85 | throw new EmitterException('Unable to emit response, found non closed buffered output.'); |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | 'HTTP/%s %d%s', |
| 108 | 108 | $response->getProtocolVersion(), |
| 109 | 109 | $statusCode, |
| 110 | - ($reasonPhrase ? ' ' . $reasonPhrase : '') |
|
| 110 | + ($reasonPhrase ? ' '.$reasonPhrase : '') |
|
| 111 | 111 | ), true, $statusCode); |
| 112 | 112 | } |
| 113 | 113 | |
@@ -125,10 +125,10 @@ discard block |
||
| 125 | 125 | { |
| 126 | 126 | $statusCode = $response->getStatusCode(); |
| 127 | 127 | |
| 128 | - foreach ($response->getHeaders() as $header => $values) { |
|
| 128 | + foreach ($response->getHeaders() as $header => $values){ |
|
| 129 | 129 | $name = $this->filterHeader($header); |
| 130 | 130 | $first = $name === 'Set-Cookie' ? false : true; |
| 131 | - foreach ($values as $value) { |
|
| 131 | + foreach ($values as $value){ |
|
| 132 | 132 | header(sprintf( |
| 133 | 133 | '%s: %s', |
| 134 | 134 | $name, |
@@ -54,13 +54,16 @@ discard block |
||
| 54 | 54 | private function emitBody(ResponseInterface $response): void |
| 55 | 55 | { |
| 56 | 56 | $body = $response->getBody(); |
| 57 | - if ($body->isSeekable()) { |
|
| 57 | + if ($body->isSeekable()) |
|
| 58 | + { |
|
| 58 | 59 | $body->rewind(); |
| 59 | 60 | } |
| 60 | - if (!$body->isReadable()) { |
|
| 61 | + if (!$body->isReadable()) |
|
| 62 | + { |
|
| 61 | 63 | return; |
| 62 | 64 | } |
| 63 | - while (!$body->eof()) { |
|
| 65 | + while (!$body->eof()) |
|
| 66 | + { |
|
| 64 | 67 | echo $body->read($this->bufferSize); |
| 65 | 68 | flush(); |
| 66 | 69 | } |
@@ -77,11 +80,13 @@ discard block |
||
| 77 | 80 | */ |
| 78 | 81 | private function assertNoPreviousOutput(): void |
| 79 | 82 | { |
| 80 | - if (headers_sent()) { |
|
| 83 | + if (headers_sent()) |
|
| 84 | + { |
|
| 81 | 85 | throw new EmitterException('Unable to emit response, headers already send.'); |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | - if (ob_get_level() > 0 && ob_get_length() > 0) { |
|
| 88 | + if (ob_get_level() > 0 && ob_get_length() > 0) |
|
| 89 | + { |
|
| 85 | 90 | throw new EmitterException('Unable to emit response, found non closed buffered output.'); |
| 86 | 91 | } |
| 87 | 92 | } |
@@ -125,10 +130,12 @@ discard block |
||
| 125 | 130 | { |
| 126 | 131 | $statusCode = $response->getStatusCode(); |
| 127 | 132 | |
| 128 | - foreach ($response->getHeaders() as $header => $values) { |
|
| 133 | + foreach ($response->getHeaders() as $header => $values) |
|
| 134 | + { |
|
| 129 | 135 | $name = $this->filterHeader($header); |
| 130 | 136 | $first = $name === 'Set-Cookie' ? false : true; |
| 131 | - foreach ($values as $value) { |
|
| 137 | + foreach ($values as $value) |
|
| 138 | + { |
|
| 132 | 139 | header(sprintf( |
| 133 | 140 | '%s: %s', |
| 134 | 141 | $name, |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | |
| 7 | 7 | use Spiral\Tests\Http\SapiEmitter\Support\HTTPFunctions; |
| 8 | 8 | |
| 9 | -if (!function_exists(__NAMESPACE__ . '\\headers_sent')) { |
|
| 9 | +if (!function_exists(__NAMESPACE__.'\\headers_sent')){ |
|
| 10 | 10 | /** |
| 11 | 11 | * Mock for the headers_sent() function for Emitter class. |
| 12 | 12 | */ |
@@ -6,7 +6,8 @@ |
||
| 6 | 6 | |
| 7 | 7 | use Spiral\Tests\Http\SapiEmitter\Support\HTTPFunctions; |
| 8 | 8 | |
| 9 | -if (!function_exists(__NAMESPACE__ . '\\headers_sent')) { |
|
| 9 | +if (!function_exists(__NAMESPACE__ . '\\headers_sent')) |
|
| 10 | +{ |
|
| 10 | 11 | /** |
| 11 | 12 | * Mock for the headers_sent() function for Emitter class. |
| 12 | 13 | */ |
@@ -63,14 +63,14 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public static function header(string $string, bool $replace = true, ?int $http_response_code = null): void |
| 65 | 65 | { |
| 66 | - if (strpos($string, 'HTTP/') !== 0) { |
|
| 66 | + if (strpos($string, 'HTTP/') !== 0){ |
|
| 67 | 67 | $header = strtolower(explode(':', $string, 2)[0]); |
| 68 | - if ($replace || !array_key_exists($header, self::$headers)) { |
|
| 68 | + if ($replace || !array_key_exists($header, self::$headers)){ |
|
| 69 | 69 | self::$headers[$header] = []; |
| 70 | 70 | } |
| 71 | 71 | self::$headers[$header][] = $string; |
| 72 | 72 | } |
| 73 | - if ($http_response_code !== null) { |
|
| 73 | + if ($http_response_code !== null){ |
|
| 74 | 74 | self::$responseCode = $http_response_code; |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | public static function header_remove(?string $header = null): void |
| 82 | 82 | { |
| 83 | - if ($header === null) { |
|
| 83 | + if ($header === null){ |
|
| 84 | 84 | self::$headers = []; |
| 85 | - } else { |
|
| 85 | + }else{ |
|
| 86 | 86 | unset(self::$headers[strtolower($header)]); |
| 87 | 87 | } |
| 88 | 88 | } |
@@ -95,8 +95,8 @@ discard block |
||
| 95 | 95 | public static function headers_list(): array |
| 96 | 96 | { |
| 97 | 97 | $result = []; |
| 98 | - foreach (self::$headers as $values) { |
|
| 99 | - foreach ($values as $header) { |
|
| 98 | + foreach (self::$headers as $values){ |
|
| 99 | + foreach ($values as $header){ |
|
| 100 | 100 | $result[] = $header; |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | public static function http_response_code(?int $response_code = null): int |
| 110 | 110 | { |
| 111 | - if ($response_code !== null) { |
|
| 111 | + if ($response_code !== null){ |
|
| 112 | 112 | self::$responseCode = $response_code; |
| 113 | 113 | } |
| 114 | 114 | return self::$responseCode; |
@@ -63,14 +63,17 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public static function header(string $string, bool $replace = true, ?int $http_response_code = null): void |
| 65 | 65 | { |
| 66 | - if (strpos($string, 'HTTP/') !== 0) { |
|
| 66 | + if (strpos($string, 'HTTP/') !== 0) |
|
| 67 | + { |
|
| 67 | 68 | $header = strtolower(explode(':', $string, 2)[0]); |
| 68 | - if ($replace || !array_key_exists($header, self::$headers)) { |
|
| 69 | + if ($replace || !array_key_exists($header, self::$headers)) |
|
| 70 | + { |
|
| 69 | 71 | self::$headers[$header] = []; |
| 70 | 72 | } |
| 71 | 73 | self::$headers[$header][] = $string; |
| 72 | 74 | } |
| 73 | - if ($http_response_code !== null) { |
|
| 75 | + if ($http_response_code !== null) |
|
| 76 | + { |
|
| 74 | 77 | self::$responseCode = $http_response_code; |
| 75 | 78 | } |
| 76 | 79 | } |
@@ -80,9 +83,12 @@ discard block |
||
| 80 | 83 | */ |
| 81 | 84 | public static function header_remove(?string $header = null): void |
| 82 | 85 | { |
| 83 | - if ($header === null) { |
|
| 86 | + if ($header === null) |
|
| 87 | + { |
|
| 84 | 88 | self::$headers = []; |
| 85 | - } else { |
|
| 89 | + } |
|
| 90 | + else |
|
| 91 | + { |
|
| 86 | 92 | unset(self::$headers[strtolower($header)]); |
| 87 | 93 | } |
| 88 | 94 | } |
@@ -95,8 +101,10 @@ discard block |
||
| 95 | 101 | public static function headers_list(): array |
| 96 | 102 | { |
| 97 | 103 | $result = []; |
| 98 | - foreach (self::$headers as $values) { |
|
| 99 | - foreach ($values as $header) { |
|
| 104 | + foreach (self::$headers as $values) |
|
| 105 | + { |
|
| 106 | + foreach ($values as $header) |
|
| 107 | + { |
|
| 100 | 108 | $result[] = $header; |
| 101 | 109 | } |
| 102 | 110 | } |
@@ -108,7 +116,8 @@ discard block |
||
| 108 | 116 | */ |
| 109 | 117 | public static function http_response_code(?int $response_code = null): int |
| 110 | 118 | { |
| 111 | - if ($response_code !== null) { |
|
| 119 | + if ($response_code !== null) |
|
| 120 | + { |
|
| 112 | 121 | self::$responseCode = $response_code; |
| 113 | 122 | } |
| 114 | 123 | return self::$responseCode; |
@@ -132,12 +132,12 @@ |
||
| 132 | 132 | { |
| 133 | 133 | $body = 'Example body'; |
| 134 | 134 | $response = $this->createResponse(200, [], $body) |
| 135 | - ->withHeader('X-Test', '1') |
|
| 136 | - ->withAddedHeader('X-Test', '2') |
|
| 137 | - ->withAddedHeader('X-Test', '3; 3.5') |
|
| 138 | - ->withHeader('Cookie-Set', '1') |
|
| 139 | - ->withAddedHeader('cookie-Set', '2') |
|
| 140 | - ->withAddedHeader('Cookie-set', '3'); |
|
| 135 | + ->withHeader('X-Test', '1') |
|
| 136 | + ->withAddedHeader('X-Test', '2') |
|
| 137 | + ->withAddedHeader('X-Test', '3; 3.5') |
|
| 138 | + ->withHeader('Cookie-Set', '1') |
|
| 139 | + ->withAddedHeader('cookie-Set', '2') |
|
| 140 | + ->withAddedHeader('Cookie-set', '3'); |
|
| 141 | 141 | |
| 142 | 142 | (new SapiEmitter())->emit($response); |
| 143 | 143 | $this->assertEquals(200, $this->getResponseCode()); |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | $this->assertEquals(200, $this->getResponseCode()); |
| 66 | 66 | $this->assertCount(2, $this->getHeaders()); |
| 67 | 67 | $this->assertContains('X-Test: 1', $this->getHeaders()); |
| 68 | - $this->assertContains('Content-Length: ' . $length, $this->getHeaders()); |
|
| 68 | + $this->assertContains('Content-Length: '.$length, $this->getHeaders()); |
|
| 69 | 69 | $this->expectOutputString('Example body'); |
| 70 | 70 | } |
| 71 | 71 | |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | $this->expectException(EmitterException::class); |
| 119 | 119 | $this->expectExceptionMessage('Unable to emit response, found non closed buffered output.'); |
| 120 | 120 | |
| 121 | - try { |
|
| 121 | + try{ |
|
| 122 | 122 | echo 'some data'; |
| 123 | 123 | $this->createEmitter()->emit($response); |
| 124 | - } catch (\Throwable $e) { |
|
| 124 | + }catch (\Throwable $e){ |
|
| 125 | 125 | throw $e; |
| 126 | - } finally { |
|
| 126 | + }finally{ |
|
| 127 | 127 | \ob_end_clean(); |
| 128 | 128 | } |
| 129 | 129 | } |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | private function createEmitter(?int $bufferSize = null): SapiEmitter |
| 155 | 155 | { |
| 156 | 156 | $emitter = new SapiEmitter(); |
| 157 | - if ($bufferSize !== null) { |
|
| 157 | + if ($bufferSize !== null){ |
|
| 158 | 158 | $emitter->bufferSize = $bufferSize; |
| 159 | 159 | } |
| 160 | 160 | return $emitter; |
@@ -169,12 +169,12 @@ discard block |
||
| 169 | 169 | $response = (new Response()) |
| 170 | 170 | ->withStatus($status) |
| 171 | 171 | ->withProtocolVersion($version); |
| 172 | - foreach ($headers as $header => $value) { |
|
| 172 | + foreach ($headers as $header => $value){ |
|
| 173 | 173 | $response = $response->withHeader($header, $value); |
| 174 | 174 | } |
| 175 | - if ($body instanceof StreamInterface) { |
|
| 175 | + if ($body instanceof StreamInterface){ |
|
| 176 | 176 | $response = $response->withBody($body); |
| 177 | - } elseif (is_string($body)) { |
|
| 177 | + } elseif (is_string($body)){ |
|
| 178 | 178 | $response->getBody()->write($body); |
| 179 | 179 | } |
| 180 | 180 | return $response; |
@@ -118,12 +118,17 @@ discard block |
||
| 118 | 118 | $this->expectException(EmitterException::class); |
| 119 | 119 | $this->expectExceptionMessage('Unable to emit response, found non closed buffered output.'); |
| 120 | 120 | |
| 121 | - try { |
|
| 121 | + try |
|
| 122 | + { |
|
| 122 | 123 | echo 'some data'; |
| 123 | 124 | $this->createEmitter()->emit($response); |
| 124 | - } catch (\Throwable $e) { |
|
| 125 | + } |
|
| 126 | + catch (\Throwable $e) |
|
| 127 | + { |
|
| 125 | 128 | throw $e; |
| 126 | - } finally { |
|
| 129 | + } |
|
| 130 | + finally |
|
| 131 | + { |
|
| 127 | 132 | \ob_end_clean(); |
| 128 | 133 | } |
| 129 | 134 | } |
@@ -154,7 +159,8 @@ discard block |
||
| 154 | 159 | private function createEmitter(?int $bufferSize = null): SapiEmitter |
| 155 | 160 | { |
| 156 | 161 | $emitter = new SapiEmitter(); |
| 157 | - if ($bufferSize !== null) { |
|
| 162 | + if ($bufferSize !== null) |
|
| 163 | + { |
|
| 158 | 164 | $emitter->bufferSize = $bufferSize; |
| 159 | 165 | } |
| 160 | 166 | return $emitter; |
@@ -169,12 +175,16 @@ discard block |
||
| 169 | 175 | $response = (new Response()) |
| 170 | 176 | ->withStatus($status) |
| 171 | 177 | ->withProtocolVersion($version); |
| 172 | - foreach ($headers as $header => $value) { |
|
| 178 | + foreach ($headers as $header => $value) |
|
| 179 | + { |
|
| 173 | 180 | $response = $response->withHeader($header, $value); |
| 174 | 181 | } |
| 175 | - if ($body instanceof StreamInterface) { |
|
| 182 | + if ($body instanceof StreamInterface) |
|
| 183 | + { |
|
| 176 | 184 | $response = $response->withBody($body); |
| 177 | - } elseif (is_string($body)) { |
|
| 185 | + } |
|
| 186 | + elseif (is_string($body)) |
|
| 187 | + { |
|
| 178 | 188 | $response->getBody()->write($body); |
| 179 | 189 | } |
| 180 | 190 | return $response; |