@@ -48,11 +48,11 @@ public static function serve(...$routes): self
48
48
49
49
public function stop (): self
50
50
{
51
- $ url = $ this ->getBaseUrl ("___api_faker_clear_router " );
51
+ $ url = $ this ->getBaseUrl ("___api_faker_router " );
52
52
53
53
\file_get_contents (filename: $ url , context: \stream_context_create ([
54
54
'http ' => [
55
- 'method ' => 'PUT ' ,
55
+ 'method ' => 'DELETE ' ,
56
56
'header ' => 'Content-Type: application/json ' ,
57
57
'content ' => '{} '
58
58
],
@@ -68,23 +68,13 @@ public function down(): self
68
68
}
69
69
$ status = proc_get_status ($ this ->process );
70
70
$ pid = $ status ['pid ' ] ?? null ;
71
-
72
71
if ($ pid ) {
73
- if (stripos (PHP_OS_FAMILY , 'Windows ' ) !== false ) {
74
- exec ("taskkill /F /T /PID {$ pid }" );
75
- } else {
76
- exec ("kill -9 {$ pid }" );
77
- }
72
+ $ this ->killPid ($ pid );
78
73
}
79
74
80
- proc_terminate ($ this ->process );
81
- proc_close ($ this ->process );
82
- $ this ->process = null ;
83
-
84
75
return $ this ;
85
76
}
86
-
87
-
77
+
88
78
public function getPort (): int
89
79
{
90
80
return $ this ->port ;
@@ -122,7 +112,7 @@ public function handle(?int $port = null): self
122
112
$ pattern = '/Development Server \(http:\/\/localhost:(?<port>\d+)\) started/ ' ;
123
113
$ start = time ();
124
114
$ timeout = 5 ;
125
-
115
+
126
116
while (true ) {
127
117
$ chunk = fread ($ pipes [2 ], 1024 );
128
118
if ($ chunk !== false && strlen ($ chunk )) {
@@ -132,48 +122,48 @@ public function handle(?int $port = null): self
132
122
break ;
133
123
}
134
124
}
135
-
125
+
136
126
if ((time () - $ start ) > $ timeout ) {
137
127
proc_terminate ($ this ->process );
138
128
proc_close ($ this ->process );
139
129
throw new RuntimeException ("Server did not start within {$ timeout } seconds. " );
140
130
}
141
-
131
+
142
132
usleep (50 );
143
133
}
144
-
134
+
145
135
// kill process
146
136
pcntl_async_signals (true );
147
137
pcntl_signal (SIGINT , fn () => $ this ->down ());
148
138
pcntl_signal (SIGTERM , fn () => $ this ->down ());
149
-
139
+
150
140
static $ process_registry = [];
151
141
$ process_registry [] = $ this ->process ;
152
-
142
+
153
143
$ status = proc_get_status ($ this ->process );
154
144
$ wrapper_pid = $ status ['pid ' ] ?? null ;
155
-
145
+
156
146
register_shutdown_function (function () use ($ wrapper_pid ) {
157
147
if ($ wrapper_pid ) {
158
148
if (stripos (PHP_OS_FAMILY , 'Windows ' ) !== false ) {
159
149
exec ("taskkill /F /T /PID {$ wrapper_pid } >nul 2>&1 " );
160
150
} else {
161
151
exec ("ps -p {$ wrapper_pid }" , $ output , $ code );
162
-
152
+
163
153
if ($ code === 0 ) {
164
154
exec ("pgrep -P {$ wrapper_pid }" , $ child_pids );
165
155
foreach ($ child_pids as $ pid ) {
166
156
if ($ pid ) {
167
157
exec ("kill -9 {$ pid } > /dev/null 2>&1 " );
168
158
}
169
159
}
170
-
160
+
171
161
exec ("kill -9 {$ wrapper_pid } > /dev/null 2>&1 " );
172
162
}
173
163
}
174
164
}
175
165
});
176
-
166
+
177
167
return $ this ;
178
168
}
179
169
@@ -190,10 +180,10 @@ public function addRoute(
190
180
): self {
191
181
$ body = is_array ($ body ) ? json_encode ($ body ) : $ body ;
192
182
193
- $ this ->addRouteParams (new Route (
183
+ $ this ->registerRoute (new Route (
194
184
method: strtoupper ($ method ),
195
185
uri: $ uri ,
196
- body: $ body ,
186
+ body: $ body ,
197
187
status: $ status ,
198
188
times: $ times ,
199
189
headers: $ headers ,
@@ -213,7 +203,7 @@ public function addFileRoute(
213
203
int $ times = 1 ,
214
204
array $ headers = []
215
205
): self {
216
- $ this ->addRouteParams (new RouteFile (
206
+ $ this ->registerRoute (new RouteFile (
217
207
method: strtoupper ($ method ),
218
208
uri: $ uri ,
219
209
file: $ file ,
@@ -234,15 +224,14 @@ public function getRoutes(): array
234
224
235
225
public function assertRoutes (): void
236
226
{
237
- $ url = $ this ->getBaseUrl ("___api_faker_router_index " );
227
+ $ url = $ this ->getBaseUrl ("___api_faker_router " );
238
228
$ routes = [];
239
229
foreach ($ this ->routes as $ route ) {
240
230
$ uri = $ route ->uri ;
241
231
$ method = $ route ->method ;
242
232
$ path = "{$ url }?route= {$ uri }&method= {$ method }" ;
243
233
$ response = \file_get_contents ($ path );
244
234
$ routes [$ route ->uri ] = $ response ;
245
-
246
235
$ currentTimes = \json_decode ($ response , true );
247
236
$ expectedTimes = $ route ->times ;
248
237
if ($ currentTimes === $ expectedTimes ) {
@@ -264,27 +253,24 @@ public function expect(
264
253
return $ this ->addRoute ($ method , $ uri , $ status , $ body , $ times , $ headers );
265
254
}
266
255
267
- protected function addRouteParams (Route |RouteFile $ route ): array
256
+ protected function registerRoute (Route |RouteFile $ route ): array
268
257
{
269
258
if (!$ this ->port || !$ this ->process ) {
270
259
$ this ->handle ();
271
260
}
272
- $ url = $ this ->getBaseUrl ("___api_faker_add_router " );
273
-
261
+ $ url = $ this ->getBaseUrl ("___api_faker_router " );
274
262
$ this ->routes [] = $ route ;
275
263
$ params = $ route ->toArray ();
276
264
if ($ route instanceof RouteFile) {
277
265
$ params ['file ' ] = base64_encode ($ params ['file ' ]);
278
266
}
279
-
280
267
$ response = \file_get_contents (filename: $ url , context: \stream_context_create ([
281
268
'http ' => [
282
- 'method ' => 'PUT ' ,
269
+ 'method ' => 'POST ' ,
283
270
'header ' => 'Content-Type: application/json ' ,
284
271
'content ' => json_encode ($ params ),
285
272
],
286
273
]));
287
-
288
274
\preg_match ('/^HTTP\/.* (\d{3})/ ' , $ http_response_header [0 ] ?? '' , $ matches );
289
275
$ status = $ matches [1 ] ?? '' ;
290
276
@@ -298,4 +284,13 @@ public function __toString()
298
284
{
299
285
return $ this ->getBaseUrl ();
300
286
}
287
+
288
+ private function killPid (int $ pid )
289
+ {
290
+ if (stripos (PHP_OS_FAMILY , 'Windows ' ) !== false ) {
291
+ exec ("taskkill /F /T /PID {$ pid }" );
292
+ } else {
293
+ exec ("kill -9 {$ pid }" );
294
+ }
295
+ }
301
296
}
0 commit comments