milk /lib/Milk/Core/Configuration.php

Language PHP Lines 199
MD5 Hash dcc6762547620181a1838832844c0965
Repository https://github.com/geekbuntu/milk.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Milk\Core;

use \StdClass,
	\ArrayAccess,
	\Iterator,
	\IteratorAggregate,
	\ArrayIterator;

// Dependencies
use Milk\Core\Module,
	Milk\Core\Exception,
	Milk\Core\Cache,
	Milk\Core\Translation;

class Configuration extends Module\Singleton {

	public static $current;
	public static $settings = array(
		'default' => array()
	);
	
	private static $group = "default";
	private static $file;
	private static $type;
	
	public $loaded = false;
	
	/**
		Set default configuration settings
			@param $defaults array
	**/
	public function setDefaults($defaults) {
		self::$settings['default'] = array_merge(
			self::$settings['default'], $defaults);
			
		return $this;
	}
	
	/**
		Set current configuration group
			@param $group string
	**/
	public function setGroup($group) {
		if ($group !== 'default')
			self::$current = new Settings(array_merge(self::$settings['default'], self::$settings[$group]));
		else
			self::$current = new Settings(self::$settings['default']);
			
		$this->loaded = true;
		return $this;
	}
	
	/**
		Load configuration settings from file
			@param $file string
			@param $mode
	**/
	public function loadFile($file) {
		if (!is_readable($file))
			throw Exception( _("Config file is missing or could not be read") );

		self::$type = substr($file, strrpos($file, '.')+1);
		
		switch (self::$type) {
			case 'yaml':
				$confobj = yaml_parse_file($file);
			break;
		
			case 'json':
				$data = file_get_contents($file);
				$confobj = json_decode($data, true);
			break;
			
			default: 
				return trigger_error("Unknown config file type");
		}
	
		self::$file = $file;
		self::$settings = array_merge(self::$settings, $confobj);
	}

	public function saveFile($type='') {
		if (empty($type))
			$type = self::$type;
		switch ($type) {
			case 'json':
				$json = json_encode(self::$config);
				file_put_contents(APP_PATH.'/config.json', $json);
			break;
			
			case 'yaml':
				$yaml = yaml_emit(self::object2array(self::$config));
				file_put_contents(APP_PATH.'/config.yaml', $yaml);
			break;
			
			default: 
				return trigger_error("Unknown config file type");
		}
		return true;
	}
	
	public function __get($var) {
		if (isset(self::$current->$var))
			return self::$current->$var;
		else if (isset($this->$var))
			return $this->$var;
	}
	
	public function __isset($var) {
		if (isset(self::$current->$var))
			return true;
		else if (isset($this->$var))
			return true;
		return false;
	}
	
	public static function __callStatic($func, $arguments) {
		$obj = self::getInstance();
		return call_user_func_array($obj->$func, $arguments);
	}
	
	/**
		Static access to the configuration
			@todo We all hate eval() so let's find a better solution
	**/
	public static function get($var) {
		return eval("return self::\$current->$var;");
	}
}

class Settings implements ArrayAccess, Iterator {

	private $position = 0;
	private $settings = array();

    public function __construct(array $settings) {
		$this->position = 0;
		$this->settings = $settings;
	}
	
	public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->settings[] = $value;
        } else {
            $this->settings[$offset] = $value;
        }
    }
	
    public function offsetExists($offset) {
        return isset($this->settings[$offset]);
    }
	
    public function offsetUnset($offset) {
        unset($this->settings[$offset]);
    }
	
    public function offsetGet($offset) {
		if (!isset($this->settings[$offset]))
			return null;
			
		if (is_array($this->settings[$offset]))
			$this->settings[$offset] = new self($this->settings[$offset]);
	
        return $this->settings[$offset];
    }

    public function rewind() {
       return reset($this->settings);
    }

    public function current() {
		return current($this->settings);
    }

    public function key() {
		return key($this->settings);
    }

    public function next() {
        return next($this->settings);
    }

    public function valid() {
        return key($this->settings) !== null;
    }
	
	public function __get($var) {
		if (isset($this->settings[$var])) {
			if (is_array($this->settings[$var]))
				$this->settings[$var] = new self($this->settings[$var]);
			return $this->settings[$var];
		}
	}
	
	public function __isset($var) {
		return isset($this->settings[$var]);
	}
}
Back to Top