italystrap/config

ItalyStrap 配置模块 - 一种简单而实用的面向对象的配置包

2.9.1 2024-07-30 17:39 UTC

README

Build status Latest Stable Version Total Downloads Latest Unstable Version License PHP from Packagist

ItalyStrap 配置模块 - 一种简单而实用的面向对象的配置包

目录

安装

使用此包的最佳方式是通过Composer

composer require italystrap/config

基本用法

此包旨在用于您的应用程序中,以管理配置设置。可以通过点符号、数组符号或单个键来访问值,您还可以设置、更新、删除并检查键是否存在。

让我们看一个简单的例子

use ItalyStrap\Config\Config;

$config = new Config([
    'key' => 'value',
    'key2' => 'value2',
    'key3' => [
        'key4' => 'value4',
    ],
]);

$value = $config->get('key');

// Output: 'value'

// You can access using dot notation
$value = $config->get('key3.key4');

// Output: 'value4'

$value = $config->get('key3.key5', 'mixed default value');

// Output: 'mixed default value'

// You can also access using array notation
$value = $config->get(['key3', 'key4']);

// Output: 'value4'

// It is possible to set a value
$isSet = $config->set('key5', 'new value');

// Output: true

$value = $config->get('key5');

// Output: 'new value'

// You can also set a value using array notation or dot notation
$isSet = $config->set(['key6', 'key7'], 'new value');
// or
$isSet = $config->set('key6.key7', 'new value');

// Output: true

$value = $config->get('key6.key7');
// or
$value = $config->get(['key6', 'key7']);

// Output: 'new value'

// You can delete a value using dot notation or array notation or a single key

$isDeleted = $config->delete('key6.key7');
// or
$isDeleted = $config->delete(['key6', 'key7']);
// or
$isDeleted = $config->delete('key6');

// Output: true

// You can check if a key exists using dot notation or array notation or a single key

$exists = $config->has('key6.key7');
// or
$exists = $config->has(['key6', 'key7']);
// or
$exists = $config->has('key6');

// Output: true if exists, false otherwise

// You can update a value using dot notation or array notation or a single key
// It works in the same way as the set method

$isUpdated = $config->update('key', 'updated value');
// or
$isUpdated = $config->update(['key6', 'key7'], 'updated value');
// or
$isUpdated = $config->update('key6.key7', 'updated value');

// Output: true

// This package also has multiple methods to work with arrays like PSR-6

$config->getMultiple(['key', 'key2', 'key3.key4'], 'default');
$config->setMultiple(['key', 'key2', 'key3.key4'], 'value');
$config->deleteMultiple(['key', 'key2', 'key3.key4']);

// You can merge iterables after the Config object is created

$config->merge([
    'key' => 'value',
    'key2' => 'value2',
    'key3' => [
        'key4' => 'value4',
    ],
]);

// You can also get all the configuration settings as an array

$all = $config->toArray();

// Output: ['key' => 'value', 'key2' => 'value2', 'key3' => ['key4' => 'value4']]

// You can use the object and pass it to a \json_encode

$json = \json_encode($config);

// Output: '{"key":"value","key2":"value2","key3":{"key4":"value4"}}'

高级用法

您可以在测试文件夹中查看更多高级示例。

弃用

以下方法将在下一个主要版本中删除的所有弃用方法的列表。

  • Config::push() => Config::set()
  • Config::add() => Config::set()
  • Config::remove() => Config::delete()
  • Config::all() => Config::toArray()
  • ConfigThemeMods::class

贡献

欢迎所有反馈/错误报告/拉取请求。

许可证

版权(c)2019 Enea Overclokk,ItalyStrap

此代码根据MIT许可证授权。

致谢

Config::class 的想法来自

关于数组表示法的想法

关于一些想法