企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
``` 重点函数 public function load($file, $name = '') protected function loadFile($file, $name) { $name = strtolower($name); $type = pathinfo($file, PATHINFO_EXTENSION); if ('php' == $type) { return $this->set(include $file, $name); } elseif ('yaml' == $type && function_exists('yaml_parse_file')) { return $this->set(yaml_parse_file($file), $name); } return $this->parse($file, $type, $name); } public function parse($config, $type = '', $name = '') { if (empty($type)) { $type = pathinfo($config, PATHINFO_EXTENSION); } $object = Loader::factory($type, '\\think\\config\\driver\\', $config); return $this->set($object->parse(), $name); } ``` 五中文件类型解析 loadFile函数默认两种(php,yaml),工厂类think\\config\\driver有三种 ![](https://img.kancloud.cn/ae/e9/aee9e793ed600638cc14015cd24d6666_329x213.png) ``` 配置获取函数,每次都会判断yaconf是否存在 ~~~ /** * 获取配置参数 为空则获取所有配置 * @access public * @param string $name 配置参数名(支持多级配置 .号分割) * @param mixed $default 默认值 * @return mixed */ abc,abc.,abc.azz public function get($name = null, $default = null) { if ($name && false === strpos($name, '.')) { $name = $this->prefix . '.' . $name; } // 无参数时获取所有 if (empty($name)) { return $this->config; } if ('.' == substr($name, -1)) { return $this->pull(substr($name, 0, -1)); } if ($this->yaconf) { $yaconfName = $this->getYaconfName($name); if (Yaconf::has($yaconfName)) { return Yaconf::get($yaconfName); } } $name = explode('.', $name); $name[0] = strtolower($name[0]); $config = $this->config; // 按.拆分成多维数组进行判断 foreach ($name as $val) { if (isset($config[$val])) { $config = $config[$val]; } else { return $default; } } return $config; } ~~~ ~~~ /** * 获取一级配置 * @access public * @param string $name 一级配置名 * @return array */ public function pull($name) { $name = strtolower($name); if ($this->yaconf) { $yaconfName = $this->getYaconfName($name); if (Yaconf::has($yaconfName)) { $config = Yaconf::get($yaconfName); return isset($this->config[$name]) ? array_merge($this->config[$name], $config) : $config;//如果php配置与yaconf配置共存则取yaconf中的配置 } } return isset($this->config[$name]) ? $this->config[$name] : []; } ~~~ ```