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] Improvements for the ServeCommand (add more loves & elevate DX) #51957

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions src/Illuminate/Foundation/Console/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Env;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
Expand All @@ -15,6 +16,8 @@
#[AsCommand(name: 'serve')]
class ServeCommand extends Command
{
use InteractsWithTime;

/**
* The console command name.
*
Expand Down Expand Up @@ -88,14 +91,14 @@ class ServeCommand extends Command
public function handle()
{
$environmentFile = $this->option('env')
? base_path('.env').'.'.$this->option('env')
: base_path('.env');
? base_path('.env').'.'.$this->option('env')
: base_path('.env');

$hasEnvironment = file_exists($environmentFile);

$environmentLastModified = $hasEnvironment
? filemtime($environmentFile)
: now()->addDays(30)->getTimestamp();
? filemtime($environmentFile)
: now()->addDays(30)->getTimestamp();

$process = $this->startProcess($hasEnvironment);

Expand Down Expand Up @@ -242,7 +245,7 @@ protected function getHostAndPort()
protected function canTryAnotherPort()
{
return is_null($this->input->getOption('port')) &&
($this->input->getOption('tries') > $this->portOffset);
($this->input->getOption('tries') > $this->portOffset);
}

/**
Expand Down Expand Up @@ -292,23 +295,29 @@ protected function flushOutputBuffer()

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

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

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

if (empty($this->requestsPool[$requestPort])) {
$this->requestsPool[$requestPort] = [
$this->getDateFromLine($line),
false,
microtime(true),
];
}

[$startDate, $file] = $this->requestsPool[$requestPort];
[$startDate, $file, $startMicrotime] = $this->requestsPool[$requestPort];

$formattedStartedAt = $startDate->format('Y-m-d H:i:s');

Expand All @@ -318,7 +327,7 @@ protected function flushOutputBuffer()

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

$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);
$runTime = $this->runTimeForHumans($startMicrotime);

if ($file) {
$this->output->write($file = " $file");
Expand All @@ -327,7 +336,7 @@ protected function flushOutputBuffer()
$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</>");
$this->output->writeln(" <fg=gray>~ {$runTime}</>");
} elseif (str($line)->contains(['Closed without sending a request', 'Failed to poll event'])) {
// ...
} elseif (! empty($line)) {
Expand Down
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/resources/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
return false;
}

$formattedDateTime = date('D M j H:i:s Y');

$requestMethod = $_SERVER['REQUEST_METHOD'];
$remoteAddress = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'];

file_put_contents("php://stdout", "[$formattedDateTime] $remoteAddress [$requestMethod] URI: $uri\n");

require_once $publicPath.'/index.php';
Loading