Remove manual phar building scripts
[bdrem.git] / src / bdrem / Config.php
1 <?php
2 namespace bdrem;
3
4 class Config
5 {
6     public $date;
7     public $daysPrev;
8     public $daysNext;
9     public $locale;
10     public $renderer;
11     public $source;
12     public $stopOnEmpty;
13
14     public $cfgFiles = array();
15     public $cfgFileExists;
16
17
18
19     public function __construct()
20     {
21         $this->loadConfigFilePaths();
22     }
23
24     public function load()
25     {
26         foreach ($this->cfgFiles as $file) {
27             if (file_exists($file)) {
28                 $this->cfgFileExists = true;
29                 return $this->loadFile($file);
30             }
31         }
32         $this->cfgFileExists = false;
33     }
34
35     protected function loadConfigFilePaths()
36     {
37         $pharFile = \Phar::running();
38         if ($pharFile == '') {
39             $this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
40         } else {
41             //remove phar:// from the path
42             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
43         }
44
45         //TODO: add ~/.config/bdrem.php
46
47         $this->cfgFiles[] = '/etc/bdrem.php';
48     }
49
50     protected function loadFile($filename)
51     {
52         include $filename;
53         $vars = get_defined_vars();
54         foreach ($vars as $k => $value) {
55             if (!isset($this->$k) || $this->$k === null) {
56                 $this->$k = $value;
57             }
58         }
59     }
60
61     public function loadSource()
62     {
63         if ($this->source === null) {
64             throw new \Exception('No source defined');
65         }
66
67         $settings = $this->source;
68         $class = '\\bdrem\\Source_' . array_shift($settings);
69
70         return new $class($settings[0]);
71     }
72
73     public function setDate($date)
74     {
75         if ($date === null) {
76             $this->date = date('Y-m-d');
77         } else {
78             $dt = new \DateTime($date);
79             $this->date = $dt->format('Y-m-d');
80         }
81     }
82
83     public function get($varname, $default = '')
84     {
85         if (!isset($this->$varname) || $this->$varname == '') {
86             return $default;
87         }
88         return $this->$varname;
89     }
90 }
91 ?>