Config.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. use Yaconf;
  13. class Config implements \ArrayAccess
  14. {
  15. /**
  16. * 配置参数
  17. * @var array
  18. */
  19. protected $config = [];
  20. /**
  21. * 配置前缀
  22. * @var string
  23. */
  24. protected $prefix = 'app';
  25. /**
  26. * 配置文件目录
  27. * @var string
  28. */
  29. protected $path;
  30. /**
  31. * 配置文件后缀
  32. * @var string
  33. */
  34. protected $ext;
  35. /**
  36. * 是否支持Yaconf
  37. * @var bool
  38. */
  39. protected $yaconf;
  40. /**
  41. * 构造方法
  42. * @access public
  43. */
  44. public function __construct($path = '', $ext = '.php')
  45. {
  46. $this->path = $path;
  47. $this->ext = $ext;
  48. $this->yaconf = class_exists('Yaconf');
  49. }
  50. public static function __make(App $app)
  51. {
  52. $path = $app->getConfigPath();
  53. $ext = $app->getConfigExt();
  54. return new static($path, $ext);
  55. }
  56. /**
  57. * 设置开启Yaconf
  58. * @access public
  59. * @param bool|string $yaconf 是否使用Yaconf
  60. * @return void
  61. */
  62. public function setYaconf($yaconf)
  63. {
  64. if ($this->yaconf) {
  65. $this->yaconf = $yaconf;
  66. }
  67. }
  68. /**
  69. * 设置配置参数默认前缀
  70. * @access public
  71. * @param string $prefix 前缀
  72. * @return void
  73. */
  74. public function setDefaultPrefix($prefix)
  75. {
  76. $this->prefix = $prefix;
  77. }
  78. /**
  79. * 解析配置文件或内容
  80. * @access public
  81. * @param string $config 配置文件路径或内容
  82. * @param string $type 配置解析类型
  83. * @param string $name 配置名(如设置即表示二级配置)
  84. * @return mixed
  85. */
  86. public function parse($config, $type = '', $name = '')
  87. {
  88. if (empty($type)) {
  89. $type = pathinfo($config, PATHINFO_EXTENSION);
  90. }
  91. $object = Loader::factory($type, '\\think\\config\\driver\\', $config);
  92. return $this->set($object->parse(), $name);
  93. }
  94. /**
  95. * 加载配置文件(多种格式)
  96. * @access public
  97. * @param string $file 配置文件名
  98. * @param string $name 一级配置名
  99. * @return mixed
  100. */
  101. public function load($file, $name = '')
  102. {
  103. if (is_file($file)) {
  104. $filename = $file;
  105. } elseif (is_file($this->path . $file . $this->ext)) {
  106. $filename = $this->path . $file . $this->ext;
  107. }
  108. if (isset($filename)) {
  109. return $this->loadFile($filename, $name);
  110. } elseif ($this->yaconf && Yaconf::has($file)) {
  111. return $this->set(Yaconf::get($file), $name);
  112. }
  113. return $this->config;
  114. }
  115. /**
  116. * 获取实际的yaconf配置参数
  117. * @access protected
  118. * @param string $name 配置参数名
  119. * @return string
  120. */
  121. protected function getYaconfName($name)
  122. {
  123. if ($this->yaconf && is_string($this->yaconf)) {
  124. return $this->yaconf . '.' . $name;
  125. }
  126. return $name;
  127. }
  128. /**
  129. * 获取yaconf配置
  130. * @access public
  131. * @param string $name 配置参数名
  132. * @param mixed $default 默认值
  133. * @return mixed
  134. */
  135. public function yaconf($name, $default = null)
  136. {
  137. if ($this->yaconf) {
  138. $yaconfName = $this->getYaconfName($name);
  139. if (Yaconf::has($yaconfName)) {
  140. return Yaconf::get($yaconfName);
  141. }
  142. }
  143. return $default;
  144. }
  145. protected function loadFile($file, $name)
  146. {
  147. $name = strtolower($name);
  148. $type = pathinfo($file, PATHINFO_EXTENSION);
  149. if ('php' == $type) {
  150. return $this->set(include $file, $name);
  151. } elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
  152. return $this->set(yaml_parse_file($file), $name);
  153. }
  154. return $this->parse($file, $type, $name);
  155. }
  156. /**
  157. * 检测配置是否存在
  158. * @access public
  159. * @param string $name 配置参数名(支持多级配置 .号分割)
  160. * @return bool
  161. */
  162. public function has($name)
  163. {
  164. if (false === strpos($name, '.')) {
  165. $name = $this->prefix . '.' . $name;
  166. }
  167. return !is_null($this->get($name));
  168. }
  169. /**
  170. * 获取一级配置
  171. * @access public
  172. * @param string $name 一级配置名
  173. * @return array
  174. */
  175. public function pull($name)
  176. {
  177. $name = strtolower($name);
  178. if ($this->yaconf) {
  179. $yaconfName = $this->getYaconfName($name);
  180. if (Yaconf::has($yaconfName)) {
  181. $config = Yaconf::get($yaconfName);
  182. return isset($this->config[$name]) ? array_merge($this->config[$name], $config) : $config;
  183. }
  184. }
  185. return isset($this->config[$name]) ? $this->config[$name] : [];
  186. }
  187. /**
  188. * 获取配置参数 为空则获取所有配置
  189. * @access public
  190. * @param string $name 配置参数名(支持多级配置 .号分割)
  191. * @param mixed $default 默认值
  192. * @return mixed
  193. */
  194. public function get($name = null, $default = null)
  195. {
  196. if ($name && false === strpos($name, '.')) {
  197. $name = $this->prefix . '.' . $name;
  198. }
  199. // 无参数时获取所有
  200. if (empty($name)) {
  201. return $this->config;
  202. }
  203. if ('.' == substr($name, -1)) {
  204. return $this->pull(substr($name, 0, -1));
  205. }
  206. if ($this->yaconf) {
  207. $yaconfName = $this->getYaconfName($name);
  208. if (Yaconf::has($yaconfName)) {
  209. return Yaconf::get($yaconfName);
  210. }
  211. }
  212. $name = explode('.', $name);
  213. $name[0] = strtolower($name[0]);
  214. $config = $this->config;
  215. // 按.拆分成多维数组进行判断
  216. foreach ($name as $val) {
  217. if (isset($config[$val])) {
  218. $config = $config[$val];
  219. } else {
  220. return $default;
  221. }
  222. }
  223. return $config;
  224. }
  225. /**
  226. * 设置配置参数 name为数组则为批量设置
  227. * @access public
  228. * @param string|array $name 配置参数名(支持三级配置 .号分割)
  229. * @param mixed $value 配置值
  230. * @return mixed
  231. */
  232. public function set($name, $value = null)
  233. {
  234. if (is_string($name)) {
  235. if (false === strpos($name, '.')) {
  236. $name = $this->prefix . '.' . $name;
  237. }
  238. $name = explode('.', $name, 3);
  239. if (count($name) == 2) {
  240. $this->config[strtolower($name[0])][$name[1]] = $value;
  241. } else {
  242. $this->config[strtolower($name[0])][$name[1]][$name[2]] = $value;
  243. }
  244. return $value;
  245. } elseif (is_array($name)) {
  246. // 批量设置
  247. if (!empty($value)) {
  248. if (isset($this->config[$value])) {
  249. $result = array_merge($this->config[$value], $name);
  250. } else {
  251. $result = $name;
  252. }
  253. $this->config[$value] = $result;
  254. } else {
  255. $result = $this->config = array_merge($this->config, $name);
  256. }
  257. } else {
  258. // 为空直接返回 已有配置
  259. $result = $this->config;
  260. }
  261. return $result;
  262. }
  263. /**
  264. * 移除配置
  265. * @access public
  266. * @param string $name 配置参数名(支持三级配置 .号分割)
  267. * @return void
  268. */
  269. public function remove($name)
  270. {
  271. if (false === strpos($name, '.')) {
  272. $name = $this->prefix . '.' . $name;
  273. }
  274. $name = explode('.', $name, 3);
  275. if (count($name) == 2) {
  276. unset($this->config[strtolower($name[0])][$name[1]]);
  277. } else {
  278. unset($this->config[strtolower($name[0])][$name[1]][$name[2]]);
  279. }
  280. }
  281. /**
  282. * 重置配置参数
  283. * @access public
  284. * @param string $prefix 配置前缀名
  285. * @return void
  286. */
  287. public function reset($prefix = '')
  288. {
  289. if ('' === $prefix) {
  290. $this->config = [];
  291. } else {
  292. $this->config[$prefix] = [];
  293. }
  294. }
  295. /**
  296. * 设置配置
  297. * @access public
  298. * @param string $name 参数名
  299. * @param mixed $value 值
  300. */
  301. public function __set($name, $value)
  302. {
  303. return $this->set($name, $value);
  304. }
  305. /**
  306. * 获取配置参数
  307. * @access public
  308. * @param string $name 参数名
  309. * @return mixed
  310. */
  311. public function __get($name)
  312. {
  313. return $this->get($name);
  314. }
  315. /**
  316. * 检测是否存在参数
  317. * @access public
  318. * @param string $name 参数名
  319. * @return bool
  320. */
  321. public function __isset($name)
  322. {
  323. return $this->has($name);
  324. }
  325. // ArrayAccess
  326. public function offsetSet($name, $value)
  327. {
  328. $this->set($name, $value);
  329. }
  330. public function offsetExists($name)
  331. {
  332. return $this->has($name);
  333. }
  334. public function offsetUnset($name)
  335. {
  336. $this->remove($name);
  337. }
  338. public function offsetGet($name)
  339. {
  340. return $this->get($name);
  341. }
  342. }