Skip to content
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

Add retry creation server when the port is used #2544

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Changes from all 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
24 changes: 22 additions & 2 deletions system/Commands/Server/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ class Serve extends BaseCommand
*/
protected $arguments = [];

/**
* The current port offset.
*
* @var int
*/
protected $portOffset = 0;

/**
* The max number of ports to attempt to serve from
*
* @var int
*/
protected $tries = 10;

/**
* Options
*
Expand Down Expand Up @@ -124,7 +138,7 @@ public function run(array $params)
// Collect any user-supplied options and apply them.
$php = CLI::getOption('php') ?? PHP_BINARY;
$host = CLI::getOption('host') ?? 'localhost';
$port = CLI::getOption('port') ?? '8080';
$port = (int) (CLI::getOption('port') ?? '8080') + $this->portOffset;

// Get the party started.
CLI::write('CodeIgniter development server started on http://' . $host . ':' . $port, 'green');
Expand All @@ -139,7 +153,13 @@ public function run(array $params)
// Call PHP's built-in webserver, making sure to set our
// base path to the public folder, and to use the rewrite file
// to ensure our environment is set and it simulates basic mod_rewrite.
passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite);
passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);

if ($status && $this->portOffset < $this->tries) {
$this->portOffset += 1;

$this->run($params);
}
}

}