65e353d3518dd7f9f5a1eab44983117f457aee4c
[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     public $cfgFileExists;
13
14     public function load()
15     {
16         $f = __DIR__ . '/../../data/bdrem.config.php';
17         if (file_exists($f)) {
18             $this->cfgFileExists = true;
19             return $this->loadFile($f);
20         }
21
22         $this->cfgFileExists = false;
23     }
24
25     protected function loadFile($filename)
26     {
27         include $filename;
28         $vars = get_defined_vars();
29         foreach ($vars as $k => $value) {
30             $this->$k = $value;
31         }
32     }
33
34     public function loadSource()
35     {
36         if ($this->source === null) {
37             throw new \Exception('No source defined');
38         }
39
40         $settings = $this->source;
41         $class = '\\bdrem\\Source_' . array_shift($settings);
42
43         return new $class($settings[0]);
44     }
45
46     public function setDate($date)
47     {
48         if ($date === null) {
49             $this->date = date('Y-m-d');
50         } else {
51             $dt = new \DateTime($date);
52             $this->date = $dt->format('Y-m-d');
53         }
54     }
55
56     public function get($varname, $default = '')
57     {
58         if (!isset($this->$varname) || $this->$varname == '') {
59             return $default;
60         }
61         return $this->$varname;
62     }
63 }
64 ?>