PHP code example of baagee / php-config
1. Go to this page and download the library: Download baagee/php-config library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baagee / php-config example snippets
interface ConfigInterface
{
public static function get(string $key);
}
// 解析ini配置文件
//ini方法两个参数 第一个是配置文件存放的根目录
\BaAGee\Config\Config::init(__DIR__ . '/config', \BaAGee\Config\Parser\IniParser::class);
$password = \BaAGee\Config\Config::get('memcache/password');// 获取memcache文件的password值
$host = \BaAGee\Config\Config::get('memcache/host');
//使用内置的yaml配置解析 需要安装yaml扩展
\BaAGee\Config\Config::init(__DIR__ . '/config', \BaAGee\Config\Parser\YamlParser::class);
$meituanServer = \BaAGee\Config\Config::get('meituan/server');
$accessKey = \BaAGee\Config\Config::get('meituan/access_key');
var_dump($meituanServer, $accessKey);
//使用内置的yaml配置解析 需要安装yaml扩展
\BaAGee\Config\Config::init(__DIR__ . '/config', \BaAGee\Config\Parser\YamlParser::class);
// 从config目录开始 service文件夹下meituan.yaml配置文件下的server的值
$meituanServer = \BaAGee\Config\Config::get('service/meituan/server');
$accessKey = \BaAGee\Config\Config::get('service/meituan/access_key');
var_dump($meituanServer, $accessKey);
//继承 \BaAGee\Config\Base\ParseConfigAbstract
class ParseKVFile extends \BaAGee\Config\Base\ParseConfigAbstract
{
// 声明配置文件后缀
protected static $configSuffix = 'kv';
// 具体的解析方法,返回解析后的配置数组
public static function parse(string $configFile): array
{
if (is_file($configFile)) {
$content = file_get_contents($configFile);
$content_array = explode(PHP_EOL, $content);
$config = [];
foreach ($content_array as $item) {
list($key, $val) = explode('=>', $item);
$config[$key] = $val;
}
return $config;
}
}
}
use BaAGee\Config\Config;
it(__DIR__ . '/config', \BaAGee\Config\Parser\PhpParser::class);
// 设置配置文件缓存目录 加速读取
Config::fast(__DIR__);
$t1 = microtime(true);
for ($i = 0; $i < 100; $i++) {
$c = Config::get('service/ddd/asd/cc/ddd/dd');
}
$t2 = microtime(true);
var_dump($t2 - $t1);
ini
;memcache配置
host = 127.0.0.1
port = 9089
password = fsdt3ty4e
[other]
abc=pp0