aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/GlobalConfig.php
blob: 63a103164055d363df397a78698348fee24d84d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Digitigrade;

class GlobalConfig extends Singleton {
    private const CONFIG_INI_PATH = __DIR__ . '/../config.ini';
    private array $confData;

    protected function __construct() {
        $this->confData = parse_ini_file(self::CONFIG_INI_PATH, process_sections: true);
    }

    public function getCanonicalHost(): string {
        $host = $this->confData['instance']['hostname'];
        if (isset($this->confData['instance']['port']) && $this->confData['instance']['port'] != ($this->isHttps() ? 443 : 80))
            $host .= ':' . $this->confData['instance']['port'];
        return $host;
    }

    public function isHttps(): bool {
        return $this->confData['debug']['https'] ?? true;
    }

    public function getBasePath(): string {
        return ($this->isHttps() ? 'https://' : 'http://') . $this->getCanonicalHost();
    }

    public function shouldReportTraces(): bool {
        return $this->confData['debug']['send_tracebacks'] ?? false;
    }

    public function getLogLevel(): int {
        return match ($this->confData['debug']['log_level']) {
            'debug' => Logger::LEVEL_DEBUG,
            'info' => Logger::LEVEL_INFO,
            'warning' => Logger::LEVEL_WARNING,
            'error' => Logger::LEVEL_ERROR,
            default => Logger::LEVEL_INFO
        };
    }

    public function getDbConnection(): string {
        return 'pgsql:host=' . $this->confData['database']['host'] .
            ';port=' . $this->confData['database']['port'] .
            ';dbname=' . $this->confData['database']['database'];
    }

    public function getDbUser(): string {
        return $this->confData['database']['username'];
    }

    public function getDbPassword(): string {
        return $this->confData['database']['password'];
    }

    public function getQueueHost(): string {
        return $this->confData['queue']['host'];
    }

    public function getQueuePort(): int {
        return $this->confData['queue']['port'] ?? 11300;
    }

    public function getQueueChannel(): string {
        return $this->confData['queue']['channel'] ?? 'digitigrade';
    }
}