Cli: add serve command
This commit is contained in:
parent
c626bc177a
commit
97a6b4b7cb
2 changed files with 96 additions and 0 deletions
|
@ -75,6 +75,38 @@ class Check {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static function ip_address($ip, $allow_private_ip_address=true, $allow_reserved_ip_address=false,
|
||||
$ipv4_only=false, $ipv6_only=false) {
|
||||
$flag = null;
|
||||
if ($ipv4_only)
|
||||
$flag |= FILTER_FLAG_IPV4;
|
||||
|
||||
if ($ipv6_only)
|
||||
$flag |= FILTER_FLAG_IPV6;
|
||||
|
||||
if (!isset($allow_private_ip_address) || !$allow_private_ip_address)
|
||||
$flag |= FILTER_FLAG_NO_PRIV_RANGE;
|
||||
|
||||
if (!isset($allow_reserved_ip_address) || !$allow_reserved_ip_address)
|
||||
$flag |= FILTER_FLAG_NO_RES_RANGE;
|
||||
|
||||
if (!is_null($flag))
|
||||
return (bool)filter_var($ip, FILTER_VALIDATE_IP, $flag);
|
||||
else
|
||||
return (bool)filter_var($ip, FILTER_VALIDATE_IP);
|
||||
}
|
||||
|
||||
public static function tcp_or_udp_port($value) {
|
||||
if (!is_int($value)) {
|
||||
if (!preg_match('/^[0-9]+$/', $value))
|
||||
return false;
|
||||
$value = intval($value);
|
||||
}
|
||||
if ($value <= 0) return false;
|
||||
if ($value > 65635) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function is_empty($val) {
|
||||
switch(gettype($val)) {
|
||||
case "boolean":
|
||||
|
|
64
src/Cli.php
64
src/Cli.php
|
@ -48,6 +48,17 @@ class Cli {
|
|||
"EesyPHP framework.")
|
||||
);
|
||||
}
|
||||
else {
|
||||
self :: add_command(
|
||||
'serve',
|
||||
array('\\EesyPHP\\Cli', 'cli_serve'),
|
||||
I18n :: ___("Start the PHP built-in HTTP server to serve the application"),
|
||||
null,
|
||||
I18n :: ___(
|
||||
"This command could be used to start the PHP built-in HTTP server to serve the ".
|
||||
"application.")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -272,4 +283,57 @@ class Cli {
|
|||
echo "done. Start coding!\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Command to start PHP built-in HTTP server to serve the EesyPHP project
|
||||
*
|
||||
* @param array $command_args The command arguments
|
||||
* @return void
|
||||
*/
|
||||
public static function cli_serve($command_args) {
|
||||
if (count($command_args) > 1) {
|
||||
self :: usage(
|
||||
_('This command only accept one argument (the listen address in formart host:port).')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check listen address
|
||||
$listen_address = ($command_args?$command_args[0]:'127.0.0.1:8000');
|
||||
$parts = explode(':', $listen_address);
|
||||
if (count($parts) != 2) {
|
||||
self :: usage(
|
||||
_('Invalid listen address specify. Must be in formart host:port.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($parts[0])) {
|
||||
$parts[0] = '0.0.0.0';
|
||||
}
|
||||
else if (!Check::ip_address($parts[0])) {
|
||||
self :: usage(
|
||||
_('Invalid listen host specified. Must be an IPv4 or IPv6 address.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Check::tcp_or_udp_port($parts[1])) {
|
||||
self :: usage(
|
||||
_('Invalid listen port specified. Must be a positive integer between 1 and 65535.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
$listen_address = implode(':', $parts);
|
||||
|
||||
$public_html = App::get('root_directory_path')."/public_html";
|
||||
chdir($public_html) or die(
|
||||
sprintf(
|
||||
'Fail to enter in the public_html directory of the application (%s).',
|
||||
$public_html
|
||||
)
|
||||
);
|
||||
passthru(PHP_BINARY." -S $listen_address index.php", $exit_code);
|
||||
exit($exit_code);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue