3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\Commands\Server;
16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
20
* Launch the PHP development server
22
* Not testable, as it throws phpunit for a loop :-/
26
class Serve extends BaseCommand
33
protected $group = 'CodeIgniter';
40
protected $name = 'serve';
47
protected $description = 'Launches the CodeIgniter PHP-Development Server.';
54
protected $usage = 'serve';
59
* @var array<string, string>
61
protected $arguments = [];
64
* The current port offset.
68
protected $portOffset = 0;
71
* The max number of ports to attempt to serve from
75
protected $tries = 10;
80
* @var array<string, string>
82
protected $options = [
83
'--php' => 'The PHP Binary [default: "PHP_BINARY"]',
84
'--host' => 'The HTTP Host [default: "localhost"]',
85
'--port' => 'The HTTP Host Port [default: "8080"]',
91
public function run(array $params)
93
// Collect any user-supplied options and apply them.
94
$php = escapeshellarg(CLI::getOption('php') ?? PHP_BINARY);
95
$host = CLI::getOption('host') ?? 'localhost';
96
$port = (int) (CLI::getOption('port') ?? 8080) + $this->portOffset;
98
// Get the party started.
99
CLI::write('CodeIgniter development server started on http://' . $host . ':' . $port, 'green');
100
CLI::write('Press Control-C to stop.');
102
// Set the Front Controller path as Document Root.
103
$docroot = escapeshellarg(FCPATH);
105
// Mimic Apache's mod_rewrite functionality with user settings.
106
$rewrite = escapeshellarg(SYSTEMPATH . 'rewrite.php');
108
// Call PHP's built-in webserver, making sure to set our
109
// base path to the public folder, and to use the rewrite file
110
// to ensure our environment is set and it simulates basic mod_rewrite.
111
passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);
113
if ($status && $this->portOffset < $this->tries) {