PHP code example of hehex / hehep-hconfigure

1. Go to this page and download the library: Download hehex/hehep-hconfigure 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/ */

    

hehex / hehep-hconfigure example snippets


$config = [
    'onCache'=>true,// 是否开启缓存
    'cacheFile'=>'',// 缓存文件路径,
    'configFiles'=>[],// 默认配置文件集合
    'alias'=>[],// 别名集合
    // 默认支持的配置解析器
    'parserExts'=>['php', 'json', 'ini', 'xml', 'yaml'],
];



use hehe\core\hconfigure\Configure;

// 创建配置对象
$hconfig = new Configure();

// 设置配置文件
$hconfig->addFiles('user.php','user.json');

// 获取配置项
$username = $hconfig->get('name');
$hasNameStatus = $hconfig->has('name');

// 获取所有配置
$allConfig = $hconfig->getConfig();


use hehe\core\hconfigure\Configure;
// 创建配置对象
$hconfig = new Configure();
// 设置配置文件
$hconfig->addFiles('user.php','user.json');

// 直接获取对象属性
$hconfig->appName;

// 获取配置项name
$username = $hconfig->get('name');

// 获取配置项name,不存在返回默认值"admin"
$username = $hconfig->get('name','admin');

// 获取二级配置项,配置项为user.name,
$username = $hconfig->get('user.name');

// 判断配置项name是否存在
$hasNameStatus = $hconfig->has('name');


use hehe\core\hconfigure\Configure;

// 创建配置对象
$hconfig = new Configure();

// 给配置项"user",指定user.php 配置文件
$hconfig->addFile('user.php','user');

$hconfig->addFiles(['user.php','user'],'user.json');

// 给配置项"user.admin",指定admin.php 配置文件
$hconfig->addFiles(['admin.php','user.admin'],'user.json');

class IniParser
{
    public static  function parse(string $file):array
    {
        return parse_ini_string(file_get_contents($file), true);
    }
}

use hehe\core\hconfigure\Configure;

// 创建配置对象
$hconfig = new Configure();
$hconfig->addParser('ini',IniParser::class);
$hconfig->addParser('ini',[IniParser::class,'parse']);

// 设置闭包解析器
$hconfig->addParser('ini',function($file){
    return parse_ini_string(file_get_contents($file), true);
});

$allConfig = $hconfig->addFiles('user.ini')->load()->getConfig();


use hehe\core\hconfigure\Configure;

// 创建配置对象
$hconfig = new Configure([
    'alias'=>[
        'appPath'=>'/www/apps/admin',
        'appConfig'=>'@appPath@/config'
    ],
    
    'file'=>'@appPath@/config.php'
]);

// 设置appPath别名
$hconfig->setAlias('appPath','/www/apps/admin');

//获取别名
$hconfig->getAlias('@appPath');

$file = $hconfig->get('file');
// $file = /www/apps/admin/config.php



use hehe\core\hconfigure\Configure;

// 创建配置对象
$hconfig = new Configure();

// 设置缓存文件路径
$hconfig->setCacheFile('cache/config.php');

$allConfig = $hconfig->addFiles('user.ini')->load()->getConfig();


use hehe\core\hconfigure\Configure;
// 创建配置对象
$hconfig = new Configure();

// 设置缓存文件路径
$hconfig->setCacheFile('cache/config.php');

// 添加缓存文件,用于检测缓存是否有效
$hconfig->addCheckFile('adminuser.php');

$allConfig = $hconfig->addFiles('user.ini')->load()->getConfig();



namespace hehe\core\base;

use hehe\core\hconfigure\Configure;

class Config extends Configure
{
    // 项目名
    public $appName = '';

    // 定义其他属性
    
    // 重写以下方法
    
    /**
     * 自动导入配置文件
     */
    protected function autoloadFiles():void
    {
        parent::autoloadFiles();

        // 导入配置目录下的所有配置文件
        
    }
    
    protected function parseConfig():void
    {
        
    }
    
}