easyswoole 中 \EasySwoole\EasySwoole\Config 类读取配置信息使用 Config::getInstance()->getConf() 方法,实际上是从swoole的内存table中读取,
因此可以在框架初始化时就往table中写入配置文件信息,在
EasySwooleEvent::initialize()
方法中加入载入配置文件的方法
定义loadConf方法
/** * 加载配置文件 */ public static function loadConf() { //遍历目录中的文件 $files = File::scanDirectory(EASYSWOOLE_ROOT . '/App/Conf'); if (is_array($files)) { //$files['files'] 一级目录下所有的文件,不包括文件夹 foreach ($files['files'] as $file) { $fileNameArr = explode('.', $file); $fileSuffix = end($fileNameArr);
if ($fileSuffix == 'php') { \EasySwoole\EasySwoole\Config::getInstance()->loadFile($file);//引入之后,文件名自动转为小写,成为配置的key } } } }
在APP目录下创建Conf文件夹(大小写敏感)
创建mysql.php
<?php return [ 'vod' => [ 'host' => \Yaconf::get('mysql.vod.host'), 'port' => \Yaconf::get('mysql.vod.port'), 'username' => \Yaconf::get('mysql.vod.username'), 'password' => \Yaconf::get('mysql.vod.password'), 'timeout' => \Yaconf::get('mysql.vod.timeout'), ], ];
在别处调用使用
$mysqlConf = Config::getInstance()->getConf('mysql.vod'); var_export($mysqlConf); //结果 array ( 'host' => '127.0.0.1', 'port' => '3306', 'username' => 'root', 'password' => 'root', 'timeout' => '30', )