@@ -118,7 +118,8 @@ discard block |
||
| 118 | 118 | $headers = $acceptHeader->getAll(); |
| 119 | 119 | $this->assertCount(count($expected), $headers); |
| 120 | 120 | |
| 121 | - foreach ($expected as $i => $value) { |
|
| 121 | + foreach ($expected as $i => $value) |
|
| 122 | + { |
|
| 122 | 123 | $this->assertSame($value, $headers[$i]->getValue()); |
| 123 | 124 | } |
| 124 | 125 | } |
@@ -164,7 +165,8 @@ discard block |
||
| 164 | 165 | $headers = $acceptHeader->getAll(); |
| 165 | 166 | $this->assertCount(count($expected), $headers); |
| 166 | 167 | |
| 167 | - foreach ($expected as $i => $value) { |
|
| 168 | + foreach ($expected as $i => $value) |
|
| 169 | + { |
|
| 168 | 170 | $this->assertSame($value, (string)$headers[$i]); |
| 169 | 171 | } |
| 170 | 172 | } |
@@ -44,7 +44,8 @@ discard block |
||
| 44 | 44 | { |
| 45 | 45 | $core = $this->getCore(); |
| 46 | 46 | |
| 47 | - $core->setHandler(function () { |
|
| 47 | + $core->setHandler(function () |
|
| 48 | + { |
|
| 48 | 49 | return 'hello world'; |
| 49 | 50 | }); |
| 50 | 51 | |
@@ -73,7 +74,8 @@ discard block |
||
| 73 | 74 | public function testHandlerInterface(): void |
| 74 | 75 | { |
| 75 | 76 | $core = $this->getCore(); |
| 76 | - $core->setHandler(new CallableHandler(function () { |
|
| 77 | + $core->setHandler(new CallableHandler(function () |
|
| 78 | + { |
|
| 77 | 79 | return 'hello world'; |
| 78 | 80 | }, new ResponseFactory(new HttpConfig(['headers' => []])))); |
| 79 | 81 | |
@@ -85,7 +87,8 @@ discard block |
||
| 85 | 87 | { |
| 86 | 88 | $core = $this->getCore(); |
| 87 | 89 | |
| 88 | - $core->setHandler(function ($req, $resp) { |
|
| 90 | + $core->setHandler(function ($req, $resp) |
|
| 91 | + { |
|
| 89 | 92 | return $resp->withAddedHeader('hello', 'value'); |
| 90 | 93 | }); |
| 91 | 94 | |
@@ -98,7 +101,8 @@ discard block |
||
| 98 | 101 | { |
| 99 | 102 | $core = $this->getCore(); |
| 100 | 103 | |
| 101 | - $core->setHandler(function ($req, $resp) { |
|
| 104 | + $core->setHandler(function ($req, $resp) |
|
| 105 | + { |
|
| 102 | 106 | echo 'hello!'; |
| 103 | 107 | |
| 104 | 108 | return $resp->withAddedHeader('hello', 'value'); |
@@ -114,7 +118,8 @@ discard block |
||
| 114 | 118 | { |
| 115 | 119 | $core = $this->getCore(); |
| 116 | 120 | |
| 117 | - $core->setHandler(function ($req, $resp) { |
|
| 121 | + $core->setHandler(function ($req, $resp) |
|
| 122 | + { |
|
| 118 | 123 | echo 'hello!'; |
| 119 | 124 | $resp->getBody()->write('world '); |
| 120 | 125 | |
@@ -131,7 +136,8 @@ discard block |
||
| 131 | 136 | { |
| 132 | 137 | $core = $this->getCore(); |
| 133 | 138 | |
| 134 | - $core->setHandler(function () { |
|
| 139 | + $core->setHandler(function () |
|
| 140 | + { |
|
| 135 | 141 | ob_start(); |
| 136 | 142 | ob_start(); |
| 137 | 143 | echo 'hello!'; |
@@ -152,7 +158,8 @@ discard block |
||
| 152 | 158 | { |
| 153 | 159 | $core = $this->getCore(); |
| 154 | 160 | |
| 155 | - $core->setHandler(function () { |
|
| 161 | + $core->setHandler(function () |
|
| 162 | + { |
|
| 156 | 163 | return [ |
| 157 | 164 | 'status' => 404, |
| 158 | 165 | 'message' => 'not found', |
@@ -168,7 +175,8 @@ discard block |
||
| 168 | 175 | { |
| 169 | 176 | $core = $this->getCore(); |
| 170 | 177 | |
| 171 | - $core->setHandler(function () { |
|
| 178 | + $core->setHandler(function () |
|
| 179 | + { |
|
| 172 | 180 | return new Json([ |
| 173 | 181 | 'status' => 404, |
| 174 | 182 | 'message' => 'not found', |
@@ -184,7 +192,8 @@ discard block |
||
| 184 | 192 | { |
| 185 | 193 | $core = $this->getCore([HeaderMiddleware::class]); |
| 186 | 194 | |
| 187 | - $core->setHandler(function () { |
|
| 195 | + $core->setHandler(function () |
|
| 196 | + { |
|
| 188 | 197 | return 'hello?'; |
| 189 | 198 | }); |
| 190 | 199 | |
@@ -201,7 +210,8 @@ discard block |
||
| 201 | 210 | $core->getPipeline()->pushMiddleware(new Header2Middleware()); |
| 202 | 211 | $core->getPipeline()->riseMiddleware(new HeaderMiddleware()); |
| 203 | 212 | |
| 204 | - $core->setHandler(function () { |
|
| 213 | + $core->setHandler(function () |
|
| 214 | + { |
|
| 205 | 215 | return 'hello?'; |
| 206 | 216 | }); |
| 207 | 217 | |
@@ -218,7 +228,8 @@ discard block |
||
| 218 | 228 | $core->getPipeline()->pushMiddleware(new HeaderMiddleware()); |
| 219 | 229 | $core->getPipeline()->riseMiddleware(new Header2Middleware()); |
| 220 | 230 | |
| 221 | - $core->setHandler(function () { |
|
| 231 | + $core->setHandler(function () |
|
| 232 | + { |
|
| 222 | 233 | return 'hello?'; |
| 223 | 234 | }); |
| 224 | 235 | |
@@ -232,7 +243,8 @@ discard block |
||
| 232 | 243 | { |
| 233 | 244 | $core = $this->getCore(); |
| 234 | 245 | |
| 235 | - $core->setHandler(function () { |
|
| 246 | + $core->setHandler(function () |
|
| 247 | + { |
|
| 236 | 248 | $this->assertTrue($this->container->has(ServerRequestInterface::class)); |
| 237 | 249 | |
| 238 | 250 | return 'OK'; |
@@ -26,7 +26,8 @@ discard block |
||
| 26 | 26 | { |
| 27 | 27 | $pipeline = new Pipeline(new Container()); |
| 28 | 28 | |
| 29 | - $handler = new CallableHandler(function () { |
|
| 29 | + $handler = new CallableHandler(function () |
|
| 30 | + { |
|
| 30 | 31 | return 'response'; |
| 31 | 32 | }, new ResponseFactory(new HttpConfig(['headers' => []]))); |
| 32 | 33 | |
@@ -41,7 +42,8 @@ discard block |
||
| 41 | 42 | { |
| 42 | 43 | $pipeline = new Pipeline(new Container()); |
| 43 | 44 | |
| 44 | - $handler = new CallableHandler(function () { |
|
| 45 | + $handler = new CallableHandler(function () |
|
| 46 | + { |
|
| 45 | 47 | return 'response'; |
| 46 | 48 | }, new ResponseFactory(new HttpConfig(['headers' => []]))); |
| 47 | 49 | |
@@ -33,7 +33,8 @@ discard block |
||
| 33 | 33 | public function emptyItemProvider(): iterable |
| 34 | 34 | { |
| 35 | 35 | $values = ['', ' ']; |
| 36 | - foreach ($values as $value) { |
|
| 36 | + foreach ($values as $value) |
|
| 37 | + { |
|
| 37 | 38 | yield from [ |
| 38 | 39 | [AcceptHeaderItem::fromString($value)], |
| 39 | 40 | [new AcceptHeaderItem($value)], |
@@ -82,11 +83,13 @@ discard block |
||
| 82 | 83 | */ |
| 83 | 84 | public function testItemQualityBoundaries(float $quality, AcceptHeaderItem $item): void |
| 84 | 85 | { |
| 85 | - if ($quality > 1) { |
|
| 86 | + if ($quality > 1) |
|
| 87 | + { |
|
| 86 | 88 | $this->assertSame(1.0, $item->getQuality()); |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | - if ($quality < 0) { |
|
| 91 | + if ($quality < 0) |
|
| 92 | + { |
|
| 90 | 93 | $this->assertSame(0.0, $item->getQuality()); |
| 91 | 94 | } |
| 92 | 95 | |
@@ -100,7 +103,8 @@ discard block |
||
| 100 | 103 | public function qualityBoundariesProvider(): iterable |
| 101 | 104 | { |
| 102 | 105 | $qualities = [-1, 0, 0.5, 1, 2]; |
| 103 | - foreach ($qualities as $quality) { |
|
| 106 | + foreach ($qualities as $quality) |
|
| 107 | + { |
|
| 104 | 108 | yield from [ |
| 105 | 109 | [$quality, AcceptHeaderItem::fromString("*;q=$quality")], |
| 106 | 110 | [$quality, AcceptHeaderItem::fromString("*;Q=$quality")], |
@@ -140,9 +144,11 @@ discard block |
||
| 140 | 144 | ] |
| 141 | 145 | ]; |
| 142 | 146 | |
| 143 | - foreach ($set as $params) { |
|
| 147 | + foreach ($set as $params) |
|
| 148 | + { |
|
| 144 | 149 | $formattedParams = []; |
| 145 | - foreach ($params['passed'] as $k => $v) { |
|
| 150 | + foreach ($params['passed'] as $k => $v) |
|
| 151 | + { |
|
| 146 | 152 | $formattedParams[] = "$k=$v"; |
| 147 | 153 | } |
| 148 | 154 | |
@@ -26,7 +26,8 @@ |
||
| 26 | 26 | string $clientFilename = null, |
| 27 | 27 | string $clientMediaType = null |
| 28 | 28 | ): UploadedFileInterface { |
| 29 | - if ($size === null) { |
|
| 29 | + if ($size === null) |
|
| 30 | + { |
|
| 30 | 31 | $size = $stream->getSize(); |
| 31 | 32 | } |
| 32 | 33 | |
@@ -37,7 +37,8 @@ |
||
| 37 | 37 | $response = new Response('php://memory', $code, []); |
| 38 | 38 | $response = $response->withStatus($code, $reasonPhrase); |
| 39 | 39 | |
| 40 | - foreach ($this->config->getBaseHeaders() as $header => $value) { |
|
| 40 | + foreach ($this->config->getBaseHeaders() as $header => $value) |
|
| 41 | + { |
|
| 41 | 42 | $response = $response->withAddedHeader($header, $value); |
| 42 | 43 | } |
| 43 | 44 | |
@@ -51,13 +51,19 @@ discard block |
||
| 51 | 51 | $output = $result = null; |
| 52 | 52 | |
| 53 | 53 | $response = $this->responseFactory->createResponse(200); |
| 54 | - try { |
|
| 54 | + try |
|
| 55 | + { |
|
| 55 | 56 | $result = ($this->callable)($request, $response); |
| 56 | - } catch (\Throwable $e) { |
|
| 57 | + } |
|
| 58 | + catch (\Throwable $e) |
|
| 59 | + { |
|
| 57 | 60 | ob_get_clean(); |
| 58 | 61 | throw $e; |
| 59 | - } finally { |
|
| 60 | - while (ob_get_level() > $outputLevel + 1) { |
|
| 62 | + } |
|
| 63 | + finally |
|
| 64 | + { |
|
| 65 | + while (ob_get_level() > $outputLevel + 1) |
|
| 66 | + { |
|
| 61 | 67 | $output = ob_get_clean() . $output; |
| 62 | 68 | } |
| 63 | 69 | } |
@@ -80,17 +86,22 @@ discard block |
||
| 80 | 86 | */ |
| 81 | 87 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
| 82 | 88 | { |
| 83 | - if ($result instanceof Response) { |
|
| 84 | - if (!empty($output) && $result->getBody()->isWritable()) { |
|
| 89 | + if ($result instanceof Response) |
|
| 90 | + { |
|
| 91 | + if (!empty($output) && $result->getBody()->isWritable()) |
|
| 92 | + { |
|
| 85 | 93 | $result->getBody()->write($output); |
| 86 | 94 | } |
| 87 | 95 | |
| 88 | 96 | return $result; |
| 89 | 97 | } |
| 90 | 98 | |
| 91 | - if (is_array($result) || $result instanceof \JsonSerializable) { |
|
| 99 | + if (is_array($result) || $result instanceof \JsonSerializable) |
|
| 100 | + { |
|
| 92 | 101 | $response = $this->writeJson($response, $result); |
| 93 | - } else { |
|
| 102 | + } |
|
| 103 | + else |
|
| 104 | + { |
|
| 94 | 105 | $response->getBody()->write((string)$result); |
| 95 | 106 | } |
| 96 | 107 | |
@@ -53,7 +53,8 @@ discard block |
||
| 53 | 53 | $this->responseFactory = $responseFactory; |
| 54 | 54 | $this->container = $container; |
| 55 | 55 | |
| 56 | - foreach ($this->config->getMiddleware() as $middleware) { |
|
| 56 | + foreach ($this->config->getMiddleware() as $middleware) |
|
| 57 | + { |
|
| 57 | 58 | $this->pipeline->pushMiddleware($this->container->get($middleware)); |
| 58 | 59 | } |
| 59 | 60 | } |
@@ -72,11 +73,16 @@ discard block |
||
| 72 | 73 | */ |
| 73 | 74 | public function setHandler($handler): self |
| 74 | 75 | { |
| 75 | - if ($handler instanceof RequestHandlerInterface) { |
|
| 76 | + if ($handler instanceof RequestHandlerInterface) |
|
| 77 | + { |
|
| 76 | 78 | $this->handler = $handler; |
| 77 | - } elseif (is_callable($handler)) { |
|
| 79 | + } |
|
| 80 | + elseif (is_callable($handler)) |
|
| 81 | + { |
|
| 78 | 82 | $this->handler = new CallableHandler($handler, $this->responseFactory); |
| 79 | - } else { |
|
| 83 | + } |
|
| 84 | + else |
|
| 85 | + { |
|
| 80 | 86 | throw new HttpException( |
| 81 | 87 | 'Invalid handler is given, expects callable or RequestHandlerInterface.' |
| 82 | 88 | ); |
@@ -93,7 +99,8 @@ discard block |
||
| 93 | 99 | */ |
| 94 | 100 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 95 | 101 | { |
| 96 | - if (empty($this->handler)) { |
|
| 102 | + if (empty($this->handler)) |
|
| 103 | + { |
|
| 97 | 104 | throw new HttpException('Unable to run HttpCore, no handler is set.'); |
| 98 | 105 | } |
| 99 | 106 | |
@@ -124,12 +124,14 @@ discard block |
||
| 124 | 124 | $uri = $this->getUri($server, $headers); |
| 125 | 125 | |
| 126 | 126 | $request = $this->requestFactory->createServerRequest($method, $uri, $server); |
| 127 | - foreach ($headers as $name => $value) { |
|
| 127 | + foreach ($headers as $name => $value) |
|
| 128 | + { |
|
| 128 | 129 | $request = $request->withAddedHeader($name, $value); |
| 129 | 130 | } |
| 130 | 131 | |
| 131 | 132 | $protocol = '1.1'; |
| 132 | - if (!empty($_SERVER['SERVER_PROTOCOL'])) { |
|
| 133 | + if (!empty($_SERVER['SERVER_PROTOCOL'])) |
|
| 134 | + { |
|
| 133 | 135 | $protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); |
| 134 | 136 | } |
| 135 | 137 | |
@@ -140,15 +142,21 @@ discard block |
||
| 140 | 142 | ->withCookieParams($cookies) |
| 141 | 143 | ->withUploadedFiles($this->getUploadedFilesArray($files)); |
| 142 | 144 | |
| 143 | - if ($body === null) { |
|
| 145 | + if ($body === null) |
|
| 146 | + { |
|
| 144 | 147 | return $request; |
| 145 | 148 | } |
| 146 | 149 | |
| 147 | - if (\is_resource($body)) { |
|
| 150 | + if (\is_resource($body)) |
|
| 151 | + { |
|
| 148 | 152 | $body = $this->streamFactory->createStreamFromResource($body); |
| 149 | - } elseif (\is_string($body)) { |
|
| 153 | + } |
|
| 154 | + elseif (\is_string($body)) |
|
| 155 | + { |
|
| 150 | 156 | $body = $this->streamFactory->createStream($body); |
| 151 | - } elseif (!$body instanceof StreamInterface) { |
|
| 157 | + } |
|
| 158 | + elseif (!$body instanceof StreamInterface) |
|
| 159 | + { |
|
| 152 | 160 | throw new \InvalidArgumentException( |
| 153 | 161 | 'Body parameter for ServerRequestFactory::createFromParameters() ' |
| 154 | 162 | . 'must be instance of StreamInterface, resource or null.' |
@@ -166,29 +174,39 @@ discard block |
||
| 166 | 174 | private function getUri(array $server, array $headers): UriInterface |
| 167 | 175 | { |
| 168 | 176 | $uri = $this->uriFactory->createUri(); |
| 169 | - if (isset($server['HTTPS'])) { |
|
| 177 | + if (isset($server['HTTPS'])) |
|
| 178 | + { |
|
| 170 | 179 | $uri = $uri->withScheme($server['HTTPS'] === 'on' ? 'https' : 'http'); |
| 171 | 180 | } |
| 172 | 181 | |
| 173 | - if (isset($server['HTTP_HOST'])) { |
|
| 174 | - if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) { |
|
| 182 | + if (isset($server['HTTP_HOST'])) |
|
| 183 | + { |
|
| 184 | + if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) |
|
| 185 | + { |
|
| 175 | 186 | $uri = $uri->withHost($matches[1])->withPort($matches[2]); |
| 176 | - } else { |
|
| 187 | + } |
|
| 188 | + else |
|
| 189 | + { |
|
| 177 | 190 | $uri = $uri->withHost($server['HTTP_HOST']); |
| 178 | 191 | } |
| 179 | - } elseif (isset($server['SERVER_NAME'])) { |
|
| 192 | + } |
|
| 193 | + elseif (isset($server['SERVER_NAME'])) |
|
| 194 | + { |
|
| 180 | 195 | $uri = $uri->withHost($server['SERVER_NAME']); |
| 181 | 196 | } |
| 182 | 197 | |
| 183 | - if (isset($server['SERVER_PORT'])) { |
|
| 198 | + if (isset($server['SERVER_PORT'])) |
|
| 199 | + { |
|
| 184 | 200 | $uri = $uri->withPort($server['SERVER_PORT']); |
| 185 | 201 | } |
| 186 | 202 | |
| 187 | - if (isset($server['REQUEST_URI'])) { |
|
| 203 | + if (isset($server['REQUEST_URI'])) |
|
| 204 | + { |
|
| 188 | 205 | $uri = $uri->withPath(\explode('?', $server['REQUEST_URI'])[0]); |
| 189 | 206 | } |
| 190 | 207 | |
| 191 | - if (isset($server['QUERY_STRING'])) { |
|
| 208 | + if (isset($server['QUERY_STRING'])) |
|
| 209 | + { |
|
| 192 | 210 | $uri = $uri->withQuery($server['QUERY_STRING']); |
| 193 | 211 | } |
| 194 | 212 | |
@@ -200,12 +218,17 @@ discard block |
||
| 200 | 218 | */ |
| 201 | 219 | private static function getHeadersFromGlobals(): array |
| 202 | 220 | { |
| 203 | - if (\function_exists('getallheaders')) { |
|
| 221 | + if (\function_exists('getallheaders')) |
|
| 222 | + { |
|
| 204 | 223 | $headers = getallheaders(); |
| 205 | - } else { |
|
| 224 | + } |
|
| 225 | + else |
|
| 226 | + { |
|
| 206 | 227 | $headers = []; |
| 207 | - foreach ($_SERVER as $name => $value) { |
|
| 208 | - if (strncmp($name, 'HTTP_', 5) === 0) { |
|
| 228 | + foreach ($_SERVER as $name => $value) |
|
| 229 | + { |
|
| 230 | + if (strncmp($name, 'HTTP_', 5) === 0) |
|
| 231 | + { |
|
| 209 | 232 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
| 210 | 233 | $headers[$name] = $value; |
| 211 | 234 | } |
@@ -222,7 +245,8 @@ discard block |
||
| 222 | 245 | private function getUploadedFilesArray(array $filesArray): array |
| 223 | 246 | { |
| 224 | 247 | $files = []; |
| 225 | - foreach ($filesArray as $class => $info) { |
|
| 248 | + foreach ($filesArray as $class => $info) |
|
| 249 | + { |
|
| 226 | 250 | $files[$class] = []; |
| 227 | 251 | $this->populateUploadedFileRecursive( |
| 228 | 252 | $files[$class], |
@@ -250,8 +274,10 @@ discard block |
||
| 250 | 274 | */ |
| 251 | 275 | private function populateUploadedFileRecursive(&$files, $names, $tempNames, $types, $sizes, $errors): void |
| 252 | 276 | { |
| 253 | - if (\is_array($names)) { |
|
| 254 | - foreach ($names as $i => $name) { |
|
| 277 | + if (\is_array($names)) |
|
| 278 | + { |
|
| 279 | + foreach ($names as $i => $name) |
|
| 280 | + { |
|
| 255 | 281 | $files[$i] = []; |
| 256 | 282 | $this->populateUploadedFileRecursive( |
| 257 | 283 | $files[$i], |
@@ -262,10 +288,15 @@ discard block |
||
| 262 | 288 | $errors[$i] |
| 263 | 289 | ); |
| 264 | 290 | } |
| 265 | - } else { |
|
| 266 | - try { |
|
| 291 | + } |
|
| 292 | + else |
|
| 293 | + { |
|
| 294 | + try |
|
| 295 | + { |
|
| 267 | 296 | $stream = $this->streamFactory->createStreamFromFile($tempNames); |
| 268 | - } catch (\RuntimeException $e) { |
|
| 297 | + } |
|
| 298 | + catch (\RuntimeException $e) |
|
| 299 | + { |
|
| 269 | 300 | $stream = $this->streamFactory->createStream(); |
| 270 | 301 | } |
| 271 | 302 | |