Skip to content

Commit

Permalink
Improves serve command (#50821)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Mar 29, 2024
1 parent 61f3908 commit bcc3044
Showing 1 changed file with 74 additions and 43 deletions.
117 changes: 74 additions & 43 deletions src/Illuminate/Foundation/Console/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class ServeCommand extends Command
*/
protected $portOffset = 0;

/**
* The list of lines that are pending to be output.
*
* @var string
*/
protected $outputBuffer = '';

/**
* The list of requests being handled and their start time.
*
Expand Down Expand Up @@ -245,68 +252,92 @@ protected function canTryAnotherPort()
*/
protected function handleProcessOutput()
{
return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) {
if (str($line)->contains('Development Server (http')) {
if ($this->serverRunningHasBeenDisplayed) {
return;
}
return function ($type, $buffer) {
$this->outputBuffer .= $buffer;

$this->components->info("Server running on [http://{$this->host()}:{$this->port()}].");
$this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');
$this->flushOutputBuffer();
};
}

$this->newLine();
/**
* Flush the output buffer.
*
* @return void
*/
protected function flushOutputBuffer()
{
$lines = str($this->outputBuffer)->explode("\n");

$this->outputBuffer = (string) $lines->pop();

$this->serverRunningHasBeenDisplayed = true;
} elseif (str($line)->contains(' Accepted')) {
$requestPort = $this->getRequestPortFromLine($line);
$lines
->map(fn ($line) => trim($line))
->filter()
->each(function ($line) {
if (str($line)->contains('Development Server (http')) {
if ($this->serverRunningHasBeenDisplayed === false) {
$this->serverRunningHasBeenDisplayed = true;

$this->requestsPool[$requestPort] = [
$this->getDateFromLine($line),
false,
];
} elseif (str($line)->contains([' [200]: GET '])) {
$requestPort = $this->getRequestPortFromLine($line);
$this->components->info("Server running on [http://{$this->host()}:{$this->port()}].");
$this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');

$this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]);
} elseif (str($line)->contains(' Closing')) {
$requestPort = $this->getRequestPortFromLine($line);
$this->newLine();
}

if (empty($this->requestsPool[$requestPort])) {
return;
}

[$startDate, $file] = $this->requestsPool[$requestPort];
if (str($line)->contains(' Accepted')) {
$requestPort = $this->getRequestPortFromLine($line);

$formattedStartedAt = $startDate->format('Y-m-d H:i:s');
$this->requestsPool[$requestPort] = [
$this->getDateFromLine($line),
false,
];
} elseif (str($line)->contains([' [200]: GET '])) {
$requestPort = $this->getRequestPortFromLine($line);

unset($this->requestsPool[$requestPort]);
$this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]);
} elseif (str($line)->contains(' Closing')) {
$requestPort = $this->getRequestPortFromLine($line);

[$date, $time] = explode(' ', $formattedStartedAt);
if (empty($this->requestsPool[$requestPort])) {
$this->requestsPool[$requestPort] = [
$this->getDateFromLine($line),
false,
];
}

$this->output->write(" <fg=gray>$date</> $time");
[$startDate, $file] = $this->requestsPool[$requestPort];

$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);
$formattedStartedAt = $startDate->format('Y-m-d H:i:s');

if ($file) {
$this->output->write($file = " $file");
}
unset($this->requestsPool[$requestPort]);

$dots = max(terminal()->width() - mb_strlen($formattedStartedAt) - mb_strlen($file) - mb_strlen($runTime) - 9, 0);
[$date, $time] = explode(' ', $formattedStartedAt);

$this->output->write(' '.str_repeat('<fg=gray>.</>', $dots));
$this->output->writeln(" <fg=gray>~ {$runTime}s</>");
} elseif (str($line)->contains(['Closed without sending a request'])) {
// ...
} elseif (! empty($line)) {
$position = strpos($line, '] ');
$this->output->write(" <fg=gray>$date</> $time");

if ($position !== false) {
$line = substr($line, $position + 1);
}
$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);

$this->components->warn($line);
}
});
if ($file) {
$this->output->write($file = " $file");
}

$dots = max(terminal()->width() - mb_strlen($formattedStartedAt) - mb_strlen($file) - mb_strlen($runTime) - 9, 0);

$this->output->write(' '.str_repeat('<fg=gray>.</>', $dots));
$this->output->writeln(" <fg=gray>~ {$runTime}s</>");
} elseif (str($line)->contains(['Closed without sending a request', 'Failed to poll event'])) {
// ...
} elseif (! empty($line)) {
if (str($line)->startsWith('[')) {
$line = str($line)->after('] ');
}

$this->output->writeln(" <fg=gray>$line</>");
}
});
}

/**
Expand Down

0 comments on commit bcc3044

Please sign in to comment.