美文网首页
自动加载配置文件

自动加载配置文件

作者: 云龙789 | 来源:发表于2018-12-15 16:11 被阅读8次

ArrayAccess::offsetGet

主要是处理\ArrayAccessoffsetGet() 方法,如果引入的文件变量不存在,则重新引入,并返回,即可得到此配置文件中的配置

index.php

<?php

define('BASEDIR', __DIR__);

include BASEDIR.'/IMooc/Loader.php';
spl_autoload_register('\\IMooc\\Loader::autoload');

$config = new \IMooc\Config(__DIR__.'/configs');
var_dump($config['controller']);

IMooc\Loader.php

<?php

namespace IMooc;

class Loader
{

    public static function autoload($class)
    {
        require BASEDIR . '/' . str_replace('\\', '/', $class) . '.php';
    }
}

IMooc\Config.php

<?php

namespace IMooc;

class Config implements \ArrayAccess
{
    protected $path;
    protected $configs = array();

    public function __construct($path)
    {
        $this->path = $path;
    }

    /**
     * 获取数组的 $key
     * @param mixed $key
     * @return mixed
     */
    public function offsetGet($key)
    {
        if (empty($this->configs[$key])) {
            $file_path = $this->path . '/' . $key . '.php';
            $config = require $file_path;
            $this->configs[$key] = $config;
        }
        return $this->configs[$key];
    }

    /**
     * @param mixed $key
     * @return bool
     */
    public function offsetExists($key)
    {
        return isset($this->configs[$key]);
    }

    /**
     * @param mixed $key
     * @param mixed $value
     * @throws \Exception
     */
    public function offsetSet($key, $value)
    {
        throw new \Exception('cant not write config file');
    }

    /**
     * 删除数组的 $key
     * @param mixed $key
     */
    public function offsetUnset($key)
    {
        unset($this->configs[$key]);
    }

}

以上是慕课网的一个 Demo ,我看很多资料都摘抄了这个。我主要找不到来源了,所以没有标注原文地址。不过我觉得这个程序是可以优化的。作者现在的做法是,没加载一个文件中的变量,都要先加载这个文件。作者的 $this->configs[$key] 只是将配置文件中的某个配置做了缓存,但是并没有将文件的整个配置做缓存。以下是我做的一个修改,做了文件的缓存。当然,也可以做文件中具体某个配置的缓存。但是我觉得一般来说是没有必要的,因为配置文件一般不会很大

<?php

namespace Common;

class Config implements \ArrayAccess
{
    /**
     * 配置文件名
     * @var string
     */
    protected $path_name;

    /**
     * 配置缓存变量
     * @var array
     */
    protected $configs = array();

    public function __construct($path_name)
    {
        // 如果传入的文件名在数组中不存在,则加载此配置文件,并存入缓存变量
        if (!in_array($path_name, $this->configs)) {
            $this->path_name = $path_name;
            $file_path = BASEDIR . '/Config/' . $this->path_name . '.php';
            if (!file_exists($file_path)) {
                throw new \Exception('配置文件' . $file_path . '不存在');
            }
            $this->configs[$this->path_name] = require $file_path;
        }
    }

    /**
     * 获取配置文件的数据
     * @param bool $file_name
     * @return array
     */
    public function getConfigs($file_name = false)
    {
        if (isset($file_name)) {
            if (!isset($this->configs[$file_name])) {
                return [];
            }
            return $this->configs[$file_name];
        }
        return $this->configs;
    }

    /**
     * 获取数组的 $key
     * @param mixed $key
     * @return mixed
     * @throws \Exception
     */
    public function offsetGet($key)
    {
        if (isset($this->configs[$this->path_name][$key])) {
            return $this->configs[$this->path_name][$key];
        }
        throw new \Exception($this->path_name . '中' . $key . '的配置不存在');
    }

    /**
     * @param mixed $key
     * @return bool
     */
    public function offsetExists($key)
    {
        return isset($this->configs[$this->path_name][$key]);
    }

    /**
     * @param mixed $key
     * @param mixed $value
     * @throws \Exception
     */
    public function offsetSet($key, $value)
    {
        throw new \Exception('cant not write config file');
    }

    /**
     * 删除数组的 $key
     * @param mixed $key
     */
    public function offsetUnset($key)
    {
        unset($this->configs[$this->path_name][$key]);
    }

}
  • 获取配置的全局函数

if (!function_exists('config')) {
    /**
     * 获取配置
     * @param $key
     * @return array
     * @throws Exception
     */
    function config($key)
    {
        if (strpos($key, '.') === false) {
            // 直接返回整个文件的配置
            $cofnig = new \Common\Config($key);
            return $cofnig->getConfigs($key);
        }

        $arr = explode('.', $key);
        // 数组的第一个键就是文件名
        $file_name = array_shift($arr);
        $config = new \Common\Config($file_name);
        $config_data = $config->getConfigs($file_name);

        foreach ($arr as $segment) {
            $config_data = $config_data[$segment];
        }
        return $config_data;
    }
}

相关文章

  • TP框架之自定义配置

    前提介绍 TP版本为:3.2.3 根据TP配置文件自动加载机制:在自动加载配置文件时,会调用TP的function...

  • 自动加载配置文件

    ArrayAccess::offsetGet 主要是处理\ArrayAccess 的 offsetGet() 方...

  • SpringBoot学习笔记一 自动配置原理

    自动配置原理 配置文件的配置属性参照 自动配置原理 springboot启动的时候加载主配置类即是@Springb...

  • Logback配置解析

    logback优点 比较吸引的几个优点如下: 内核重写,初始化内存加载更小 文档比较齐全 支持自动重新加载配置文件...

  • 自动配置原理

    配置文件能配置的属性参照 1、自动配置原理: 1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能...

  • SpringBoot系列之配置文件加载位置

    SpringBoot系列之配置文件加载位置 SpringBoot启动会自动扫描如下位置的application.p...

  • Android集成编译之产品添加

    概述 核心配置文件 原理介绍 注:源码来自7.1 1.AndroidProducts.mk怎么被自动加载? a.b...

  • C# 根据XML配置文件加载列

    根据XML文件路径加载配置文件 UIOperate加载配置文件列 UIOperate

  • MyBatis的工作流程

    MyBatis的工作流程 1 流程图解 2 步骤详情 加载配置文件。需要加载的配置文件包括全局配置文件(SqlMa...

  • webpack简单记录

    安装webpack 修改package.json 添加设置配置文件 配置文件示例 构件命令 加载css 示例 加载...

网友评论

      本文标题:自动加载配置文件

      本文链接:https://www.haomeiwen.com/subject/aueahqtx.html