INI.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class Core_Config_INI {
  3. var $_config_file;
  4. var $_data;
  5. public function __construct($config_file) {
  6. $this->_config_file = $config_file;
  7. $this->_data = null;
  8. }
  9. /**
  10. * @return an associative array on success, and FALSE on failure.
  11. */
  12. public function getData() {
  13. if ($this->_data === null) {// @see __construct()
  14. if (!file_exists($this->_config_file)) {
  15. $this->_data = false;
  16. trigger_error('config file not exists', E_USER_WARNING);
  17. } else {
  18. $this->_data = parse_ini_file($this->_config_file, true);// @return false if error
  19. }
  20. }
  21. return $this->_data;
  22. }
  23. public function get($key, $section = null) {
  24. $_data = $this->getData();
  25. if ($_data == false) {
  26. return false;
  27. }
  28. if (null != $section) {
  29. if (array_key_exists($section, $_data)) {
  30. if (array_key_exists($key, $_data[$section])) {
  31. return $_data[$section][$key];
  32. } else {
  33. trigger_error('config error: key('.$key.') not exists in section ('.$section.')', E_USER_NOTICE);
  34. }
  35. } else {
  36. trigger_error('config error: section not exists: ('.$section.')', E_USER_NOTICE);
  37. }
  38. }
  39. else {
  40. if (array_key_exists($key, $_data)) {
  41. return $_data[$key];
  42. } else {
  43. trigger_error('config error: key not exists ('.$key.')', E_USER_NOTICE);
  44. }
  45. }
  46. return false;
  47. }
  48. }