Skip to content

Commit 4371762

Browse files
authored
[11.x] Improvements for the ServeCommand (add more loves & elevate DX) (#51957)
* use InteractWithTimes * Improvements for ServeCommand
1 parent 264afa5 commit 4371762

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Illuminate/Foundation/Console/ServeCommand.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Env;
8+
use Illuminate\Support\InteractsWithTime;
89
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Input\InputOption;
1011
use Symfony\Component\Process\PhpExecutableFinder;
@@ -15,6 +16,8 @@
1516
#[AsCommand(name: 'serve')]
1617
class ServeCommand extends Command
1718
{
19+
use InteractsWithTime;
20+
1821
/**
1922
* The console command name.
2023
*
@@ -88,14 +91,14 @@ class ServeCommand extends Command
8891
public function handle()
8992
{
9093
$environmentFile = $this->option('env')
91-
? base_path('.env').'.'.$this->option('env')
92-
: base_path('.env');
94+
? base_path('.env').'.'.$this->option('env')
95+
: base_path('.env');
9396

9497
$hasEnvironment = file_exists($environmentFile);
9598

9699
$environmentLastModified = $hasEnvironment
97-
? filemtime($environmentFile)
98-
: now()->addDays(30)->getTimestamp();
100+
? filemtime($environmentFile)
101+
: now()->addDays(30)->getTimestamp();
99102

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

@@ -242,7 +245,7 @@ protected function getHostAndPort()
242245
protected function canTryAnotherPort()
243246
{
244247
return is_null($this->input->getOption('port')) &&
245-
($this->input->getOption('tries') > $this->portOffset);
248+
($this->input->getOption('tries') > $this->portOffset);
246249
}
247250

248251
/**
@@ -292,23 +295,29 @@ protected function flushOutputBuffer()
292295

293296
$this->requestsPool[$requestPort] = [
294297
$this->getDateFromLine($line),
295-
false,
298+
$this->requestsPool[$requestPort][1] ?? false,
299+
microtime(true),
296300
];
297301
} elseif (str($line)->contains([' [200]: GET '])) {
298302
$requestPort = $this->getRequestPortFromLine($line);
299303

300304
$this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]);
305+
} elseif (str($line)->contains('URI:')) {
306+
$requestPort = $this->getRequestPortFromLine($line);
307+
308+
$this->requestsPool[$requestPort][1] = trim(explode('URI: ', $line)[1]);
301309
} elseif (str($line)->contains(' Closing')) {
302310
$requestPort = $this->getRequestPortFromLine($line);
303311

304312
if (empty($this->requestsPool[$requestPort])) {
305313
$this->requestsPool[$requestPort] = [
306314
$this->getDateFromLine($line),
307315
false,
316+
microtime(true),
308317
];
309318
}
310319

311-
[$startDate, $file] = $this->requestsPool[$requestPort];
320+
[$startDate, $file, $startMicrotime] = $this->requestsPool[$requestPort];
312321

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

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

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

321-
$runTime = $this->getDateFromLine($line)->diffInSeconds($startDate);
330+
$runTime = $this->runTimeForHumans($startMicrotime);
322331

323332
if ($file) {
324333
$this->output->write($file = " $file");
@@ -327,7 +336,7 @@ protected function flushOutputBuffer()
327336
$dots = max(terminal()->width() - mb_strlen($formattedStartedAt) - mb_strlen($file) - mb_strlen($runTime) - 9, 0);
328337

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

src/Illuminate/Foundation/resources/server.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
return false;
1414
}
1515

16+
$formattedDateTime = date('D M j H:i:s Y');
17+
18+
$requestMethod = $_SERVER['REQUEST_METHOD'];
19+
$remoteAddress = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'];
20+
21+
file_put_contents("php://stdout", "[$formattedDateTime] $remoteAddress [$requestMethod] URI: $uri\n");
22+
1623
require_once $publicPath.'/index.php';

0 commit comments

Comments
 (0)