Skip to content

Commit e7ac9b1

Browse files
[9.x] Improves output capture from serve command (#43461)
* Improves output capture from `serve` command * Update ServeCommand.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7c338af commit e7ac9b1

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/Illuminate/Foundation/Console/ServeCommand.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,39 +227,37 @@ protected function canTryAnotherPort()
227227
protected function handleProcessOutput()
228228
{
229229
return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) {
230-
$parts = explode(']', $line);
231-
232230
if (str($line)->contains('Development Server (http')) {
233231
$this->components->info("Server running on [http://{$this->host()}:{$this->port()}].");
234232
$this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');
235233

236234
$this->newLine();
237235
} elseif (str($line)->contains(' Accepted')) {
238-
$startDate = Carbon::createFromFormat('D M d H:i:s Y', ltrim($parts[0], '['));
239-
240-
preg_match('/\:(\d+)/', $parts[1], $matches);
236+
$requestPort = $this->getRequestPortFromLine($line);
241237

242-
$this->requestsPool[$matches[1]] = [$startDate, false];
238+
$this->requestsPool[$requestPort] = [
239+
$this->getDateFromLine($line),
240+
false,
241+
];
243242
} elseif (str($line)->contains([' [200]: GET '])) {
244-
preg_match('/\:(\d+)/', $parts[1], $matches);
243+
$requestPort = $this->getRequestPortFromLine($line);
245244

246-
$this->requestsPool[$matches[1]][1] = trim(explode('[200]: GET', $line)[1]);
245+
$this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]);
247246
} elseif (str($line)->contains(' Closing')) {
248-
preg_match('/\:(\d+)/', $parts[1], $matches);
249-
250-
$request = $this->requestsPool[$matches[1]];
247+
$requestPort = $this->getRequestPortFromLine($line);
248+
$request = $this->requestsPool[$requestPort];
251249

252250
[$startDate, $file] = $request;
251+
253252
$formattedStartedAt = $startDate->format('Y-m-d H:i:s');
254253

255-
unset($this->requestsPool[$matches[1]]);
254+
unset($this->requestsPool[$requestPort]);
256255

257256
[$date, $time] = explode(' ', $formattedStartedAt);
258257

259258
$this->output->write(" <fg=gray>$date</> $time");
260259

261-
$runTime = Carbon::createFromFormat('D M d H:i:s Y', ltrim($parts[0], '['))
262-
->diffInSeconds($startDate);
260+
$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);
263261

264262
if ($file) {
265263
$this->output->write($file = " $file");
@@ -269,16 +267,41 @@ protected function handleProcessOutput()
269267

270268
$this->output->write(' '.str_repeat('<fg=gray>.</>', $dots));
271269
$this->output->writeln(" <fg=gray>~ {$runTime}s</>");
272-
} elseif (str($line)->contains(['Closed without sending a request', ']: '])) {
270+
} elseif (str($line)->contains(['Closed without sending a request'])) {
273271
// ...
274-
} elseif (isset($parts[1])) {
275-
$this->components->warn($parts[1]);
276272
} elseif (! empty($line)) {
277-
$this->components->warn($line);
273+
$warning = explode('] ', $line);
274+
$this->components->warn(count($warning) > 1 ? $warning[1] : $warning[0]);
278275
}
279276
});
280277
}
281278

279+
/**
280+
* Get the date from the given PHP server output.
281+
*
282+
* @param string $line
283+
* @return \Illuminate\Support\Carbon
284+
*/
285+
protected function getDateFromLine($line)
286+
{
287+
preg_match('/^\[([^\]]+)\]/', $line, $matches);
288+
289+
return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
290+
}
291+
292+
/**
293+
* Get the request port from the given PHP server output.
294+
*
295+
* @param string $line
296+
* @return int
297+
*/
298+
protected function getRequestPortFromLine($line)
299+
{
300+
preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
301+
302+
return (int) $matches[1];
303+
}
304+
282305
/**
283306
* Get the console command options.
284307
*

0 commit comments

Comments
 (0)