thinkphp5源码剖析系列2-配置文件

前言

tp5的配置种类包含四个分类

  • 惯例配置

    核心框架内置的配置文件(thinkphp/convention.php),无需更改。

  • 应用配置

    每个应用的全局配置文件(项目根目录下app/config目录下的文件)。

  • 模块配置

    每个模块的配置文件(相同配置参数会覆盖应用配置。)比如index模块app/index/config/database.php

  • 动态配置

    主要指在控制器或行为中进行(动态)更改配置。建议少用

以上配置优先级越来越高

ArrayAccess相关知识

如果你的类实现了ArrayAccess接口,那么这个类的对象就可以使用$foo['xxx']这种结构了。

$foo['xxx'] 对应调用offsetGet方法。

$foo['xxx'] = 'yyy' 对应调用offsetSet方法。

isset($foo['xxx']) 对应调用offsetExists方法。

unset($foo['xxx']) 对应调用offsetUnset方法。
具体实现:

<?php
namespace lib;
class ArrayObj implements ArrayAccess{
    private $arr = [
        'name' => 'cl',
        'age' => '25'
    ];
    public function offsetExists( $offset ) {
        return isset($this->arr[$offset]);
    }

    public function offsetGet( $offset ) {
        return $this->arr[$offset];
    }

    public function offsetSet( $offset, $value ) {
        return $this->arr[$offset] = $value;
    }

    public function offsetUnset( $offset ) {
        unset( $this->arr[$offset]);
    }
}

入口文件

// [ 应用入口文件 ]
namespace think;

// 加载基础文件
require __DIR__ . '/../thinkphp/base.php';

// 支持事先使用静态方法设置Request对象和Config对象

// 执行应用并响应
Container::get('app')->run()->send();
// 走向 --App.php => run() => initialize() => init()

App.php


    /**
     * 初始化应用或模块
     * @access public
     * @param  string $module 模块名
     * @return void
     */
    public function init($module = '')
    {
        // 定位模块目录
        $module = $module ? $module . DIRECTORY_SEPARATOR : '';
        $path   = $this->appPath . $module;

        // 加载初始化文件
        if (is_file($path . 'init.php')) {
            include $path . 'init.php';
        } elseif (is_file($this->runtimePath . $module . 'init.php')) {
            include $this->runtimePath . $module . 'init.php';
        } else {
            // 加载行为扩展文件
            if (is_file($path . 'tags.php')) {
                $tags = include $path . 'tags.php';
                if (is_array($tags)) {
                    $this->hook->import($tags);
                }
            }

            // 加载公共文件
            if (is_file($path . 'common.php')) {
                echo $path . 'common.php';
                include_once $path . 'common.php';
            }

            if ('' == $module) {
                var_dump($module);
                // 加载系统助手函数
                include $this->thinkPath . 'helper.php';
            }

            // 加载中间件
            if (is_file($path . 'middleware.php')) {
                $middleware = include $path . 'middleware.php';
                if (is_array($middleware)) {
                    $this->middleware->import($middleware);
                }
            }

            // 注册服务的容器对象实例
            if (is_file($path . 'provider.php')) {
                $provider = include $path . 'provider.php';
                if (is_array($provider)) {
                    $this->bindTo($provider);
                }
            }
            
            /**
             * 该处一共进来两遍,第一遍,$module为"",第二遍,$module为index
             * 第一遍判断的是:
             * string(61) "D:workspaceprojectphpSite	p5.1code	p5applicationconfig"
             * string(50) "D:workspaceprojectphpSite	p5.1code	p5config"
             * 第二遍判断的是:
             * string(67) "D:workspaceprojectphpSite	p5.1code	p5applicationindexconfig"
             * string(56) "D:workspaceprojectphpSite	p5.1code	p5configindex"
             */
            // 自动读取配置文件
            if (is_dir($path . 'config')) {
                $dir = $path . 'config' . DIRECTORY_SEPARATOR;
            } elseif (is_dir($this->configPath . $module)) {
                $dir = $this->configPath . $module;
            }
            /*
             * 框架初始情况,$dir只有在第一遍的第一个if里面才是目录
             * 即$dir : D:workspaceprojectphpSite	p5.1code	p5applicationconfig
             */

            // scandir() 函数返回指定目录中的文件和目录的数组。
            $files = isset($dir) ? scandir($dir) : [];
            // 把所有.php配置文件循环初始化
            foreach ($files as $file) {
                /*
                 * pathinfo($file,PATHINFO_EXTENSION) :返回文件后缀名
                 * $this->configExt : 配置文件的后缀形式,为".php"
                 * 下面的判断是判断文件后缀名是否是配置文件后缀名
                 */
                
                if ('.' . pathinfo($file, PATHINFO_EXTENSION) === $this->configExt) {
                    /* 
                     * 将配置文件绝对地址,以及配置文件名作为参数传过去。
                     * 简单来说,上面做的就是通过config的不同存在形式
                     * ,得到config目录中所有的文件并筛选符号要求的
                     */
                    
                    $this->config->load($dir . $file, pathinfo($file, PATHINFO_FILENAME));
                }
            }
        }
        // var_dump( Config::config);
        $this->setModulePath($path);

        if ($module) {
            // 对容器中的对象实例进行配置更新
            $this->containerConfigUpdate($module);
        }
    }

Config.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

use Yaconf;

class Config implements ArrayAccess
{
    /**
     * 配置参数
     * @var array
     */
    protected $config = [];

    /**
     * 配置前缀
     * @var string
     */
    protected $prefix = 'app';

    /**
     * 配置文件目录
     * @var string
     */
    protected $path;

    /**
     * 配置文件后缀
     * @var string
     */
    protected $ext;

    /**
     * 是否支持Yaconf
     * @var bool
     */
    protected $yaconf;

    /**
     * 构造方法
     * @access public
     */
    public function __construct($path = '', $ext = '.php')
    {
        $this->path   = $path;
        $this->ext    = $ext;
        $this->yaconf = class_exists('Yaconf');
    }

    public static function __make(App $app)
    {
        $path = $app->getConfigPath();
        $ext  = $app->getConfigExt();
        return new static($path, $ext);
    }

    /**
     * 设置开启Yaconf
     * @access public
     * @param  bool|string    $yaconf  是否使用Yaconf
     * @return void
     */
    public function setYaconf($yaconf)
    {
        if ($this->yaconf) {
            $this->yaconf = $yaconf;
        }
    }

    /**
     * 设置配置参数默认前缀
     * @access public
     * @param string    $prefix 前缀
     * @return void
     */
    public function setDefaultPrefix($prefix)
    {
        $this->prefix = $prefix;
    }

    /**
     * 解析配置文件或内容
     * @access public
     * @param  string    $config 配置文件路径或内容
     * @param  string    $type 配置解析类型
     * @param  string    $name 配置名(如设置即表示二级配置)
     * @return mixed
     */
    public function parse($config, $type = '', $name = '')
    {
        if (empty($type)) {
            $type = pathinfo($config, PATHINFO_EXTENSION);
        }

        /*
         * 伟大的工厂模式,
         * 如果以后多了其他后缀的配置文件,
         * 只需在 \think\config\driver\ 新增一个类(类名对应配置文件后缀名),并且实现parse() 解析规则方法
         */
        $object = Loader::factory($type, '\think\config\driver\', $config);

        // 殊途同归
        return $this->set($object->parse(), $name);
    }

    /**
     * 加载配置文件(多种格式)
     * @access public
     * @param  string    $file 配置文件名
     * @param  string    $name 一级配置名
     * @return mixed
     */
    public function load($file, $name = '')
    {
        /*
         * $file :string(57) "D:workspaceprojectphpSite	p5.1code	p5configapp.php"
         * 下面两个if都默认走第一个
         */
        if (is_file($file)) {
            $filename = $file;
        } elseif (is_file($this->path . $file . $this->ext)) {
            $filename = $this->path . $file . $this->ext;
        }
        
        if (isset($filename)) {
            return $this->loadFile($filename, $name);
        } elseif ($this->yaconf && Yaconf::has($file)) {
            return $this->set(Yaconf::get($file), $name);
        }

        return $this->config;
    }

    /**
     * 获取实际的yaconf配置参数
     * @access protected
     * @param  string    $name 配置参数名
     * @return string
     */
    protected function getYaconfName($name)
    {
        if ($this->yaconf && is_string($this->yaconf)) {
            return $this->yaconf . '.' . $name;
        }

        return $name;
    }

    /**
     * 获取yaconf配置
     * @access public
     * @param  string    $name 配置参数名
     * @param  mixed     $default   默认值
     * @return mixed
     */
    public function yaconf($name, $default = null)
    {
        if ($this->yaconf) {
            $yaconfName = $this->getYaconfName($name);

            if (Yaconf::has($yaconfName)) {
                return Yaconf::get($yaconfName);
            }
        }

        return $default;
    }

    protected function loadFile($file, $name)
    {
        $name = strtolower($name);
        // 得到文件后缀名:"php"
        $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);
        }

        // xml json ini。。。之类的文件
        return $this->parse($file, $type, $name);
    }

    /**
     * 检测配置是否存在
     * @access public
     * @param  string    $name 配置参数名(支持多级配置 .号分割)
     * @return bool
     */
    public function has($name)
    {
        if (false === strpos($name, '.')) {
            $name = $this->prefix . '.' . $name;
        }

        return !is_null($this->get($name));
    }

    /**
     * 获取一级配置
     * @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;
            }
        }

        return isset($this->config[$name]) ? $this->config[$name] : [];
    }

    /**
     * 获取配置参数 为空则获取所有配置
     * @access public
     * @param  string    $name      配置参数名(支持多级配置 .号分割)
     * @param  mixed     $default   默认值
     * @return mixed
     */
    public function get($name = null, $default = null)
    {
        // 如果没有配置名,则给他添加配置名app
        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;

        // 按.拆分成多维数组进行判断
        // ['app','app_name']
        foreach ($name as $val) {
            // 巧妙!
            if (isset($config[$val])) {
                $config = $config[$val];
            } else {
                return $default;
            }
        }

        return $config;
    }

    /**
     * 设置配置参数 name为数组则为批量设置
     * @access public
     * @param  string|array  $name 配置参数名(支持三级配置 .号分割)
     * @param  mixed         $value 配置值
     * @return mixed
     */
    public function set($name, $value = null)
    {
        // 设置配置,一般为 手动配置触发
        if (is_string($name)) {
            // 如果没有前缀,给它app前缀
            if (false === strpos($name, '.')) {
                $name = $this->prefix . '.' . $name;
            }

            $name = explode('.', $name, 3);

            if (count($name) == 2) {
                $this->config[strtolower($name[0])][$name[1]] = $value;
            } else {
                $this->config[strtolower($name[0])][$name[1]][$name[2]] = $value;
            }

            return $value;
        } elseif (is_array($name)) {
            // 批量设置配置,一般为框架内部初始化配置时触发
            // 批量设置
            if (!empty($value)) {
                // 这里是精髓,如果有该键,那么用 array_merge 将现在的配置合并之前的配置
                if (isset($this->config[$value])) {
                    $result = array_merge($this->config[$value], $name);
                } else {
                    $result = $name;
                }

                $this->config[$value] = $result;
            } else {
                // 如果没有配置名,则合并配置到主配置里面
                $result = $this->config = array_merge($this->config, $name);
            }
        } else {
            // 为空直接返回 已有配置
            $result = $this->config;
        }

        return $result;
    }

    /**
     * 移除配置
     * @access public
     * @param  string  $name 配置参数名(支持三级配置 .号分割)
     * @return void
     */
    public function remove($name)
    {
        if (false === strpos($name, '.')) {
            $name = $this->prefix . '.' . $name;
        }

        $name = explode('.', $name, 3);

        if (count($name) == 2) {
            unset($this->config[strtolower($name[0])][$name[1]]);
        } else {
            unset($this->config[strtolower($name[0])][$name[1]][$name[2]]);
        }
    }

    /**
     * 重置配置参数
     * @access public
     * @param  string    $prefix  配置前缀名
     * @return void
     */
    public function reset($prefix = '')
    {
        if ('' === $prefix) {
            $this->config = [];
        } else {
            $this->config[$prefix] = [];
        }
    }

    /**
     * 设置配置
     * @access public
     * @param  string    $name  参数名
     * @param  mixed     $value 值
     */
    public function __set($name, $value)
    {
        return $this->set($name, $value);
    }

    /**
     * 获取配置参数
     * @access public
     * @param  string $name 参数名
     * @return mixed
     */
    public function __get($name)
    {
        return $this->get($name);
    }

    /**
     * 检测是否存在参数
     * @access public
     * @param  string $name 参数名
     * @return bool
     */
    public function __isset($name)
    {
        return $this->has($name);
    }

    // ArrayAccess
    public function offsetSet($name, $value)
    {
        $this->set($name, $value);
    }

    public function offsetExists($name)
    {
        return $this->has($name);
    }

    public function offsetUnset($name)
    {
        $this->remove($name);
    }

    public function offsetGet($name)
    {
        return $this->get($name);
    }
}

总结

tp5中的配置文件,在app初始化时,根据配置目录规则加载所有的配置文件,并解析到Config类对应的对象的属性中,Config类开放get方法以供应用调用配置项

原文地址:https://www.cnblogs.com/cl94/p/12636611.html