Skip to content

Show options + add open flag for Serve command #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 24, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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
{
Expand Down Expand Up @@ -79,6 +86,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);
}

Expand Down Expand Up @@ -149,7 +157,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';
Expand All @@ -161,15 +169,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$xDebugInstalled ? sprintf(
'%s, %s',
phpversion('xdebug'),
$xDebugEnabled ? '<info>enabled</>' : '<error>disabled. Add --xdebug 1 to enable</>',
$xDebugEnabled ? '<info> Enabled </>' : '<error> Disabled </>',
) : '<error>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);
Expand All @@ -185,6 +194,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;
Expand Down
Loading