@@ -137,7 +137,7 @@ public function file($uri, $data = [], $headers = [])
137137 public function request (string $ method , string $ path , array $ options = [])
138138 {
139139 return wait (function () use ($ method , $ path , $ options ) {
140- return $ this ->execute ($ this ->init ($ method , $ path , $ options ));
140+ return $ this ->execute ($ this ->initRequest ($ method , $ path , $ options ));
141141 }, $ this ->waitTimeout );
142142 }
143143
@@ -148,6 +148,50 @@ public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterf
148148 }, $ this ->waitTimeout );
149149 }
150150
151+ public function initRequest (string $ method , string $ path , array $ options = []): ServerRequestInterface
152+ {
153+ $ query = $ options ['query ' ] ?? [];
154+ $ params = $ options ['form_params ' ] ?? [];
155+ $ json = $ options ['json ' ] ?? [];
156+ $ headers = $ options ['headers ' ] ?? [];
157+ $ multipart = $ options ['multipart ' ] ?? [];
158+
159+ $ parsePath = parse_url ($ path );
160+ $ path = $ parsePath ['path ' ];
161+ $ uriPathQuery = $ parsePath ['query ' ] ?? [];
162+ if (! empty ($ uriPathQuery )) {
163+ parse_str ($ uriPathQuery , $ pathQuery );
164+ $ query = array_merge ($ pathQuery , $ query );
165+ }
166+
167+ $ data = $ params ;
168+
169+ // Initialize PSR-7 Request and Response objects.
170+ $ uri = (new Uri ($ this ->baseUri . ltrim ($ path , '/ ' )))->withQuery (http_build_query ($ query ));
171+
172+ $ content = http_build_query ($ params );
173+ if ($ method == 'POST ' && data_get ($ headers , 'Content-Type ' ) == 'application/json ' ) {
174+ $ content = json_encode ($ json , JSON_UNESCAPED_UNICODE );
175+ $ data = $ json ;
176+ }
177+
178+ $ body = new SwooleStream ($ content );
179+
180+ $ request = new Psr7Request ($ method , $ uri , $ headers , $ body );
181+
182+ return $ request ->withQueryParams ($ query )
183+ ->withParsedBody ($ data )
184+ ->withUploadedFiles ($ this ->normalizeFiles ($ multipart ));
185+ }
186+
187+ /**
188+ * @deprecated It will be removed in v3.0
189+ */
190+ protected function init (string $ method , string $ path , array $ options = []): ServerRequestInterface
191+ {
192+ return $ this ->initRequest ($ method , $ path , $ options );
193+ }
194+
151195 protected function execute (ServerRequestInterface $ psr7Request ): ResponseInterface
152196 {
153197 $ this ->persistToContext ($ psr7Request , new Psr7Response ());
@@ -191,41 +235,6 @@ protected function initBaseUri($server): void
191235 }
192236 }
193237
194- protected function init (string $ method , string $ path , array $ options = []): ServerRequestInterface
195- {
196- $ query = $ options ['query ' ] ?? [];
197- $ params = $ options ['form_params ' ] ?? [];
198- $ json = $ options ['json ' ] ?? [];
199- $ headers = $ options ['headers ' ] ?? [];
200- $ multipart = $ options ['multipart ' ] ?? [];
201-
202- $ parsePath = parse_url ($ path );
203- $ path = $ parsePath ['path ' ];
204- $ uriPathQuery = $ parsePath ['query ' ] ?? [];
205- if (! empty ($ uriPathQuery )) {
206- parse_str ($ uriPathQuery , $ pathQuery );
207- $ query = array_merge ($ pathQuery , $ query );
208- }
209-
210- $ data = $ params ;
211-
212- // Initialize PSR-7 Request and Response objects.
213- $ uri = (new Uri ($ this ->baseUri . ltrim ($ path , '/ ' )))->withQuery (http_build_query ($ query ));
214-
215- $ content = http_build_query ($ params );
216- if ($ method == 'POST ' && data_get ($ headers , 'Content-Type ' ) == 'application/json ' ) {
217- $ content = json_encode ($ json , JSON_UNESCAPED_UNICODE );
218- $ data = $ json ;
219- }
220-
221- $ body = new SwooleStream ($ content );
222-
223- $ request = new Psr7Request ($ method , $ uri , $ headers , $ body );
224- return $ request ->withQueryParams ($ query )
225- ->withParsedBody ($ data )
226- ->withUploadedFiles ($ this ->normalizeFiles ($ multipart ));
227- }
228-
229238 protected function normalizeFiles (array $ multipart ): array
230239 {
231240 $ files = [];
0 commit comments