自动加载配置文件

2018-12-15  本文已影响8人  云龙789

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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读