1515use Spiral \RoadRunner \Message \Command \StreamStop ;
1616use Spiral \RoadRunner \Payload ;
1717use Spiral \RoadRunner \WorkerInterface ;
18- use Stringable ;
1918
2019/**
2120 * @psalm-import-type HeadersList from Request
4039 */
4140class HttpWorker implements HttpWorkerInterface
4241{
43- /**
44- * @var WorkerInterface
45- */
46- private WorkerInterface $ worker ;
47-
48- /**
49- * @param WorkerInterface $worker
50- */
51- public function __construct (WorkerInterface $ worker )
52- {
53- $ this ->worker = $ worker ;
42+ public function __construct (
43+ private readonly WorkerInterface $ worker ,
44+ ) {
5445 }
5546
56- /**
57- * @return WorkerInterface
58- */
5947 public function getWorker (): WorkerInterface
6048 {
6149 return $ this ->worker ;
6250 }
6351
6452 /**
65- * {@inheritDoc}
6653 * @throws \JsonException
6754 */
6855 public function waitRequest (): ?Request
@@ -81,11 +68,15 @@ public function waitRequest(): ?Request
8168 }
8269
8370 /**
84- * {@inheritDoc}
8571 * @throws \JsonException
8672 */
87- public function respond (int $ status , string $ body , array $ headers = []): void
73+ public function respond (int $ status , string | Generator $ body , array $ headers = []): void
8874 {
75+ if ($ body instanceof Generator) {
76+ $ this ->respondStream ($ status , $ body , $ headers );
77+ return ;
78+ }
79+
8980 $ head = (string )\json_encode ([
9081 'status ' => $ status ,
9182 'headers ' => $ headers ?: (object )[],
@@ -94,14 +85,7 @@ public function respond(int $status, string $body, array $headers = []): void
9485 $ this ->worker ->respond (new Payload ($ body , $ head ));
9586 }
9687
97- /**
98- * Respond data using Streamed Output
99- *
100- * @param Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null> $body Body generator.
101- * Each yielded value will be sent as a separated stream chunk.
102- * Returned value will be sent as a last stream package.
103- */
104- public function respondStream (int $ status , Generator $ body , array $ headers = []): void
88+ private function respondStream (int $ status , Generator $ body , array $ headers = []): void
10589 {
10690 $ head = (string )\json_encode ([
10791 'status ' => $ status ,
@@ -126,50 +110,33 @@ public function respondStream(int $status, Generator $body, array $headers = [])
126110 }
127111
128112 /**
129- * @param string $body
130113 * @param RequestContext $context
131- * @return Request
132- *
133- * @psalm-suppress InaccessibleProperty
134114 */
135115 private function createRequest (string $ body , array $ context ): Request
136116 {
137- $ request = new Request ();
138- $ request ->body = $ body ;
139-
140- $ this ->hydrateRequest ($ request , $ context );
141-
142- return $ request ;
143- }
144-
145- /**
146- * @param Request $request
147- * @param RequestContext $context
148- *
149- * @psalm-suppress InaccessibleProperty
150- * @psalm-suppress MixedPropertyTypeCoercion
151- */
152- private function hydrateRequest (Request $ request , array $ context ): void
153- {
154- $ request ->remoteAddr = $ context ['remoteAddr ' ];
155- $ request ->protocol = $ context ['protocol ' ];
156- $ request ->method = $ context ['method ' ];
157- $ request ->uri = $ context ['uri ' ];
158- \parse_str ($ context ['rawQuery ' ], $ request ->query );
159-
160- $ request ->attributes = (array )($ context ['attributes ' ] ?? []);
161- $ request ->headers = $ this ->filterHeaders ((array )($ context ['headers ' ] ?? []));
162- $ request ->cookies = (array )($ context ['cookies ' ] ?? []);
163- $ request ->uploads = (array )($ context ['uploads ' ] ?? []);
164- $ request ->parsed = (bool )$ context ['parsed ' ];
165-
166- $ request ->attributes [Request::PARSED_BODY_ATTRIBUTE_NAME ] = $ request ->parsed ;
117+ \parse_str ($ context ['rawQuery ' ], $ query );
118+ return new Request (
119+ remoteAddr: $ context ['remoteAddr ' ],
120+ protocol: $ context ['protocol ' ],
121+ method: $ context ['method ' ],
122+ uri: $ context ['uri ' ],
123+ headers: $ this ->filterHeaders ((array )($ context ['headers ' ] ?? [])),
124+ cookies: (array )($ context ['cookies ' ] ?? []),
125+ uploads: (array )($ context ['uploads ' ] ?? []),
126+ attributes: [
127+ Request::PARSED_BODY_ATTRIBUTE_NAME => (bool )$ context ['parsed ' ],
128+ ] + (array )($ context ['attributes ' ] ?? []),
129+ query: $ query ,
130+ body: $ body ,
131+ parsed: (bool )$ context ['parsed ' ],
132+ );
167133 }
168134
169135 /**
170136 * Remove all non-string and empty-string keys
171137 *
172- * @return array<string, mixed>
138+ * @param array<array-key, array<array-key, string>> $headers
139+ * @return HeadersList
173140 */
174141 private function filterHeaders (array $ headers ): array
175142 {
@@ -180,7 +147,8 @@ private function filterHeaders(array $headers): array
180147 unset($ headers [$ key ]);
181148 }
182149 }
183- /** @var array<string, mixed> $headers */
150+
151+ /** @var HeadersList $headers */
184152 return $ headers ;
185153 }
186154}
0 commit comments