定制 nitier/config-loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nitier/config-loader

最新稳定版本:0.9.9

Composer 安装命令:

composer require nitier/config-loader

包简介

README 文档

README

A flexible configuration loader for PHP projects with support for multiple formats and easy extensibility.

📦 Installation

composer require nitier/config-loader

🌟 Features

  • Supports multiple formats: .php, .json, .env
  • Deep configuration merging
  • Easy integration of custom loaders
  • Automatic type conversion for .env files
  • PSR-4 compatible

🚀 Quick Start

Basic Usage

use Nitier\ConfigLoader\Config;

// Load a single configuration file  
$config = Config::load('/path/to/config.php');

// Load multiple files with overrides  
$config = Config::load([
    '/path/to/base.json',
    '/path/to/override.env'
]);

Example .env File

APP_DEBUG=true
DB_HOST=localhost
DB_PORT=3306

🔌 Available Loaders

Format Class Supported Extensions
PHP PhpLoader .php, .inc
JSON JsonLoader .json
ENV EnvLoader .env

🛠 Extending Functionality

Adding a Custom Loader

  1. Create a class implementing LoaderInterface:
namespace App\ConfigLoaders;
use Nitier\ConfigLoader\Interface\LoaderInterface;

class XmlLoader implements LoaderInterface
{
    public static function supports(string $extension): bool
    {
       return $extension === 'xml';
    }

    public static function load(string $path): array
    {
        // Your XML parsing implementation here
    }
}
  1. Register the loader:
Config::addLoader(\App\ConfigLoaders\XmlLoader::class);

⚙️ Configuration

Loader Priorities

Loaders are checked in the order they were registered. The most recently added loaders take priority.

Deep Merging

When loading multiple configuration files, arrays are merged recursively:

// base.php
['db' => ['host' => 'localhost', 'user'=> 'test']]

// override.php
['db' => ['user'=> 'prod', 'port' => 3306]]

// Result
['db' => ['host' => 'localhost', 'user'=> 'prod', 'port' => 3306]]

📜 License

MIT

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-03-24