Backend programs
[infodrom/musiikki-web.git] / class / config.class.php
1 <?php
2
3 class Config {
4   const PATH = '/etc/musiikki.conf';
5
6   private static $main = false;
7
8   private $values = array();
9
10   public static function main()
11   {
12     if (self::$main === false)
13       self::$main = new Config(Config::PATH);
14
15     return self::$main;
16   }
17
18   private function __construct($path)
19   {
20     $this->loadValues($path);
21   }
22
23   private function loadValues($path)
24   {
25     if (!is_readable($path))
26       throw new Exception('Cannot open config file ' . $path);
27
28     $f = fopen($path, 'r');
29     if ($f === false)
30       throw new Exception('Cannot open config file ' . $path);
31
32     while (($line = fgets($f)) !== false) {
33       $line = trim($line);
34       if (substr($line,0,1) == '#') continue;
35       if (!strlen($line)) continue;
36
37       $parts = explode('=', $line, 2);
38       $this->values[$parts[0]] = $parts[1];
39     }
40
41     fclose($f);
42   }
43
44   public function get($name)
45   {
46     if (!array_key_exists($name, $this->values))
47       return NULL;
48
49     return $this->values[$name];
50   }
51 }