From f40fe7d303544a66f7bcbee8e25c96c743d64d1a Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 19 Sep 2022 16:27:36 +0100 Subject: [PATCH] [9.x] Fixes artisan `serve` command with `PHP_CLI_SERVER_WORKERS` environment variable (#44204) * Fixed erroneous date parse when running with multiple workers. PHP_CLI_SERVER_WORKERS prepends the process PID to the log line. Fixes https://github.com/laravel/framework/issues/44082 * Fixed typo * Apply fixes from StyleCI * Avoids `Server running on..` being displayed multiple times * Update ServeCommand.php Co-authored-by: Wahib Mkadmi <33043525+TheRealMkadmi@users.noreply.github.com> Co-authored-by: StyleCI Bot Co-authored-by: Taylor Otwell --- .../Foundation/Console/ServeCommand.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index c4b32b6d4004..0f863f1407c8 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -53,6 +53,13 @@ class ServeCommand extends Command */ protected $requestsPool; + /** + * Indicates if the "Server running on..." output message has been displayed. + * + * @var bool + */ + protected $serverRunningHasBeenDisplayed = false; + /** * The environment variables that should be passed from host machine to the PHP server process. * @@ -106,6 +113,8 @@ public function handle() $process->stop(5); + $this->serverRunningHasBeenDisplayed = false; + $process = $this->startProcess($hasEnvironment); } @@ -228,10 +237,16 @@ 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; + } + $this->components->info("Server running on [http://{$this->host()}:{$this->port()}]."); $this->comment(' Press Ctrl+C to stop the server'); $this->newLine(); + + $this->serverRunningHasBeenDisplayed = true; } elseif (str($line)->contains(' Accepted')) { $requestPort = $this->getRequestPortFromLine($line); @@ -284,7 +299,11 @@ protected function handleProcessOutput() */ protected function getDateFromLine($line) { - preg_match('/^\[([^\]]+)\]/', $line, $matches); + $regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1 + ? '/^\[\d+]\s\[(.*)]/' + : '/^\[([^\]]+)\]/'; + + preg_match($regex, $line, $matches); return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]); }