Config.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace StatsDBundle\StatsDBundle\Services;
  3. class Config
  4. {
  5. const INI_FILE = '../Resources/config/statsd.ini';
  6. /**
  7. * @var Config
  8. */
  9. private static $_instance;
  10. /**
  11. * @var array
  12. */
  13. private $_data;
  14. /**
  15. * @param string $ini_file
  16. */
  17. private function __construct($ini_file = self::INI_FILE)
  18. {
  19. if ($ini_file == self::INI_FILE) {
  20. $ini_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . $ini_file;
  21. }
  22. $this->_data = parse_ini_file($ini_file, true);
  23. }
  24. /**
  25. * @return Config
  26. */
  27. public static function getInstance()
  28. {
  29. if (!self::$_instance) {
  30. self::$_instance = new self();
  31. }
  32. return self::$_instance;
  33. }
  34. /**
  35. * @param string $section
  36. *
  37. * @return boolean
  38. */
  39. public function isEnabled($section)
  40. {
  41. return isset($this->_data[$section]);
  42. }
  43. /**
  44. * @param string $name
  45. *
  46. * @return string
  47. */
  48. public function getConfig($name)
  49. {
  50. $name_array = explode('.', $name, 2);
  51. if (count($name_array) < 2) {
  52. return;
  53. }
  54. list($section, $param) = $name_array;
  55. if (!isset($this->_data[$section][$param])) {
  56. return;
  57. }
  58. return $this->_data[$section][$param];
  59. }
  60. }