@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | public function testRewindAfterRead(): void |
| 50 | 50 | { |
| 51 | - if (PHP_VERSION_ID < 80000) { |
|
| 51 | + if (PHP_VERSION_ID < 80000){ |
|
| 52 | 52 | $this->markTestSkipped('See issue https://bugs.php.net/bug.php?id=79927.'); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -103,9 +103,9 @@ discard block |
||
| 103 | 103 | $rValue = 'return-value'; |
| 104 | 104 | $stream = $this->createStream(self::DEFAULT_SEQUENCE, $rValue); |
| 105 | 105 | |
| 106 | - $result = (string) $stream; |
|
| 106 | + $result = (string)$stream; |
|
| 107 | 107 | |
| 108 | - $this->assertSame(self::DEFAULT_CONTENT_RESULT . $rValue, $result); |
|
| 108 | + $this->assertSame(self::DEFAULT_CONTENT_RESULT.$rValue, $result); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | public function testToStringWithReturnOnly(): void |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $rValue = 'return-value'; |
| 114 | 114 | $stream = $this->createStream([], $rValue); |
| 115 | 115 | |
| 116 | - $result = (string) $stream; |
|
| 116 | + $result = (string)$stream; |
|
| 117 | 117 | |
| 118 | 118 | $this->assertSame($rValue, $result); |
| 119 | 119 | } |
@@ -48,7 +48,8 @@ |
||
| 48 | 48 | |
| 49 | 49 | public function testRewindAfterRead(): void |
| 50 | 50 | { |
| 51 | - if (PHP_VERSION_ID < 80000) { |
|
| 51 | + if (PHP_VERSION_ID < 80000) |
|
| 52 | + { |
|
| 52 | 53 | $this->markTestSkipped('See issue https://bugs.php.net/bug.php?id=79927.'); |
| 53 | 54 | } |
| 54 | 55 | |
@@ -27,23 +27,23 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | public function __toString(): string |
| 29 | 29 | { |
| 30 | - try { |
|
| 30 | + try{ |
|
| 31 | 31 | return $this->getContents(); |
| 32 | - } catch (\Exception $e) { |
|
| 32 | + }catch (\Exception $e){ |
|
| 33 | 33 | return ''; |
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | public function close(): void |
| 38 | 38 | { |
| 39 | - if ($this->stream !== null) { |
|
| 39 | + if ($this->stream !== null){ |
|
| 40 | 40 | $this->detach(); |
| 41 | 41 | } |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function detach() |
| 45 | 45 | { |
| 46 | - if ($this->stream === null) { |
|
| 46 | + if ($this->stream === null){ |
|
| 47 | 47 | return null; |
| 48 | 48 | } |
| 49 | 49 | $this->stream = null; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | public function rewind(): void |
| 83 | 83 | { |
| 84 | - if ($this->stream !== null) { |
|
| 84 | + if ($this->stream !== null){ |
|
| 85 | 85 | $this->stream->rewind(); |
| 86 | 86 | } |
| 87 | 87 | $this->caret = 0; |
@@ -105,26 +105,26 @@ discard block |
||
| 105 | 105 | |
| 106 | 106 | public function read($length): string |
| 107 | 107 | { |
| 108 | - if (!$this->readable) { |
|
| 108 | + if (!$this->readable){ |
|
| 109 | 109 | throw new RuntimeException('Cannot read from non-readable stream.'); |
| 110 | 110 | } |
| 111 | - if ($this->stream === null) { |
|
| 111 | + if ($this->stream === null){ |
|
| 112 | 112 | throw new RuntimeException('Cannot read from detached stream.'); |
| 113 | 113 | } |
| 114 | - do { |
|
| 115 | - if ($this->started) { |
|
| 114 | + do{ |
|
| 115 | + if ($this->started){ |
|
| 116 | 116 | $read = (string)$this->stream->send(null); |
| 117 | - } else { |
|
| 117 | + }else{ |
|
| 118 | 118 | $this->started = true; |
| 119 | 119 | $read = (string)$this->stream->current(); |
| 120 | 120 | } |
| 121 | - if (!$this->stream->valid()) { |
|
| 121 | + if (!$this->stream->valid()){ |
|
| 122 | 122 | $read .= $this->stream->getReturn(); |
| 123 | 123 | break; |
| 124 | 124 | } |
| 125 | - } while ($read === ''); |
|
| 125 | + }while ($read === ''); |
|
| 126 | 126 | $this->caret += \strlen($read); |
| 127 | - if (!$this->stream->valid()) { |
|
| 127 | + if (!$this->stream->valid()){ |
|
| 128 | 128 | $this->size = $this->caret; |
| 129 | 129 | } |
| 130 | 130 | return $read; |
@@ -132,19 +132,19 @@ discard block |
||
| 132 | 132 | |
| 133 | 133 | public function getContents(): string |
| 134 | 134 | { |
| 135 | - if ($this->stream === null) { |
|
| 135 | + if ($this->stream === null){ |
|
| 136 | 136 | throw new RuntimeException('Unable to read stream contents.'); |
| 137 | 137 | } |
| 138 | 138 | $content = ''; |
| 139 | - do { |
|
| 139 | + do{ |
|
| 140 | 140 | $content .= $this->read(PHP_INT_MAX); |
| 141 | - } while ($this->stream->valid()); |
|
| 141 | + }while ($this->stream->valid()); |
|
| 142 | 142 | return $content; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | public function getMetadata($key = null) |
| 146 | 146 | { |
| 147 | - if ($this->stream === null) { |
|
| 147 | + if ($this->stream === null){ |
|
| 148 | 148 | return $key ? null : []; |
| 149 | 149 | } |
| 150 | 150 | |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | 'eof' => $this->eof(), |
| 154 | 154 | ]; |
| 155 | 155 | |
| 156 | - if (null === $key) { |
|
| 156 | + if (null === $key){ |
|
| 157 | 157 | return $meta; |
| 158 | 158 | } |
| 159 | 159 | |
@@ -27,23 +27,28 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | public function __toString(): string |
| 29 | 29 | { |
| 30 | - try { |
|
| 30 | + try |
|
| 31 | + { |
|
| 31 | 32 | return $this->getContents(); |
| 32 | - } catch (\Exception $e) { |
|
| 33 | + } |
|
| 34 | + catch (\Exception $e) |
|
| 35 | + { |
|
| 33 | 36 | return ''; |
| 34 | 37 | } |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | public function close(): void |
| 38 | 41 | { |
| 39 | - if ($this->stream !== null) { |
|
| 42 | + if ($this->stream !== null) |
|
| 43 | + { |
|
| 40 | 44 | $this->detach(); |
| 41 | 45 | } |
| 42 | 46 | } |
| 43 | 47 | |
| 44 | 48 | public function detach() |
| 45 | 49 | { |
| 46 | - if ($this->stream === null) { |
|
| 50 | + if ($this->stream === null) |
|
| 51 | + { |
|
| 47 | 52 | return null; |
| 48 | 53 | } |
| 49 | 54 | $this->stream = null; |
@@ -81,7 +86,8 @@ discard block |
||
| 81 | 86 | |
| 82 | 87 | public function rewind(): void |
| 83 | 88 | { |
| 84 | - if ($this->stream !== null) { |
|
| 89 | + if ($this->stream !== null) |
|
| 90 | + { |
|
| 85 | 91 | $this->stream->rewind(); |
| 86 | 92 | } |
| 87 | 93 | $this->caret = 0; |
@@ -105,26 +111,34 @@ discard block |
||
| 105 | 111 | |
| 106 | 112 | public function read($length): string |
| 107 | 113 | { |
| 108 | - if (!$this->readable) { |
|
| 114 | + if (!$this->readable) |
|
| 115 | + { |
|
| 109 | 116 | throw new RuntimeException('Cannot read from non-readable stream.'); |
| 110 | 117 | } |
| 111 | - if ($this->stream === null) { |
|
| 118 | + if ($this->stream === null) |
|
| 119 | + { |
|
| 112 | 120 | throw new RuntimeException('Cannot read from detached stream.'); |
| 113 | 121 | } |
| 114 | - do { |
|
| 115 | - if ($this->started) { |
|
| 122 | + do |
|
| 123 | + { |
|
| 124 | + if ($this->started) |
|
| 125 | + { |
|
| 116 | 126 | $read = (string)$this->stream->send(null); |
| 117 | - } else { |
|
| 127 | + } |
|
| 128 | + else |
|
| 129 | + { |
|
| 118 | 130 | $this->started = true; |
| 119 | 131 | $read = (string)$this->stream->current(); |
| 120 | 132 | } |
| 121 | - if (!$this->stream->valid()) { |
|
| 133 | + if (!$this->stream->valid()) |
|
| 134 | + { |
|
| 122 | 135 | $read .= $this->stream->getReturn(); |
| 123 | 136 | break; |
| 124 | 137 | } |
| 125 | 138 | } while ($read === ''); |
| 126 | 139 | $this->caret += \strlen($read); |
| 127 | - if (!$this->stream->valid()) { |
|
| 140 | + if (!$this->stream->valid()) |
|
| 141 | + { |
|
| 128 | 142 | $this->size = $this->caret; |
| 129 | 143 | } |
| 130 | 144 | return $read; |
@@ -132,11 +146,13 @@ discard block |
||
| 132 | 146 | |
| 133 | 147 | public function getContents(): string |
| 134 | 148 | { |
| 135 | - if ($this->stream === null) { |
|
| 149 | + if ($this->stream === null) |
|
| 150 | + { |
|
| 136 | 151 | throw new RuntimeException('Unable to read stream contents.'); |
| 137 | 152 | } |
| 138 | 153 | $content = ''; |
| 139 | - do { |
|
| 154 | + do |
|
| 155 | + { |
|
| 140 | 156 | $content .= $this->read(PHP_INT_MAX); |
| 141 | 157 | } while ($this->stream->valid()); |
| 142 | 158 | return $content; |
@@ -144,7 +160,8 @@ discard block |
||
| 144 | 160 | |
| 145 | 161 | public function getMetadata($key = null) |
| 146 | 162 | { |
| 147 | - if ($this->stream === null) { |
|
| 163 | + if ($this->stream === null) |
|
| 164 | + { |
|
| 148 | 165 | return $key ? null : []; |
| 149 | 166 | } |
| 150 | 167 | |
@@ -153,7 +170,8 @@ discard block |
||
| 153 | 170 | 'eof' => $this->eof(), |
| 154 | 171 | ]; |
| 155 | 172 | |
| 156 | - if (null === $key) { |
|
| 173 | + if (null === $key) |
|
| 174 | + { |
|
| 157 | 175 | return $meta; |
| 158 | 176 | } |
| 159 | 177 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | CoreInterface $core, |
| 57 | 57 | ScopeInterface $scope, |
| 58 | 58 | ResponseFactoryInterface $responseFactory |
| 59 | - ) { |
|
| 59 | + ){ |
|
| 60 | 60 | $this->core = $core; |
| 61 | 61 | $this->scope = $scope; |
| 62 | 62 | $this->responseFactory = $responseFactory; |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | public function handle(Request $request): Response |
| 96 | 96 | { |
| 97 | - if ($this->controller === null) { |
|
| 97 | + if ($this->controller === null){ |
|
| 98 | 98 | throw new HandlerException('Controller and action pair is not set'); |
| 99 | 99 | } |
| 100 | 100 | |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $output = $result = null; |
| 105 | 105 | |
| 106 | 106 | $response = $this->responseFactory->createResponse(200); |
| 107 | - try { |
|
| 107 | + try{ |
|
| 108 | 108 | // run the core withing PSR-7 Request/Response scope |
| 109 | 109 | $result = $this->scope->runScope( |
| 110 | 110 | [ |
@@ -119,29 +119,29 @@ discard block |
||
| 119 | 119 | ); |
| 120 | 120 | } |
| 121 | 121 | ); |
| 122 | - } catch (ControllerException $e) { |
|
| 122 | + }catch (ControllerException $e){ |
|
| 123 | 123 | ob_get_clean(); |
| 124 | 124 | throw $this->mapException($e); |
| 125 | - } catch (\Throwable $e) { |
|
| 125 | + }catch (\Throwable $e){ |
|
| 126 | 126 | ob_get_clean(); |
| 127 | 127 | throw $e; |
| 128 | - } finally { |
|
| 129 | - while (ob_get_level() > $outputLevel + 1) { |
|
| 130 | - $output = ob_get_clean() . $output; |
|
| 128 | + }finally{ |
|
| 129 | + while (ob_get_level() > $outputLevel + 1){ |
|
| 130 | + $output = ob_get_clean().$output; |
|
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | return $this->wrapResponse( |
| 135 | 135 | $response, |
| 136 | 136 | $result, |
| 137 | - ob_get_clean() . $output |
|
| 137 | + ob_get_clean().$output |
|
| 138 | 138 | ); |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | private function getAction(Request $request): string |
| 142 | 142 | { |
| 143 | - if ($this->verbActions) { |
|
| 144 | - return strtolower($request->getMethod()) . ucfirst($this->action); |
|
| 143 | + if ($this->verbActions){ |
|
| 144 | + return strtolower($request->getMethod()).ucfirst($this->action); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | return $this->action; |
@@ -156,21 +156,21 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
| 158 | 158 | { |
| 159 | - if ($result instanceof Response) { |
|
| 160 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
| 159 | + if ($result instanceof Response){ |
|
| 160 | + if ($output !== '' && $result->getBody()->isWritable()){ |
|
| 161 | 161 | $result->getBody()->write($output); |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | return $result; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - if ($result instanceof \Generator) { |
|
| 167 | + if ($result instanceof \Generator){ |
|
| 168 | 168 | return $response->withBody(new GeneratorStream($result)); |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - if (\is_array($result) || $result instanceof \JsonSerializable) { |
|
| 171 | + if (\is_array($result) || $result instanceof \JsonSerializable){ |
|
| 172 | 172 | $response = $this->writeJson($response, $result); |
| 173 | - } else { |
|
| 173 | + }else{ |
|
| 174 | 174 | $response->getBody()->write((string)$result); |
| 175 | 175 | } |
| 176 | 176 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | */ |
| 186 | 186 | private function mapException(ControllerException $exception): ClientException |
| 187 | 187 | { |
| 188 | - switch ($exception->getCode()) { |
|
| 188 | + switch ($exception->getCode()){ |
|
| 189 | 189 | case ControllerException::BAD_ACTION: |
| 190 | 190 | case ControllerException::NOT_FOUND: |
| 191 | 191 | return new NotFoundException($exception->getMessage()); |
@@ -94,7 +94,8 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | public function handle(Request $request): Response |
| 96 | 96 | { |
| 97 | - if ($this->controller === null) { |
|
| 97 | + if ($this->controller === null) |
|
| 98 | + { |
|
| 98 | 99 | throw new HandlerException('Controller and action pair is not set'); |
| 99 | 100 | } |
| 100 | 101 | |
@@ -104,14 +105,16 @@ discard block |
||
| 104 | 105 | $output = $result = null; |
| 105 | 106 | |
| 106 | 107 | $response = $this->responseFactory->createResponse(200); |
| 107 | - try { |
|
| 108 | + try |
|
| 109 | + { |
|
| 108 | 110 | // run the core withing PSR-7 Request/Response scope |
| 109 | 111 | $result = $this->scope->runScope( |
| 110 | 112 | [ |
| 111 | 113 | Request::class => $request, |
| 112 | 114 | Response::class => $response, |
| 113 | 115 | ], |
| 114 | - function () use ($request) { |
|
| 116 | + function () use ($request) |
|
| 117 | + { |
|
| 115 | 118 | return $this->core->callAction( |
| 116 | 119 | $this->controller, |
| 117 | 120 | $this->getAction($request), |
@@ -119,14 +122,21 @@ discard block |
||
| 119 | 122 | ); |
| 120 | 123 | } |
| 121 | 124 | ); |
| 122 | - } catch (ControllerException $e) { |
|
| 125 | + } |
|
| 126 | + catch (ControllerException $e) |
|
| 127 | + { |
|
| 123 | 128 | ob_get_clean(); |
| 124 | 129 | throw $this->mapException($e); |
| 125 | - } catch (\Throwable $e) { |
|
| 130 | + } |
|
| 131 | + catch (\Throwable $e) |
|
| 132 | + { |
|
| 126 | 133 | ob_get_clean(); |
| 127 | 134 | throw $e; |
| 128 | - } finally { |
|
| 129 | - while (ob_get_level() > $outputLevel + 1) { |
|
| 135 | + } |
|
| 136 | + finally |
|
| 137 | + { |
|
| 138 | + while (ob_get_level() > $outputLevel + 1) |
|
| 139 | + { |
|
| 130 | 140 | $output = ob_get_clean() . $output; |
| 131 | 141 | } |
| 132 | 142 | } |
@@ -140,7 +150,8 @@ discard block |
||
| 140 | 150 | |
| 141 | 151 | private function getAction(Request $request): string |
| 142 | 152 | { |
| 143 | - if ($this->verbActions) { |
|
| 153 | + if ($this->verbActions) |
|
| 154 | + { |
|
| 144 | 155 | return strtolower($request->getMethod()) . ucfirst($this->action); |
| 145 | 156 | } |
| 146 | 157 | |
@@ -156,21 +167,27 @@ discard block |
||
| 156 | 167 | */ |
| 157 | 168 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
| 158 | 169 | { |
| 159 | - if ($result instanceof Response) { |
|
| 160 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
| 170 | + if ($result instanceof Response) |
|
| 171 | + { |
|
| 172 | + if ($output !== '' && $result->getBody()->isWritable()) |
|
| 173 | + { |
|
| 161 | 174 | $result->getBody()->write($output); |
| 162 | 175 | } |
| 163 | 176 | |
| 164 | 177 | return $result; |
| 165 | 178 | } |
| 166 | 179 | |
| 167 | - if ($result instanceof \Generator) { |
|
| 180 | + if ($result instanceof \Generator) |
|
| 181 | + { |
|
| 168 | 182 | return $response->withBody(new GeneratorStream($result)); |
| 169 | 183 | } |
| 170 | 184 | |
| 171 | - if (\is_array($result) || $result instanceof \JsonSerializable) { |
|
| 185 | + if (\is_array($result) || $result instanceof \JsonSerializable) |
|
| 186 | + { |
|
| 172 | 187 | $response = $this->writeJson($response, $result); |
| 173 | - } else { |
|
| 188 | + } |
|
| 189 | + else |
|
| 190 | + { |
|
| 174 | 191 | $response->getBody()->write((string)$result); |
| 175 | 192 | } |
| 176 | 193 | |
@@ -185,7 +202,8 @@ discard block |
||
| 185 | 202 | */ |
| 186 | 203 | private function mapException(ControllerException $exception): ClientException |
| 187 | 204 | { |
| 188 | - switch ($exception->getCode()) { |
|
| 205 | + switch ($exception->getCode()) |
|
| 206 | + { |
|
| 189 | 207 | case ControllerException::BAD_ACTION: |
| 190 | 208 | case ControllerException::NOT_FOUND: |
| 191 | 209 | return new NotFoundException($exception->getMessage()); |