Skip to content

Commit f03d3a1

Browse files
crynoboneStyleCIBottaylorotwell
authored
[11.x] Fix serve command with PHP_CLI_SERVER_WORKERS (#54606)
* Fixes `serve` command with `PHP_CLI_SERVER_WORKERS` By default, serve command would restart child process when there's modification to the `.env` file. However, with `PHP_CLI_SERVER_WORKERS` set to larger than 1 the process doesn't close properly (port is still being used) and this cause the restart process to pick an incremented port number. The changes here will unset `PHP_CLI_SERVER_WORKERS` value unless the command is executed with `--no-reload` option. fixes #54574 Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * Update src/Illuminate/Foundation/Console/ServeCommand.php * Update src/Illuminate/Foundation/Console/ServeCommand.php * Update ServeCommand.php * Update ServeCommand.php --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9626c3d commit f03d3a1

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Illuminate/Foundation/Console/ServeCommand.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Illuminate\Support\InteractsWithTime;
1010
use Illuminate\Support\Stringable;
1111
use Symfony\Component\Console\Attribute\AsCommand;
12+
use Symfony\Component\Console\Input\InputInterface;
1213
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Output\OutputInterface;
1315
use Symfony\Component\Process\Process;
1416

1517
use function Illuminate\Support\php_binary;
@@ -34,6 +36,13 @@ class ServeCommand extends Command
3436
*/
3537
protected $description = 'Serve the application on the PHP development server';
3638

39+
/**
40+
* The number of PHP CLI server workers.
41+
*
42+
* @var int<2, max>|false
43+
*/
44+
protected $phpServerWorkers = 1;
45+
3746
/**
3847
* The current port offset.
3948
*
@@ -76,14 +85,28 @@ class ServeCommand extends Command
7685
'IGNITION_LOCAL_SITES_PATH',
7786
'LARAVEL_SAIL',
7887
'PATH',
79-
'PHP_CLI_SERVER_WORKERS',
8088
'PHP_IDE_CONFIG',
8189
'SYSTEMROOT',
8290
'XDEBUG_CONFIG',
8391
'XDEBUG_MODE',
8492
'XDEBUG_SESSION',
8593
];
8694

95+
/** {@inheritdoc} */
96+
#[\Override]
97+
protected function initialize(InputInterface $input, OutputInterface $output)
98+
{
99+
$this->phpServerWorkers = transform(env('PHP_CLI_SERVER_WORKERS', 1), function ($workers) {
100+
if (! is_int($workers) || $workers < 2) {
101+
return false;
102+
}
103+
104+
return $workers > 1 && ! $this->option('no-reload') ? false : $workers;
105+
});
106+
107+
parent::initialize($input, $output);
108+
}
109+
87110
/**
88111
* Execute the console command.
89112
*
@@ -154,7 +177,7 @@ protected function startProcess($hasEnvironment)
154177
}
155178

156179
return in_array($key, static::$passthroughVariables) ? [$key => $value] : [$key => false];
157-
})->all());
180+
})->merge(['PHP_CLI_SERVER_WORKERS' => $this->phpServerWorkers])->all());
158181

159182
$this->trap(fn () => [SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signal) use ($process) {
160183
if ($process->isRunning()) {
@@ -360,7 +383,7 @@ protected function flushOutputBuffer()
360383
*/
361384
protected function getDateFromLine($line)
362385
{
363-
$regex = ! windows_os() && env('PHP_CLI_SERVER_WORKERS', 1) > 1
386+
$regex = ! windows_os() && is_int($this->phpServerWorkers)
364387
? '/^\[\d+]\s\[([a-zA-Z0-9: ]+)\]/'
365388
: '/^\[([^\]]+)\]/';
366389

0 commit comments

Comments
 (0)