Skip to content

Commit f40fe7d

Browse files
nunomaduroTheRealMkadmiStyleCIBottaylorotwell
authored
[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 #44082 * Fixed typo * Apply fixes from StyleCI * Avoids `Server running on..` being displayed multiple times * Update ServeCommand.php Co-authored-by: Wahib Mkadmi <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent b9b5c5c commit f40fe7d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Illuminate/Foundation/Console/ServeCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class ServeCommand extends Command
5353
*/
5454
protected $requestsPool;
5555

56+
/**
57+
* Indicates if the "Server running on..." output message has been displayed.
58+
*
59+
* @var bool
60+
*/
61+
protected $serverRunningHasBeenDisplayed = false;
62+
5663
/**
5764
* The environment variables that should be passed from host machine to the PHP server process.
5865
*
@@ -106,6 +113,8 @@ public function handle()
106113

107114
$process->stop(5);
108115

116+
$this->serverRunningHasBeenDisplayed = false;
117+
109118
$process = $this->startProcess($hasEnvironment);
110119
}
111120

@@ -228,10 +237,16 @@ protected function handleProcessOutput()
228237
{
229238
return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) {
230239
if (str($line)->contains('Development Server (http')) {
240+
if ($this->serverRunningHasBeenDisplayed) {
241+
return;
242+
}
243+
231244
$this->components->info("Server running on [http://{$this->host()}:{$this->port()}].");
232245
$this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');
233246

234247
$this->newLine();
248+
249+
$this->serverRunningHasBeenDisplayed = true;
235250
} elseif (str($line)->contains(' Accepted')) {
236251
$requestPort = $this->getRequestPortFromLine($line);
237252

@@ -284,7 +299,11 @@ protected function handleProcessOutput()
284299
*/
285300
protected function getDateFromLine($line)
286301
{
287-
preg_match('/^\[([^\]]+)\]/', $line, $matches);
302+
$regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1
303+
? '/^\[\d+]\s\[(.*)]/'
304+
: '/^\[([^\]]+)\]/';
305+
306+
preg_match($regex, $line, $matches);
288307

289308
return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
290309
}

0 commit comments

Comments
 (0)