@@ -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,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 |
@@ -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()); |