devly/config-loader

该包的最新版本(1.0.1)没有可用的许可证信息。

从多个来源加载配置到 `Devly\Repository` 对象中。

1.0.1 2024-02-01 15:59 UTC

This package is auto-updated.

Last update: 2024-09-30 17:41:58 UTC


README

将配置从多个来源加载到 Repository 对象中。支持的文件格式有:PHP、JSON、INI、XML、Yaml & Neon。

安装

使用 composer 安装

$ composer require devly/config-loader

用法

创建 Loader 对象

$safeMode = false // Set 'true' to skip exceptions

$loader = new \Devly\ConfigLoader\Loader($safeMode);

从文件加载配置

// Load config from a single file
$config = $loader->load('path/to/config.php');

// Load config from a list of files
$segment = true; // Segment configuration by file name

$files = [
    'path/to/config/app.php',
    'path/to/config/database.php',
];

$config = $loader->load($files, $segment);

var_dump($config->all());

// array(1) {
//  'app' => ...,
//  'database' => ...
// }

从目录加载配置文件

// Load config from a single file
$formats = ['php', 'json']; // load only php and json files. keep null to load all the files in the directory
$segment = true; // Segment configuration by file names
$config = $loader->load('path/to/config', $segment, $formats);

缓存配置

配置可以被缓存到文件、MySQL 数据库或自定义存储提供者。

文件缓存

将配置缓存到文件所需的是设置一个完整的缓存目录路径。

// When creating new Loader instance
$loader = new Loader($safeMode, 'full/path/to/cache');

// Or using the `setStorage()` method:
$loader->setStorage('full/path/to/cache');

MySQL 数据库缓存

$name = 'db_name';
$username = 'db_user';
$password = 'db_password';
$host = '172.0.0.1'; // Default 'localhost'

// Create instance of DatabaseStorage
$storage = new \Devly\ConfigLoader\Storages\DatabaseStorage($name, $username, $password, $host)

// Creating new Loader instance
$loader = new Loader($safeMode, $storage);

// Or using the `setStorage()` method:
$loader->setStorage($storage);

自定义缓存存储

class CustomStorage implements \Devly\ConfigLoader\Contracts\IStorage
{
    ...
}

$storage = new CustomStorage();

// Creating new Loader instance
$loader = new Loader($safeMode, $storage);

// Or using the `setStorage()` method:
$loader->setStorage($storage);