Config.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Config implements \ArrayAccess
  13. {
  14. /**
  15. * 配置参数
  16. * @var array
  17. */
  18. private $config = [];
  19. /**
  20. * 缓存前缀
  21. * @var string
  22. */
  23. private $prefix = 'app';
  24. /**
  25. * 设置配置参数默认前缀
  26. * @access public
  27. * @param string $prefix 前缀
  28. * @return void
  29. */
  30. public function setDefaultPrefix($prefix)
  31. {
  32. $this->prefix = $prefix;
  33. }
  34. /**
  35. * 解析配置文件或内容
  36. * @access public
  37. * @param string $config 配置文件路径或内容
  38. * @param string $type 配置解析类型
  39. * @param string $name 配置名(如设置即表示二级配置)
  40. * @return mixed
  41. */
  42. public function parse($config, $type = '', $name = '')
  43. {
  44. if (empty($type)) {
  45. $type = pathinfo($config, PATHINFO_EXTENSION);
  46. }
  47. $class = false !== strpos($type, '\\') ? $type : '\\think\\config\\driver\\' . ucwords($type);
  48. return $this->set((new $class())->parse($config), $name);
  49. }
  50. /**
  51. * 加载配置文件(多种格式)
  52. * @access public
  53. * @param string $file 配置文件名
  54. * @param string $name 一级配置名
  55. * @return mixed
  56. */
  57. public function load($file, $name = '')
  58. {
  59. if (is_file($file)) {
  60. $name = strtolower($name);
  61. $type = pathinfo($file, PATHINFO_EXTENSION);
  62. if ('php' == $type) {
  63. return $this->set(include $file, $name);
  64. } elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
  65. return $this->set(yaml_parse_file($file), $name);
  66. } else {
  67. return $this->parse($file, $type, $name);
  68. }
  69. } else {
  70. return $this->config;
  71. }
  72. }
  73. /**
  74. * 自动加载配置文件(PHP格式)
  75. * @access public
  76. * @param string $name 配置名
  77. * @return void
  78. */
  79. protected function autoLoad($name)
  80. {
  81. // 如果尚未载入 则动态加载配置文件
  82. $module = Container::get('request')->module();
  83. $module = $module ? $module . '/' : '';
  84. $app = Container::get('app');
  85. $path = $app->getAppPath() . $module;
  86. if (is_dir($path . 'config')) {
  87. $file = $path . 'config/' . $name . $app->getConfigExt();
  88. } elseif (is_dir($app->getConfigPath() . $module)) {
  89. $file = $app->getConfigPath() . $module . $name . $app->getConfigExt();
  90. }
  91. if (isset($file) && is_file($file)) {
  92. $this->load($file, $name);
  93. }
  94. }
  95. /**
  96. * 检测配置是否存在
  97. * @access public
  98. * @param string $name 配置参数名(支持多级配置 .号分割)
  99. * @return bool
  100. */
  101. public function has($name)
  102. {
  103. if (!strpos($name, '.')) {
  104. $name = $this->prefix . '.' . $name;
  105. }
  106. return $this->get($name) ? true : false;
  107. }
  108. /**
  109. * 获取一级配置
  110. * @access public
  111. * @param string $name 一级配置名
  112. * @return array
  113. */
  114. public function pull($name)
  115. {
  116. $name = strtolower($name);
  117. if (!isset($this->config[$name])) {
  118. // 如果尚未载入 则动态加载配置文件
  119. $this->autoLoad($name);
  120. }
  121. return isset($this->config[$name]) ? $this->config[$name] : [];
  122. }
  123. /**
  124. * 获取配置参数 为空则获取所有配置
  125. * @access public
  126. * @param string $name 配置参数名(支持多级配置 .号分割)
  127. * @return mixed
  128. */
  129. public function get($name = null)
  130. {
  131. // 无参数时获取所有
  132. if (empty($name)) {
  133. return $this->config;
  134. }
  135. if (!strpos($name, '.')) {
  136. $name = $this->prefix . '.' . $name;
  137. } elseif ('.' == substr($name, -1)) {
  138. return $this->pull(substr($name, 0, -1));
  139. }
  140. $name = explode('.', $name);
  141. $name[0] = strtolower($name[0]);
  142. $config = $this->config;
  143. if (!isset($config[$name[0]])) {
  144. // 如果尚未载入 则动态加载配置文件
  145. $this->autoLoad($name[0]);
  146. }
  147. // 按.拆分成多维数组进行判断
  148. foreach ($name as $val) {
  149. if (isset($config[$val])) {
  150. $config = $config[$val];
  151. } else {
  152. return;
  153. }
  154. }
  155. return $config;
  156. }
  157. /**
  158. * 设置配置参数 name为数组则为批量设置
  159. * @access public
  160. * @param string|array $name 配置参数名(支持三级配置 .号分割)
  161. * @param mixed $value 配置值
  162. * @return mixed
  163. */
  164. public function set($name, $value = null)
  165. {
  166. if (is_string($name)) {
  167. if (!strpos($name, '.')) {
  168. $name = $this->prefix . '.' . $name;
  169. }
  170. $name = explode('.', $name, 3);
  171. if (count($name) == 2) {
  172. $this->config[strtolower($name[0])][$name[1]] = $value;
  173. } else {
  174. $this->config[strtolower($name[0])][$name[1]][$name[2]] = $value;
  175. }
  176. return $value;
  177. } elseif (is_array($name)) {
  178. // 批量设置
  179. if (!empty($value)) {
  180. if (isset($this->config[$value])) {
  181. $result = array_merge($this->config[$value], $name);
  182. } else {
  183. $result = $name;
  184. }
  185. $this->config[$value] = $result;
  186. } else {
  187. $result = $this->config = array_merge($this->config, $name);
  188. }
  189. } else {
  190. // 为空直接返回 已有配置
  191. $result = $this->config;
  192. }
  193. return $result;
  194. }
  195. /**
  196. * 移除配置
  197. * @access public
  198. * @param string $name 配置参数名(支持三级配置 .号分割)
  199. * @return void
  200. */
  201. public function remove($name)
  202. {
  203. if (!strpos($name, '.')) {
  204. $name = $this->prefix . '.' . $name;
  205. }
  206. $name = explode('.', $name, 3);
  207. if (count($name) == 2) {
  208. unset($this->config[strtolower($name[0])][$name[1]]);
  209. } else {
  210. unset($this->config[strtolower($name[0])][$name[1]][$name[2]]);
  211. }
  212. }
  213. /**
  214. * 重置配置参数
  215. * @access public
  216. * @param string $prefix 配置前缀名
  217. * @return void
  218. */
  219. public function reset($prefix = '')
  220. {
  221. if ('' === $prefix) {
  222. $this->config = [];
  223. } else {
  224. $this->config[$prefix] = [];
  225. }
  226. }
  227. /**
  228. * 设置配置
  229. * @access public
  230. * @param string $name 参数名
  231. * @param mixed $value 值
  232. */
  233. public function __set($name, $value)
  234. {
  235. return $this->set($name, $value);
  236. }
  237. /**
  238. * 获取配置参数
  239. * @access public
  240. * @param string $name 参数名
  241. * @return mixed
  242. */
  243. public function __get($name)
  244. {
  245. return $this->get($name);
  246. }
  247. /**
  248. * 检测是否存在参数
  249. * @access public
  250. * @param string $name 参数名
  251. * @return bool
  252. */
  253. public function __isset($name)
  254. {
  255. return $this->has($name);
  256. }
  257. // ArrayAccess
  258. public function offsetSet($name, $value)
  259. {
  260. $this->set($name, $value);
  261. }
  262. public function offsetExists($name)
  263. {
  264. return $this->has($name);
  265. }
  266. public function offsetUnset($name)
  267. {
  268. $this->remove($name);
  269. }
  270. public function offsetGet($name)
  271. {
  272. return $this->get($name);
  273. }
  274. }