aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Config.php
blob: 57d0cfd7d1995527cb8163b3bb0355756071e417 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace bdrem;

class Config
{
    public $date;
    public $daysPrev;
    public $daysNext;
    public $locale;
    public $renderer;
    public $source;
    public $stopOnEmpty;

    public $cfgFiles = array();
    public $cfgFileExists;



    public function __construct()
    {
        $this->loadConfigFilePaths();
    }

    public function load()
    {
        foreach ($this->cfgFiles as $file) {
            if (file_exists($file)) {
                $this->cfgFileExists = true;
                return $this->loadFile($file);
            }
        }
        $this->cfgFileExists = false;
    }

    protected function loadConfigFilePaths()
    {
        $pharFile = \Phar::running();
        if ($pharFile == '') {
            $this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
        } else {
            //remove phar:// from the path
            $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
        }

        //TODO: add ~/.config/bdrem.php

        $this->cfgFiles[] = '/etc/bdrem.php';
    }

    protected function loadFile($filename)
    {
        include $filename;
        $vars = get_defined_vars();
        foreach ($vars as $k => $value) {
            if (!isset($this->$k) || $this->$k === null) {
                $this->$k = $value;
            }
        }
    }

    public function loadSource()
    {
        if ($this->source === null) {
            throw new \Exception('No source defined');
        }

        $settings = $this->source;
        $class = '\\bdrem\\Source_' . array_shift($settings);

        return new $class($settings[0]);
    }

    public function setDate($date)
    {
        if ($date === null) {
            $this->date = date('Y-m-d');
        } else {
            $dt = new \DateTime($date);
            $this->date = $dt->format('Y-m-d');
        }
    }

    public function get($varname, $default = '')
    {
        if (!isset($this->$varname) || $this->$varname == '') {
            return $default;
        }
        return $this->$varname;
    }
}
?>