Cli::serve: fix/improve handling listen address and add -p/--public-url parameter

This commit is contained in:
Benjamin Renard 2023-07-28 10:30:52 +02:00
parent 8321c1cdd2
commit d9451fd66a
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -53,12 +53,13 @@ class Cli {
'serve', 'serve',
array('\\EesyPHP\\Cli', 'cli_serve'), array('\\EesyPHP\\Cli', 'cli_serve'),
___("Start the PHP built-in HTTP server to serve the application"), ___("Start the PHP built-in HTTP server to serve the application"),
'[-h] [-P -O path]', '[-h] [-P -O path] [[A.B.C.D][:port]] [-p http://public.url]',
___( ___(
"This command could be used to start the PHP built-in HTTP server to serve "This command could be used to start the PHP built-in HTTP server to serve
the application. the application.
Additionnal parameters: Additionnal parameters:
-p/--public-url Define the public URL (default: based on listen address)
-P/--enable-profiler Enable Xdebug profiler -P/--enable-profiler Enable Xdebug profiler
-O/--profiler-output [path] Xdebug profiler output directory path" -O/--profiler-output [path] Xdebug profiler output directory path"
) )
@ -310,10 +311,16 @@ Additionnal parameters:
*/ */
public static function cli_serve($command_args) { public static function cli_serve($command_args) {
$listen_address = null; $listen_address = null;
$public_url = null;
$enable_profiler = false; $enable_profiler = false;
$profiler_output_dir = realpath(getcwd()); $profiler_output_dir = realpath(getcwd());
for($i=0; $i < count($command_args); $i++) { for($i=0; $i < count($command_args); $i++) {
switch($command_args[$i]) { switch($command_args[$i]) {
case '-p':
case '--public-url':
$i++;
$public_url = $command_args[$i];
break;
case '-P': case '-P':
case '--enable-profiler': case '--enable-profiler':
if (phpversion('xdebug') === false) if (phpversion('xdebug') === false)
@ -337,7 +344,6 @@ Additionnal parameters:
break; break;
default: default:
if (is_null($listen_address)) { if (is_null($listen_address)) {
$i++;
$listen_address = $command_args[$i]; $listen_address = $command_args[$i];
} }
else else
@ -350,9 +356,9 @@ Additionnal parameters:
if (is_null($listen_address)) if (is_null($listen_address))
$listen_address = '127.0.0.1:8000'; $listen_address = '127.0.0.1:8000';
$parts = explode(':', $listen_address); $parts = explode(':', $listen_address);
if (count($parts) != 2) if (count($parts) > 2)
self :: usage( self :: usage(
I18n::_('Invalid listen address specify. Must be in formart host:port (or :port).') I18n::_('Invalid listen address specify. Must be in format host:port, host or :port.')
); );
if (empty($parts[0])) { if (empty($parts[0])) {
@ -363,11 +369,15 @@ Additionnal parameters:
I18n::_('Invalid listen host specified. Must be an IPv4 or IPv6 address.') I18n::_('Invalid listen host specified. Must be an IPv4 or IPv6 address.')
); );
if (!Check::tcp_or_udp_port($parts[1])) if (count($parts) < 2)
$parts[1] = 8000;
else if (!Check::tcp_or_udp_port($parts[1]))
self :: usage( self :: usage(
I18n::_('Invalid listen port specified. Must be a positive integer between 1 and 65535.') I18n::_('Invalid listen port specified. Must be a positive integer between 1 and 65535.')
); );
$listen_address = implode(':', $parts); $listen_address = implode(':', $parts);
if (is_null($public_url))
$public_url = sprintf("http://%s:%s", $parts[0]=='0.0.0.0'?'127.0.0.1':$parts[0], $parts[1]);
$public_html = App::get('root_directory_path')."/public_html"; $public_html = App::get('root_directory_path')."/public_html";
chdir($public_html) or self :: fatal_error( chdir($public_html) or self :: fatal_error(
@ -388,7 +398,7 @@ Additionnal parameters:
); );
} }
passthru( passthru(
"EESYPHP_SERVE_URL=http://$listen_address ".PHP_BINARY." ". "EESYPHP_SERVE_URL=$public_url ".PHP_BINARY." ".
implode(' ', array_map('escapeshellarg', $args))." index.php", implode(' ', array_map('escapeshellarg', $args))." index.php",
$exit_code $exit_code
); );