From d858146b542885e63e85fe14c1f647e7cc0621ea Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 22 Jun 2024 11:03:22 +0300 Subject: [PATCH 1/4] Show options + add open flag --- src/Command/Serve.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Command/Serve.php b/src/Command/Serve.php index 6feefdee..8336b871 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -12,6 +12,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; use Yiisoft\Yii\Console\ExitCode; @@ -22,6 +23,13 @@ use function is_dir; use function passthru; +use function explode; +use function fclose; +use function file_exists; +use function fsockopen; +use function is_dir; +use function passthru; + #[AsCommand('serve', 'Runs PHP built-in web server')] final class Serve extends Command { @@ -79,6 +87,7 @@ public function configure(): void $this->defaultWorkers ) ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.') + ->addOption('open', 'o', InputOption::VALUE_OPTIONAL, 'Opens the serving server in the default browser.') ->addOption('xdebug', 'x', InputOption::VALUE_OPTIONAL, 'Enables XDEBUG session.', false); } @@ -149,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $xDebugInstalled = extension_loaded('xdebug'); - $xDebugEnabled = $isLinux && $xDebugInstalled && $input->hasOption('xdebug') && $input->getOption('xdebug'); + $xDebugEnabled = $isLinux && $xDebugInstalled && $input->hasOption('xdebug') && $input->getOption('xdebug') === null; if ($xDebugEnabled) { $command[] = 'XDEBUG_MODE=debug XDEBUG_TRIGGER=yes'; @@ -161,15 +170,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $xDebugInstalled ? sprintf( '%s, %s', phpversion('xdebug'), - $xDebugEnabled ? 'enabled' : 'disabled', + $xDebugEnabled ? ' Enabled ' : ' Disabled ', ) : 'Not installed', + '--xdebug', ]; - $outputTable[] = ['Workers', $isLinux ? $workers : 'Not supported']; + $outputTable[] = ['Workers', $isLinux ? $workers : 'Not supported', '--workers, -w']; $outputTable[] = ['Address', $address]; - $outputTable[] = ['Document root', $documentRoot]; - $outputTable[] = ($router ? ['Routing file', $router] : []); + $outputTable[] = ['Document root', $documentRoot, '--docroot, -t']; + $outputTable[] = ($router ? ['Routing file', $router, '--router, -r'] : []); - $io->table(['Configuration'], $outputTable); + $io->table(['Configuration', null, 'Options'], $outputTable); $command[] = '"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"; $command = implode(' ', $command); @@ -185,6 +195,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int return ExitCode::OK; } + $openInBrowser = $input->hasOption('open') && $input->getOption('open') === null; + + if ($openInBrowser) { + passthru('open http://' . $address); + } passthru($command, $result); return $result; From 3f35c59bd2b49991da76619e54d85390cf2f385b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 22 Jun 2024 08:03:36 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/Command/Serve.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Command/Serve.php b/src/Command/Serve.php index 8336b871..ca02482f 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -12,7 +12,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; use Yiisoft\Yii\Console\ExitCode; From 6c48b1e9bf2337a7e91c890dc14ee2238640d45f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 23 Jun 2024 11:54:05 +0300 Subject: [PATCH 3/4] Clean doubled use --- src/Command/Serve.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Command/Serve.php b/src/Command/Serve.php index ca02482f..ec40ee89 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -22,13 +22,6 @@ use function is_dir; use function passthru; -use function explode; -use function fclose; -use function file_exists; -use function fsockopen; -use function is_dir; -use function passthru; - #[AsCommand('serve', 'Runs PHP built-in web server')] final class Serve extends Command { From 4f6cc5b0155ce494ec4a56fab291f78de9d1dd9d Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 23 Jun 2024 12:56:18 +0300 Subject: [PATCH 4/4] Add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f53413..359a4b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 2.2.1 under development -- no changes in this release. +- Enh #207: Add `--open` option for `serve` command (@xepozz) +- Enh #207: Print possible options for `serve` command (@xepozz) ## 2.2.0 February 17, 2024