1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/Config.php:
8
  Configuration manager
9
*/
10
11
class Belokan_Config {
12
  
13
  private static
14
    $_instance,
15
    $_config;
16
    
17
  public static function getInstance ()
18
  {
19
    if (self::$_instance === null)
20
      self::$_instance = new self;
21
    return self::$_instance;
22
  }
23
  
24
  public function __construct ()
25
  {
26
    $this->_config['path'] = '';
27
    $this->_config['dir'] = array(
28
      'controllers' => '../controllers/',
29
      'models' => '../models/',
30
      'views' => '../views/',
31
      'langs' => '../langs/',
32
      'logs' => '../logs/'
33
    );
34
    $this->_config['log'] = array(
35
      'autosave' => true,
36
      'belokan' => true
37
    );
38
    $this->_config['format'] = array(
39
      'enabled' => true,
40
      'default' => 'html'
41
    );
42
    $this->_config['intl'] = array(
43
      'enabled' => false,
44
      'default' => 'en'
45
    );
46
  }
47
  
48
  public function loadFile ($config_file=null)
49
  {
50
    if (!file_exists($config_file)) {
51
      if (Belokan_Config::getInstance()->get('log', 'belokan'))
52
        Belokan_Log::getInstance()->add('Belokan', 'ERROR: configuration file not found ('.$config_file.').');
53
      return false;
54
    }
55
    $userConfig = include $config_file;
56
    $this->merge($userConfig);
57
    return true;
58
  }
59
  
60
  public function set ($val, $group, $key=null)
61
  {
62
    if ($key != null) $this->_config[$group][$key] = $val;
63
    else $this->_config[$group] = $val;
64
  }
65
  
66
  public function get ($group, $key=null)
67
  {
68
    if (!$this->exists($group, $key)) return null;
69
    if ($key != null) return $this->_config[$group][$key];
70
    return $this->_config[$group];
71
  }
72
  
73
  public function exists ($group, $key=null)
74
  {
75
    if ($key != null) return isset($this->_config[$group][$key]);
76
    return isset($this->_config[$group]);
77
  }
78
  
79
  private function merge ($user_config)
80
  {
81
    foreach ($user_config as $group => $config) {
82
      if (array_key_exists($group, $this->_config) && is_array($config))
83
        $this->_config[$group] = array_merge($this->_config[$group], $config);
84
      else
85
        $this->_config[$group] = $config;
86
    }
87
  }
88
  
89
}