Skip to content

Commit

Permalink
[9.x] Fixes artisan serve command with PHP_CLI_SERVER_WORKERS env…
Browse files Browse the repository at this point in the history
…ironment 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]>
  • Loading branch information
4 people authored Sep 19, 2022
1 parent b9b5c5c commit f40fe7d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Illuminate/Foundation/Console/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -106,6 +113,8 @@ public function handle()

$process->stop(5);

$this->serverRunningHasBeenDisplayed = false;

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

Expand Down Expand Up @@ -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(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');

$this->newLine();

$this->serverRunningHasBeenDisplayed = true;
} elseif (str($line)->contains(' Accepted')) {
$requestPort = $this->getRequestPortFromLine($line);

Expand Down Expand Up @@ -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]);
}
Expand Down

6 comments on commit f40fe7d

@masihgh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have this issu in laravel framework

@ashraf-frotan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have this issu in laravel framework
I have the same issue.
did you solve this issue?
if you have solved this issue please let me know.

@akmalabaa99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still have this issue on latest version laravel. Does anyone solve this?

@gatelog
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also faced similar issue in Laravel Framework 9.39.0
ErrorException Undefined array key 1
[2022-11-11 16:03:21] local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at D:\wamp\www\gchat\vendor\laravel\framework\src\Illuminate\Foundation\Console\ServeCommand.php:308)

@furkan42arslan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bende çözemedim ama yanlız olmadığıma da sevindim :)

@rexyou0831
Copy link

@rexyou0831 rexyou0831 commented on f40fe7d Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I know is there any news about it?
or it will carry forward to Laravel 10? ><

Please sign in to comment.