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