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

[10.x] Set IP:PORT as key to $requestsPool on ServeCommand #46740

Closed
wants to merge 1 commit into from
Closed
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
146 changes: 101 additions & 45 deletions src/Illuminate/Foundation/Console/ServeCommand.php
Original file line number Diff line number Diff line change
@@ -227,62 +227,118 @@ protected function canTryAnotherPort()
protected function handleProcessOutput()
{
return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) {
if (empty($line)) {
return;
}

if (str($line)->contains('Development Server (http')) {
if ($this->serverRunningHasBeenDisplayed) {
return;
}
$this->handleProcessOutputStart($line);
} elseif (str($line)->contains(' Accepted')) {
$this->handleProcessOutputAccepted($line);
} elseif (str($line)->contains([' [200]: GET '])) {
$this->handleProcessOutputGet($line);
} elseif (str($line)->contains(' Closing')) {
$this->handleProcessOutputClosing($line);
} elseif (! str($line)->contains(['Closed without sending a request'])) {
$this->handleProcessOutputWarning($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</>');
/**
* Handle the Process Output: Development Server Start
*
* @param string $line
* @return void
*/
protected function handleProcessOutputStart(string $line): void
{
if ($this->serverRunningHasBeenDisplayed) {
return;
}

$this->newLine();
$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->serverRunningHasBeenDisplayed = true;
} elseif (str($line)->contains(' Accepted')) {
$requestPort = $this->getRequestPortFromLine($line);
$this->newLine();

$this->requestsPool[$requestPort] = [
$this->getDateFromLine($line),
false,
];
} elseif (str($line)->contains([' [200]: GET '])) {
$requestPort = $this->getRequestPortFromLine($line);
$this->serverRunningHasBeenDisplayed = true;
}

$this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]);
} elseif (str($line)->contains(' Closing')) {
$requestPort = $this->getRequestPortFromLine($line);
/**
* Handle the Process Output: Accepted
*
* @param string $line
* @return void
*/
protected function handleProcessOutputAccepted(string $line): void
{
$this->requestsPool[$this->getRequestPoolKey($line)] = [
$this->getDateFromLine($line),
false,
];
}

/**
* Handle the Process Output: Get
*
* @param string $line
* @return void
*/
protected function handleProcessOutputGet(string $line): void
{
$key = $this->getRequestPoolKey($line);

if (empty($this->requestsPool[$requestPort])) {
return;
}
$this->requestsPool[$key][1] = trim(explode('[200]: GET', $line)[1]);
}

[$startDate, $file] = $this->requestsPool[$requestPort];
/**
* Handle the Process Output: Closing
*
* @param string $line
* @return void
*/
protected function handleProcessOutputClosing(string $line): void
{
$key = $this->getRequestPoolKey($line);

$formattedStartedAt = $startDate->format('Y-m-d H:i:s');
if (empty($this->requestsPool[$key])) {
return;
}

unset($this->requestsPool[$requestPort]);
[$startDate, $file] = $this->requestsPool[$key];

[$date, $time] = explode(' ', $formattedStartedAt);
unset($this->requestsPool[$key]);

$this->output->write(" <fg=gray>$date</> $time");
$formattedStartedAt = $startDate->format('Y-m-d H:i:s');

$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);
[$date, $time] = explode(' ', $formattedStartedAt);

if ($file) {
$this->output->write($file = " $file");
}
$this->output->write(" <fg=gray>$date</> $time");

$dots = max(terminal()->width() - mb_strlen($formattedStartedAt) - mb_strlen($file) - mb_strlen($runTime) - 9, 0);
$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);

$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)) {
$warning = explode('] ', $line);
$this->components->warn(count($warning) > 1 ? $warning[1] : $warning[0]);
}
});
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</>");
}

/**
* Handle the Process Output: Warning
*
* @param string $line
* @return void
*/
protected function handleProcessOutputWarning(string $line): void
{
$warning = explode('] ', $line);

$this->components->warn(count($warning) > 1 ? $warning[1] : $warning[0]);
}

/**
@@ -291,7 +347,7 @@ protected function handleProcessOutput()
* @param string $line
* @return \Illuminate\Support\Carbon
*/
protected function getDateFromLine($line)
protected function getDateFromLine(string $line): Carbon
{
$regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1
? '/^\[\d+]\s\[([a-zA-Z0-9: ]+)\]/'
@@ -306,13 +362,13 @@ protected function getDateFromLine($line)
* Get the request port from the given PHP server output.
*
* @param string $line
* @return int
* @return string
*/
protected function getRequestPortFromLine($line)
protected function getRequestPoolKey(string $line): string
{
preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
preg_match('/\]\s([^:]+):(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);

return (int) $matches[1];
return $matches[1].':'.$matches[2];
}

/**