Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Man\Core\Lib;
  3. /**
  4. *
  5. * 配置
  6. * @author walkor
  7. *
  8. */
  9. class Config
  10. {
  11. /**
  12. * 默认应用配置匹配路径
  13. * @var string
  14. */
  15. const DEFAULT_CONFD_PATH = '../../applications/*/conf.d/*.conf';
  16. /**
  17. * 配置文件名称
  18. * @var string
  19. */
  20. public static $configFile;
  21. /**
  22. * 配置数据
  23. * @var array
  24. */
  25. public static $config = array();
  26. /**
  27. * 实例
  28. * @var instance of Config
  29. */
  30. protected static $instances = null;
  31. /**
  32. * 构造函数
  33. * @throws \Exception
  34. */
  35. private function __construct()
  36. {
  37. // 主配置位置
  38. $config_file = WORKERMAN_ROOT_DIR . '/conf/workerman.conf';
  39. if (!file_exists($config_file))
  40. {
  41. throw new \Exception('Configuration file "' . $config_file . '" not found');
  42. }
  43. // 载入主配置
  44. self::$config['workerman'] = self::parseFile($config_file);
  45. self::$config['workerman']['log_dir'] = isset(self::$config['workerman']['log_dir']) ? self::$config['workerman']['log_dir'] : WORKERMAN_ROOT_DIR.'/logs';
  46. self::$configFile = realpath($config_file);
  47. // 寻找应用配置
  48. $conf_d = isset(self::$config['workerman']['include']) ? self::$config['workerman']['include'] : self::DEFAULT_CONFD_PATH;
  49. foreach(glob($conf_d) as $config_file)
  50. {
  51. $worker_name = basename($config_file, '.conf');
  52. $config_data = self::parseFile($config_file);
  53. if(!isset($config_data['enable']) || $config_data['enable'] )
  54. {
  55. self::$config[$worker_name] = self::parseFile($config_file);
  56. }
  57. else
  58. {
  59. continue;
  60. }
  61. // 支持 WORKERMAN_ROOT_DIR 配置
  62. array_walk_recursive(self::$config[$worker_name], array('\Man\Core\Lib\Config', 'replaceWORKERMAN_ROOT_DIR'));
  63. // 找出绝对路径
  64. self::$config[$worker_name]['worker_file'] =dirname($config_file).'/'.self::$config[$worker_name]['worker_file'];
  65. if(!isset(self::$config[$worker_name]['chdir']))
  66. {
  67. self::$config[$worker_name]['chdir'] = dirname($config_file);
  68. }
  69. }
  70. // 整理Monitor配置
  71. self::$config['Monitor'] = self::$config['workerman']['Monitor'];
  72. unset(self::$config['workerman']['Monitor']);
  73. self::$config['Monitor']['worker_file']= '../Common/Monitor.php';
  74. self::$config['Monitor']['persistent_connection'] = 1;
  75. self::$config['Monitor']['start_workers'] = 1;
  76. self::$config['Monitor']['user'] = 'root';
  77. self::$config['Monitor']['preread_length'] = 8192;
  78. self::$config['Monitor']['exclude_path'] = isset(self::$config['Monitor']['exclude_path']) ? array_merge(self::$config['Monitor']['exclude_path'], get_included_files()) : get_included_files();
  79. self::$config['Monitor']['exclude_path'][] = self::$config['workerman']['log_dir'];
  80. self::$config['Monitor']['exclude_path'][] =sys_get_temp_dir();
  81. if(!isset(self::$config['Monitor']['listen']))
  82. {
  83. $socket_file = '/tmp/workerman.'.fileinode(__FILE__).'.sock';
  84. self::$config['Monitor']['listen'] = 'unix://' . $socket_file;
  85. }
  86. // 支持 WORKERMAN_ROOT_DIR 配置
  87. array_walk_recursive(self::$config['Monitor'], array('\Man\Core\Lib\Config', 'replaceWORKERMAN_ROOT_DIR'));
  88. }
  89. /**
  90. * 解析配置文件
  91. * @param string $config_file
  92. * @throws \Exception
  93. */
  94. protected static function parseFile($config_file)
  95. {
  96. $config = parse_ini_file($config_file, true);
  97. if (!is_array($config) || empty($config))
  98. {
  99. throw new \Exception('Invalid configuration format');
  100. }
  101. return $config;
  102. }
  103. /**
  104. * 获取实例
  105. * @return \Man\Core\Lib\instance
  106. */
  107. public static function instance()
  108. {
  109. if (!self::$instances) {
  110. self::$instances = new self();
  111. }
  112. return self::$instances;
  113. }
  114. /**
  115. * 获取配置
  116. * @param string $uri
  117. * @return mixed
  118. */
  119. public static function get($uri)
  120. {
  121. $node = self::$config;
  122. $paths = explode('.', $uri);
  123. while (!empty($paths)) {
  124. $path = array_shift($paths);
  125. if (!isset($node[$path])) {
  126. return null;
  127. }
  128. $node = $node[$path];
  129. }
  130. return $node;
  131. }
  132. /**
  133. * 获取所有的workers
  134. * @return array
  135. */
  136. public static function getAllWorkers()
  137. {
  138. $copy = self::$config;
  139. unset($copy['workerman']);
  140. return $copy;
  141. }
  142. /**
  143. * 重新载入配置
  144. * @return void
  145. */
  146. public static function reload()
  147. {
  148. self::$instances = null;
  149. self::instance();
  150. }
  151. /**
  152. * 替换WORKERMAN_ROOT_DIR为真实WORKERMAN_ROOT_DIR常量表示的路径
  153. * @param mixed $val
  154. */
  155. public static function replaceWORKERMAN_ROOT_DIR(&$val)
  156. {
  157. $val = str_replace('WORKERMAN_ROOT_DIR', WORKERMAN_ROOT_DIR, $val);
  158. }
  159. }