From 867f8719bffe784ae6ccc86e233dd4303a7cbc31 Mon Sep 17 00:00:00 2001 From: Seth Phat Date: Fri, 28 Jun 2024 21:16:50 +0700 Subject: [PATCH 1/2] use InteractWithTimes --- src/Illuminate/Foundation/Console/ServeCommand.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 0c9cd9f5e6b1..3d46dd53c56c 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -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; @@ -15,6 +16,8 @@ #[AsCommand(name: 'serve')] class ServeCommand extends Command { + use InteractsWithTime; + /** * The console command name. * @@ -318,7 +321,10 @@ protected function flushOutputBuffer() $this->output->write(" $date $time"); - $runTime = $this->getDateFromLine($line)->diffInSeconds($startDate); + $runTime = $this->runTimeForHumans( + $startDate, + $this->getDateFromLine($line) + ); if ($file) { $this->output->write($file = " $file"); From 0d231c9d793e061b0402ecae39b7a207185bdcd4 Mon Sep 17 00:00:00 2001 From: Seth Phat Date: Sat, 29 Jun 2024 09:15:29 +0700 Subject: [PATCH 2/2] Improvements for ServeCommand --- .../Foundation/Console/ServeCommand.php | 27 ++++++++++--------- .../Foundation/resources/server.php | 7 +++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 3d46dd53c56c..ae204bf24c4e 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -91,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); @@ -245,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); } /** @@ -295,12 +295,17 @@ 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); @@ -308,10 +313,11 @@ protected function flushOutputBuffer() $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'); @@ -321,10 +327,7 @@ protected function flushOutputBuffer() $this->output->write(" $date $time"); - $runTime = $this->runTimeForHumans( - $startDate, - $this->getDateFromLine($line) - ); + $runTime = $this->runTimeForHumans($startMicrotime); if ($file) { $this->output->write($file = " $file"); @@ -333,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('.', $dots)); - $this->output->writeln(" ~ {$runTime}s"); + $this->output->writeln(" ~ {$runTime}"); } elseif (str($line)->contains(['Closed without sending a request', 'Failed to poll event'])) { // ... } elseif (! empty($line)) { diff --git a/src/Illuminate/Foundation/resources/server.php b/src/Illuminate/Foundation/resources/server.php index 99eddccc1752..ae59d720298a 100644 --- a/src/Illuminate/Foundation/resources/server.php +++ b/src/Illuminate/Foundation/resources/server.php @@ -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';