Skip to content

Commit

Permalink
Merge pull request #2544 from thanhtaivtt/develop
Browse files Browse the repository at this point in the history
Add retry creation server when the port is used
  • Loading branch information
lonnieezell authored Feb 13, 2020
2 parents 1449773 + 5d472ea commit ddfc072
Showing 1 changed file with 22 additions and 2 deletions.
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);
}
}

}

0 comments on commit ddfc072

Please sign in to comment.