Skip to content

Commit

Permalink
Merge pull request #7 from wirecli/dev
Browse files Browse the repository at this point in the history
Dev: serve command enhancement
  • Loading branch information
flydev-fr authored Jul 20, 2023
2 parents 970952e + 814bd79 commit 02148df
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# .github/workflows/release.yml
name: Release
on: workflow_dispatch
on:
push:
branches:
- main
jobs:
release:
runs-on: ubuntu-20.04
Expand Down
62 changes: 58 additions & 4 deletions src/Commands/Common/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\Question;

use Wirecli\Helpers\PwConnector;
use Wirecli\Helpers\WsTools as Tools;

/**
* Class ServeCommand
Expand All @@ -21,7 +25,10 @@ class ServeCommand extends PwConnector {
protected function configure() {
$this
->setName('serve')
->setDescription('Serve ProcessWire via built in PHP webserver');
->setDescription('Serve ProcessWire via built in PHP webserver')
->addOption('scheme', null, InputOption::VALUE_OPTIONAL, 'Scheme to serve on', 'http')
->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host to serve on', 'localhost')
->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port to serve on', '8080');
}

/**
Expand All @@ -31,9 +38,56 @@ protected function configure() {
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$this->checkForProcessWire($output);
$output->writeln("Starting PHP server at localhost:8000");
passthru("php -S localhost:8000");

$scheme = $input->getOption('scheme');
$host = $input->getOption('host');
$port = $input->getOption('port');
$url = "$scheme://$host:$port";
$this->tools->writeComment("Starting PHP server at $url ...");
$this->helper = $this->getHelper('question');

$port = (int)$port;
$pf = @fsockopen($host, $port); // Try to open a socket
if (is_resource($pf)) {
fclose($pf); // Close the previous socket
do {
$this->tools->writeError("Port $port is already in use.");
$this->tools->nl();

$port++; // Increment the port number and try again

$question = new Question($this->tools->getQuestion("Trying another one", (int)$port), $port);
$question->setValidator(function ($answer) {
if ($answer && !filter_var($answer, FILTER_VALIDATE_INT)) {
throw new \RuntimeException('Please enter a valid port number.');
}
return $answer;
});

$newPort = $this->helper->ask($input, $output, $question); // Ask for a new port number
if (is_resource($pf)) { // If the previous socket is still open, close it
fclose($pf);
}
$pf = @fsockopen($host, (int)$newPort);
}
while($pf !== false);

if (is_resource($pf)) { // If the previous socket is still open, close it
fclose($pf);
}
$port = $newPort;
}

$url = "$scheme://$host:$port";
$this->tools->nl();
$this->tools->writeSuccess("PHP server started, serving at $url. Press CTRL+C to stop the server.");
$this->tools->nl();

if (passthru("php -S $host:$port") !== null) {
$this->tools->writeError("Failed to start PHP server.");
return static::FAILURE;
}

return static::SUCCESS;
}
}
}

0 comments on commit 02148df

Please sign in to comment.