dkikki/config

以 YAML、JSON、INI 或 PHP 数组格式编写和解析配置文件

2.0.1 2024-09-24 07:39 UTC

This package is auto-updated.

Last update: 2024-09-24 07:40:36 UTC


README

声明:本页面的文档是由 Claude AI 编写的.

Dikki Config 是一个 PHP 库,允许您创建和管理 PHP 应用程序的配置文件。它支持多种文件格式,包括 PHP 数组、.env 文件、JSON、YAML、INI 和 Neon。此库提供了一个统一的接口来访问配置值,无论底层文件格式如何。

支持的解析器

  • PHP 数组 (Dikki\Config\PhpArrayParser)
  • JSON (Dikki\Config\JsonParser)
  • YAML (Dikki\Config\YamlParser)
  • INI (Dikki\Config\IniParser)
  • Neon (Dikki\Config\NeonParser)
  • DotEnv (Dikki\Config\DotEnvParser)

安装

要安装 Dikki Config,请使用 Composer

composer require dikki/config

用法

多种配置文件类型

如果您正在使用多种类型的配置文件,您可以使用 ConfigFetcher 类从所有支持的文件类型中检索和合并配置。

使用 ConfigFetcher 类的示例

use Dikki\Config\ConfigFetcher;

$configFetcher = new ConfigFetcher(__DIR__ . '/config');

// Fetch all configurations
$config = $configFetcher->fetch();

// Get a specific configuration value using dot notation
echo $configFetcher->get('app.timezone');

单个配置文件类型

如果您只使用一种类型的配置文件,您可以使用带有相应解析器的 Config 类。

创建配置文件或文件夹

首先,请确保您项目中有一个配置文件夹。将所有配置文件添加到此文件夹中。

示例

use Dikki\Config\Config;
use Dikki\Config\YamlParser;

// Create an instance of the Config class
$config = new Config(new YamlParser(__DIR__ . '/config')); # pass either single file path or whole directory

// Get a configuration value (dot notation is supported)
echo $config->get('app.timezone');

输出: UTC

更多信息

ConfigFetcher 类

ConfigFetcher 类旨在在给定目录中递归搜索并解析所有支持的配置文件格式。

构造函数

public function __construct(string $path)
  • path:包含配置文件的目录路径。

方法

  • fetch():检索并解析所有支持的配置文件。
  • getFiles(string $dir):从给定目录递归获取所有文件。
  • mergeConfigs(array $baseConfig, array $newConfig):合并两个配置数组,保留嵌套键。
  • get(string $key, mixed $default = null):使用点表示法通过键获取特定的配置值。

Config 类

Config 类接收一个实现 ConfigInterface 的类的实例,并将 get() 方法委托给它。

构造函数

public function __construct(ConfigInterface $parser)
  • parser:实现 ConfigInterface 的类的实例。

方法

  • get(string $key, mixed $default = null):使用点表示法从配置数组中获取值。
  • getAll():获取所有配置值。
  • parse():解析配置文件(们)并返回一个数组。

解析器类

每个解析器类都实现了 ConfigInterface,并提供了解析特定文件格式的方法。

常用方法

  • parse():解析配置文件(们)并返回一个数组。
  • get(string $key, mixed $default = null):使用点表示法从配置数组中获取值。

DotEnvParser

解析 .env 文件。

IniParser

解析 INI 文件。

JsonParser

解析 JSON 文件。

NeonParser

解析 Neon 文件。

PhpArrayParser

解析 PHP 数组文件。

YamlParser

解析 YAML 文件。

测试

要测试 Dikki Config 库的功能,您可以使用使用 Nette Tester 编码提供的测试用例。

use Dikki\Config\Config;
use Dikki\Config\ConfigFetcher;
use Dikki\Config\DotEnvParser;
use Dikki\Config\IniParser;
use Dikki\Config\JsonParser;
use Dikki\Config\NeonParser;
use Dikki\Config\PhpArrayParser;
use Dikki\Config\YamlParser;
use Tester\Assert;

require __DIR__ . '/vendor/autoload.php';

Tester\Environment::setup();

$configDir = __DIR__ . '/config';

$configFetcher = new ConfigFetcher($configDir);
$dotenvConfig = new Config(new DotEnvParser($configDir . '/.env'));
$iniConfig = new Config(new IniParser($configDir . '/iniconfig.ini'));
$jsonConfig = new Config(new JsonParser($configDir . '/jsonconfig.json'));
$neonConfig = new Config(new NeonParser($configDir . '/neonconfig.neon'));
$phpConfig = new Config(new PhpArrayParser($configDir . '/phpconfig.php'));
$yamlConfig = new Config(new YamlParser($configDir . '/yamlconfig.yaml'));

// Test individual parsers
Assert::equal($dotenvConfig->get('APP_NAME'), "Sample App");
Assert::equal($iniConfig->get('app.url'), "https://example.com");
Assert::equal($jsonConfig->get('app.author'), "Naman");
Assert::equal($neonConfig->get('app.debug'), false);
Assert::equal($phpConfig->get('app.timezone'), "Asia/Kolkata");
Assert::equal($yamlConfig->get('app.theme'), "Default");

// Test ConfigFetcher
Assert::equal($configFetcher->get('APP_NAME'), "Sample App");
Assert::equal($configFetcher->get('app.author'), "Naman");
Assert::equal($configFetcher->get('app.url'), "https://example.com");
Assert::equal($configFetcher->get('app.debug'), false);
Assert::equal($configFetcher->get('app.timezone'), "Asia/Kolkata");
Assert::equal($configFetcher->get('app.theme'), "Default");

许可证

本项目受 MIT 许可证的许可 - 请参阅 LICENSE 文件以获取详细信息。