rnr1721/le7-config

轻量级PHP框架,具有API、CLI、WEB控制器 - 配置类

1.1.0 2023-12-02 14:23 UTC

This package is auto-updated.

Last update: 2024-08-31 00:30:07 UTC


README

需求

  • PHP 8.1或更高版本。
  • Composer 2.0或更高版本。

它有哪些功能?

  • 读取PHP(数组)、INI和JSON文件作为配置
  • 将包含配置文件的文件夹加载为一个(它理解所有支持的格式中的配置)
  • 将配置参数作为对象属性获取
  • 将配置参数作为数组值获取
  • 通过路径获取配置参数
  • 严格控制参数类型,并在配置中不存在参数时获取默认值
  • 使用PSR SimpleCache接口进行缓存
  • 获取过滤后的参数(替换参数值的部分)
  • 您可以编写自己的适配器以用于配置类型
  • 通过路径注册动态运行时参数

安装

composer require rnr1721/le7-config

测试

composer test

它是如何工作的?

use Core\Config\ConfigFactoryGeneric;

    $data = [
        'myparam' => 2,
        'myparam2' => "string value",
        'myparam3' => [
            'myparam4' => false,
            'myparam5' => 44.33
        ]
    ];

    $factory = new ConfigFactoryGeneric();
    $config = $factory->fromArray($data);

    // Get params as object properties (null if empty)
    echo $config->myparam;
    echo $config->myparam3->myparam5;

    // Get params as array
    echo $config['myparam'];
    echo $config['myparam3']['myparam5'];

    // Get params by path (recommended way)
    echo $config->int('myparam',54); // 54 is default value if not exists in config
    echo $config->float('myparam3.myparam5',33.44); // 33.44 is default value if not exists in config
    echo $config->float('myparam3.myparam5'); // throw exception if value not exists in config
    var_dump($config->bool('myparam4/myparam4',true,'/'));
    echo $config->string('myparam2',"default value");

如何加载PHP数组、JSON或INI文件?

use Core\Config\ConfigFactoryGeneric;

    $filename = '/var/www/example.com/htdocs/config/config.php';

    $factory = new ConfigFactoryGeneric();
    $config = $factory->fromJsonFile($filename, 'My JSON config');

    // echo $config['myparam']...

如何在不同文件夹中加载文件夹?

此配置管理器可以从文件夹中加载配置,支持不同的格式。例如,您有一个文件夹,其中放置了JSON文件、INI文件和php数组文件

use Core\Config\ConfigFactoryGeneric;

    $folders = [
        '/var/www/example.com/htdocs/config',
        '/var/www/example.com/htdocs/config2'
    ];

    $factory = new ConfigFactoryGeneric();
    // $folders can be string - one folder or array
    // seconf parameter - is suffix between filename and extension i.e. dbConfig.ini or dbConfig.php in this case
    $config = $factory->harvest($folders, 'Config');

    // $config->string('myparam')

如何使用缓存?

简单注入PSR CacheInterface/

use Core\Config\ConfigFactoryGeneric;

    $filename = '/var/www/example.com/htdocs/config/config.php';

    // $cache is PSR Cacheinterface
    $factory = new ConfigFactoryGeneric($cache);
    
    // myconfig is cache key to store in cache
    $config = $factory->fromArrayFile($filename, 'My PHP config', 'myconfig');

    // $config->string('myparam')

过滤参数

use Core\Config\ConfigFactoryGeneric;

    $data = [
        'myparam' => 2,
        'myparam2' => "My site is {myvariable1}",
        'myparam3' => [
            'myparam4' => false,
            'myparam5' => 44.33
        ]
    ];

    $factory = new ConfigFactoryGeneric();
    $config = $factory->fromArray($data);

    $config->applyFilter('vyvariable1','https://example.com');

    // stringf will return "My site is https://example.com"
    echo $config->stringf('myparam2');

动态参数

您可以添加自己的参数(不允许重复)

use Core\Config\ConfigFactoryGeneric;

    $data = [
        'myparam' => 2,
        'myparam2' => "My site is {myvariable1}",
        'myparam3' => [
            'myparam4' => false,
            'myparam5' => 44.33
        ]
    ];

    $factory = new ConfigFactoryGeneric();
    $config = $factory->fromArray($data);

    // Add own parameter
    $config->registerParam('myparam3.testparam77',"test value");

    // Get this parameter
    $config->string("myparam3.testparam77");

您还可以将动态参数与过滤器组合

use Core\Config\ConfigFactoryGeneric;

    $data = [
        'myparam' => 2,
        'myparam2' => "My site is {myvariable1}",
        'myparam3' => [
            'myparam4' => false,
            'myparam5' => 44.33
        ]
    ];

    $factory = new ConfigFactoryGeneric();
    $config = $factory->fromArray($data);

    // Add own parameter 1
    $config->registerParam('myparam3.testdirhome',"/home/www",'homepath');

    // Add own parameter 2
    $config->registerParam('myparam3.testdirbase',"{homepath}/base");

    // Get this parameter (return /home/www/base)
    $config->stringf("myparam3.testdirbase");