Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Improves serve Artisan command #50821

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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