当前位置:首页 > PHP > 正文内容

php arrayaccess的应用场景:配置管理器

高老师7年前 (2018-12-25)PHP1762

上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。

一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?

PHP项目中大部分的框架都是通过数组来保存配置信息,比如微擎在data.php中囊括了全局所有的配置项。实际上微擎的这种方式是不可取的,随着项目越来越大,配置项越来越多,但是并非每次请求的业务都需要用到配置信息,造成多余的内存占用。理想的配置管理应该是按需加载。

例如在我的框架下存在一个config文件夹,其中包含db.php,rpc.php,文件内容如下:

(1).db.php

<?php
return [
    'mysql' => [
        'host' => '127.0.0.1',
        'port' => '3306',
        'user' => 'root',
        'pass' => 'root',
        'dbname' => 'dnet'
    ],
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379'
    ],
];

(2).rpc.php

<?php
return [
    'default' => [
        'ip' => '127.0.0.1',
        'port' => 8199,
        'pools' => 10,
        'local' => true,
    ],
    'order' => [
        'ip' => '127.0.0.1',
        'port' => 8199,
        'pools' => 10,
        'local' => true,
    ]
];

当然随着项目的扩展,config文件夹会存在一堆这样的配置文件,通过我的配置管理器即可做到按需加载。代码如下:

<?php

/**
 * 配置管理器
 * Class Configer
 */
class Configer implements ArrayAccess
{
    /**
     * 配置文件目录
     */
    private $path;

    /**
     * 配置集合
     * @var array
     */
    private $config = [];

    /**
     * 配置实例
     * @var
     */
    private static $instance;

    /**
     * 初始化配置目录
     * Configer constructor.
     */
    public function __construct()
    {
        $this->path = __DIR__ . '/config/';
    }

    /**
     * 配置器单件实例
     */
    public static function instance()
    {
        if (!(self::$instance instanceof Configer))
        {
            self::$instance = new Configer();
        }
        return self::$instance;
    }

    /**
     * 检查指定的Key是否存在
     * @param mixed $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->config[$offset]);
    }

    /**
     * 获取指定的Key值
     * @param mixed $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        if (empty($this->config[$offset]))
        {
            $this->config[$offset] = require $this->path . $offset . '.php';
        }
        return $this->config[$offset];
    }

    /**
     * 重新设置某个Key的值
     * @param mixed $offset
     * @param mixed $value
     * @throws Exception
     */
    public function offsetSet($offset, $value)
    {
        var_dump($offset);

        $this->config[$offset] = $value;
    }

    /**
     * 销毁某个Key的值
     * @param mixed $offset
     */
    public function offsetUnset($offset)
    {


        if (isset($this->config[$offset]))
        {
            $this->config[$offset] = null;
        }
    }

}

//初始化配置器
$configer = Configer::instance();

//获取DB配置信息
//var_dump($configer['db']['mysql']);
//var_dump($configer['db']['redis']);

//获取RPC配置信息
//var_dump($configer['rpc']['order']);

在fpm框架中按需加载的确很重要,但如果是基于swoole的常驻内存性框架,可以直接忽略,遍历文件批量获取即可。

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/73.html

分享给朋友:

“php arrayaccess的应用场景:配置管理器” 的相关文章

PHP可变变量的作用

PHP可变变量的作用

<!doctype html> <html> <head> <meta charset="utf-8"> <title>demo</title> </head> <bod...

PHP session和cookie的关联

PHP session和cookie的关联

先看看下面的代码:<?php session_start(); $_SESSION['username']='lucy'; ?>当我们请求访问上面的脚本,默认会在我们的客户端生成一个名为PHPSESSID的cookie,我这里的值是PHPSESSID=...

php生成器yield from详解

php生成器yield from详解

PHP7中,通过生成器委托(yield from),可以将其他生成器、可迭代的对象、数组委托给外层生成器。外层的生成器会先顺序 yield 委托出来的值,然后继续 yield 本身中定义的值。同时yield from也能获取到生成器的返回值...

thinkphp6定时任务,tp6定时任务,thinkphp定时任务,php定时任务,php定时器

thinkphp6定时任务,tp6定时任务,thinkphp定时任务,php定时任务,php定时器

本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask(1).安装tp6composer create-project topthink/think tp(2).安装定时任务compos...

EasyTask使用redis队列教程

EasyTask使用redis队列教程

场景:模拟验证码发送。仅做代码演示。(1).创建一个验证码发送接口sendCaptcha/**  * 发送验证码  */ public function sendCaptcha() {     //外部参数(获...

thinkphp断线重连

thinkphp断线重连

在编写thinkphp常驻内存的命令行应用中我们需要保证数据库连接不会断开,保证断开还能重新连接,因此大部分人的方案是直接修改tp的数据库配置文件database.php// 开启断线重连 'break_reconnect' => true,通常...