orbit/src/Orbit/Config.php
2020-09-28 09:35:13 -05:00

89 lines
2 KiB
PHP

<?php declare(strict_types=1);
namespace Orbit;
/**
* Config
*
* @package Orbit
*/
class Config
{
public $host = "0.0.0.0";
public $port = 1965;
public $hostname = "localhost";
public $tls_certfile = "";
public $tls_keyfile = "";
public $key_passphrase = "";
public $log_file = ".log/orbit.log";
public $log_level = "info";
public $root_dir = ".";
public $index_file = "index.gmi";
public $enable_directory_index = true;
private $is_development_server = false;
/**
* __construct
*
* @param bool $is_development
* @return void
*/
public function __construct($is_development_server = false)
{
$this->setIsDevelopmentServer($is_development_server);
}
public function setIsDevelopmentServer($is_development_server): void
{
$this->is_development_server = (bool) $is_development_server;
}
public function getIsDevelopmentServer(): bool
{
return $this->is_development_server;
}
/**
* Read config values from ini file
*
* @param string $filename Path to ini file
* @return void
*/
public function readFromIniFile(string $filename): void
{
if (!file_exists($filename) || !is_readable($filename)) {
throw new \Exception("Cannot read config file '$filename'");
}
$ini = parse_ini_file($filename);
$this->readFromArray($ini);
}
/**
* Read config values from array
*
* @param array $data Array of config values
* @return void
*/
public function readFromArray(array $params): void
{
$valid_keys = [
'host', 'port', 'hostname', 'tls_certfile',
'tls_keyfile', 'key_passphrase', 'log_file', 'log_level',
'root_dir', 'index_file', 'enable_directory_index',
];
foreach ($params as $key => $value) {
if (!in_array($key, $valid_keys)) {
continue;
}
$this->{$key} = $value;
}
}
}