catch exceptions
[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;
9     public $daysNext;
10     public $locale;
11
12     public function load()
13     {
14         $f = __DIR__ . '/../../data/bdrem.config.php';
15         if (file_exists($f)) {
16             return $this->loadFile($f);
17         }
18
19         throw new \Exception('No config file found');
20     }
21
22     protected function loadFile($filename)
23     {
24         include $filename;
25         $vars = get_defined_vars();
26         foreach ($vars as $k => $value) {
27             $this->$k = $value;
28         }
29     }
30
31     public function loadSource()
32     {
33         if ($this->source === null) {
34             throw new \Exception('No source defined');
35         }
36
37         $settings = $this->source;
38         $class = '\\bdrem\\Source_' . array_shift($settings);
39
40         return new $class($settings[0]);
41     }
42
43     public function get($varname, $default = '')
44     {
45         if (!isset($this->$varname) || $this->$varname == '') {
46             return $default;
47         }
48         return $this->$varname;
49     }
50 }
51 ?>