emargareten/config-php

PHP的简单配置管理器

v0.0.1 2023-04-18 09:04 UTC

This package is auto-updated.

Last update: 2024-09-18 12:15:53 UTC


README

Latest Version on Packagist Software License Tests Total Downloads

此包简化了在应用程序中管理配置设置的过程。它提供了一种简单方便的方式来设置、获取和操作配置值。

需求

此包需要PHP 8.0或更高版本。

安装

您可以通过composer安装此包

composer require emargareten/config-php

用法

设置配置文件路径

在您可以使用此包之前,您必须创建一个配置文件并设置其路径。配置文件应返回一个包含配置值的数组

<?php

return [
    'key' => 'value',
    'another_key' => 'another_value',
];

现在在您的应用程序引导文件等中设置配置文件的路径。

Config::setPath('/path/to/config.php');

或者,您可以在首次实例化Config类时传递路径作为参数

$config = new Config('/path/to/config.php');

如果您不想使用配置文件,您可以直接使用setValues方法来设置配置值

Config::setValues([
    'key' => 'value',
    'another_key' => 'another_value',
]);

使用配置

Config值是静态的,因此您可以在应用程序的任何位置访问它们。

您可以使用以下方法来获取、设置和操作配置值:(以静态方式或实例化Config类的实例调用这些方法)

// Get a value from your configuration
$value = Config::get('key');

// Get a value with a default value if key is not found
$value = Config::get('key', 'default');

// Set a value in your configuration
Config::set('key', 'value');

// Remove a value from your configuration
Config::forget('key');

// Remove all values from your configuration
Config::clear();

// Set multiple values in your configuration
Config::setMany([
    'key1' => 'value1',
    'key2' => 'value2',
]);

// Reset your configuration to its initial state (rereads the config file)
Config::reset();

// Check if a key exists in your configuration
if (Config::has('key')) {
    // ...
}

// Get all values from your configuration
$values = Config::all();

您还可以使用config辅助函数来访问Config

config()->get('key'); // or config('key')

config()->set('key', 'value');

// ...

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

composer test

贡献

欢迎贡献!如果您发现任何错误或问题或有一个功能请求,请打开一个新问题或提交一个拉取请求。在贡献之前,请确保您已阅读贡献指南

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件