Config.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Man\Core\Lib;
  3. class Config
  4. {
  5. public static $filename;
  6. public static $config = array();
  7. protected static $instances = null;
  8. private function __construct()
  9. {
  10. $config_file = WORKERMAN_ROOT_DIR . 'conf/workerman.conf';
  11. if (!file_exists($config_file))
  12. {
  13. throw new \Exception('Configuration file "' . $config_file . '" not found');
  14. }
  15. self::$config['workerman'] = self::parseFile($config_file);
  16. self::$filename = realpath($config_file);
  17. foreach(glob(WORKERMAN_ROOT_DIR . 'conf/conf.d/*.conf') as $config_file)
  18. {
  19. $worker_name = basename($config_file, '.conf');
  20. self::$config[$worker_name] = self::parseFile($config_file);
  21. }
  22. }
  23. protected static function parseFile($config_file)
  24. {
  25. $config = parse_ini_file($config_file, true);
  26. if (!is_array($config) || empty($config))
  27. {
  28. throw new \Exception('Invalid configuration format');
  29. }
  30. return $config;
  31. }
  32. public static function instance()
  33. {
  34. if (!self::$instances) {
  35. self::$instances = new self();
  36. }
  37. return self::$instances;
  38. }
  39. public static function get($uri)
  40. {
  41. $node = self::$config;
  42. $paths = explode('.', $uri);
  43. while (!empty($paths)) {
  44. $path = array_shift($paths);
  45. if (!isset($node[$path])) {
  46. return null;
  47. }
  48. $node = $node[$path];
  49. }
  50. return $node;
  51. }
  52. public static function getAllWorkers()
  53. {
  54. $copy = self::$config;
  55. unset($copy['workerman']);
  56. return $copy;
  57. }
  58. public static function reload()
  59. {
  60. self::$instances = null;
  61. }
  62. }