<?php


class Core_Config_INI {

	var $_config_file;
	var $_data;

	public function __construct($config_file) {
		$this->_config_file = $config_file;
		$this->_data = null;
	}

	/**
	 * @return an associative array on success, and FALSE on failure.
	 */
	public function &getData() {
		if ($this->_data === null) {// @see __construct()
			if (!file_exists($this->_config_file)) {
				$this->_data = false;
				trigger_error('config file not exists', E_USER_WARNING);
			} else {
				$this->_data = parse_ini_file($this->_config_file, true);// @return false if error
			}
		}
		return $this->_data;
	}

	public function get($key, $section = null) {
		$_data = $this->getData();
		if ($_data == false) {
			return false;
		}

		if (null != $section) {
			if (array_key_exists($section, $_data)) {
				if (array_key_exists($key, $_data[$section])) {
					return $_data[$section][$key];
				} else {
					trigger_error('config error: key('.$key.') not exists in section ('.$section.')', E_USER_NOTICE);
				}
			} else {
				trigger_error('config error: section not exists: ('.$section.')', E_USER_NOTICE);
			}
		}
		else {
			if (array_key_exists($key, $_data)) {
				return $_data[$key];
			} else {
				trigger_error('config error: key not exists ('.$key.')', E_USER_NOTICE);
			}
		}

		return false;
	}

}
