orbit/src/Orbit/Console.php

212 lines
5.2 KiB
PHP

<?php declare(strict_types=1);
namespace Orbit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
/**
* Console
*
* @package Orbit
*/
class Console extends \Qi_Console_Client
{
public static $under_test = false;
/**
* Execute the console command
*
* @param Logger $logger
* @return int Status code
*/
public function execute(Logger $logger = null): int
{
if ($this->_args->get('no-color')) {
$this->_terminal->setIsatty(false);
}
if ($this->_args->version) {
$this->showVersion();
return 0;
}
if ($this->_args->help) {
$this->showHelp();
return 0;
}
$config = $this->makeConfig();
if (!$logger) {
$logger = $this->makeLogger($config, $this->_args->quiet);
}
if (!$this->_args->quiet) {
print "Orbit // Gemini server software\n";
}
$cert = new Cert($config, $logger);
$server = new Server($config, $cert, $logger);
if (!self::$under_test) {
$server->listen();
}
return 0;
}
/**
* Make Config object
*
* @return Config
*/
public function makeConfig(): Config
{
$config = new Config($this->_args->dev);
if ($this->_args->config) {
$config->readFromIniFile($this->_args->config);
}
if ($this->_args->host || $this->_args->host == "0") {
$config->host = $this->_args->host;
}
if ($this->_args->port || $this->_args->port == "0") {
$config->port = $this->_args->port;
}
if ($this->_args->hostname) {
$config->hostname = $this->_args->hostname;
}
if ($this->_args->log) {
$config->log_file = $this->_args->log;
}
if ($this->_args->verbose) {
$config->log_level = Logger::DEBUG;
}
if ($this->_args->get("root-dir")) {
$config->root_dir = $this->_args->get("root-dir");
}
if ($this->_args->get("tls-cert")) {
$config->tls_certfile = $this->_args->get("tls-cert");
}
if ($this->_args->get("tls-key")) {
$config->tls_keyfile = $this->_args->get("tls-key");
}
if ($this->_args->get("tls-passphrase")) {
$config->key_passphrase = $this->_args->get("tls-passphrase");
}
return $config;
}
/**
* Make Logger object
*
* @param Config $config
* @param bool $is_quiet
* @return Logger
*/
public function makeLogger(Config $config, $is_quiet = false): Logger
{
$logger = new Logger('orbit');
$level = Logger::INFO;
if ($config->log_level) {
$level = $config->log_level;
}
$log_stream = new StreamHandler($config->log_file, $level);
$log_stream->setFormatter($this->makeLogFormatter());
$logger->pushHandler($log_stream);
if (!$is_quiet) {
$std_stream = new StreamHandler('php://stdout', $level);
$std_stream->setFormatter($this->makeLogFormatter(true));
$logger->pushHandler($std_stream);
}
return $logger;
}
/**
* Make a LineFormatter object
*
* @return LineFormatter
*/
private function makeLogFormatter($is_tty = false): LineFormatter
{
$pid = getmypid();
if ($is_tty) {
$output = "{2}[%datetime%]{} {3}$pid %channel%.%level_name%:{} %message% %context%\n";
$output = str_replace("{2}", $this->_terminal->do_setaf(2), $output);
$output = str_replace("{3}", $this->_terminal->do_setaf(3), $output);
$output = str_replace("{}", $this->_terminal->do_op(), $output);
} else {
$output = "[%datetime%] $pid %channel%.%level_name%: %message% %context%\n";
}
return new LineFormatter($output, 'Y-m-d\TH:i:s');
}
/**
* Print the version of orbit server
*
* @return void
*/
public function showVersion(): void
{
print "Orbit " . Server::$version . "\n";
}
/**
* Print the help message
*
* @return void
*/
public function showHelp(): void
{
$this->showVersion();
$out = $this->_terminal->do_op()
. "Usage: orbit [options]\n"
. "\n"
. $this->_terminal->do_setaf(3)
. "Options:\n"
. $this->_terminal->do_op();
foreach ($this->_args->getHelp() as $key => $value) {
if (strpos($key, "|")) {
$parts = explode("|", $key);
$left = sprintf("-%s, --%s", $parts[0], $parts[1]);
} else {
$left = sprintf(" --%s", $key);
}
if ($left[-1] == ":") {
$left = substr($left, 0, -1) . " <arg>";
}
$out .= sprintf(
" %s%s %s%s\n",
$this->_terminal->do_setaf(2),
str_pad($left, 20),
$this->_terminal->do_op(),
$value
);
}
print $out;
}
}