定制 simoneddy/config 二次开发

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

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

simoneddy/config

Composer 安装命令:

composer require simoneddy/config

包简介

basic read only config class with file/dir loader and dot notation

README 文档

README

Basic Config class with file loader.

Changes in 2.0.0

  • The ConfigLoader class has been renamed PathLoader
  • Using the PathLoader->load method no longer returns a Config object. Instead it now returns an array of values loaded from the filesystem.
  • Construction of Config objects removed from PathLoader.
  • Added a ConfigFactory class to handle construction when using static factory methods.
  • Added a MutableConfig class that extends Config with methods for setting and unsetting values.

Changes in 1.1

  • Can now load CSV files

Installation

This library can be installed with composer:

composer require simoneddy/config

Dependencies

Usage

A Config object can be created by either passing values to the class constructor or by using one of the various factory methods:

require 'vendor/autoload.php';

// Using the Config constructor. This method requires that all config values be supplied as the
// constructors $values argument
$config = new \Eddy\Config\Config($values = []);

// Using the MutableConfig constructor. This method allows config values be supplied as the
// constructors $values argument, but can be modified after construction.
$config = new \Eddy\Config\MutableConfig($values = []);

// Using the Config objects static factory method
$config = \Eddy\Config\Config::fromPath(__DIR__ . '/config');

// Using the MutableConfig objects static factory method
$config = \Eddy\Config\MutableConfig::fromPath(__DIR__ . '/config');

// Using the ConfigFactory directly
$config = \Eddy\Config\ConfigFactory::fromPath(__DIR__ . '/config', $isMutable = false);

// Using a new ConfigFactory to create an immutable Config object
$config = (new \Eddy\Config\ConfigFactory())->createImmutable(__DIR__ . '/config');

// Using a new ConfigFactory to create a mutable MutableConfig object
$config = (new \Eddy\Config\ConfigFactory())->createMutable(__DIR__ . '/config');

Internally, this factory method uses the PathLoader class to scan the provided path and load all supported filetypes into an array. This array is given to the ConfigFactory which constructs the neccessary config class.

Unsupported files will be ignored.

If the path is a directory the PathLoader will attempt to load values from every supported file the directory contains. This also applies to any subdirectories and their contents.

Files are loaded as key-value pairs, using the base filename (minus extension) as the key, while the file contents is the value.

For example:

// config directory contains the file 'test.php' which returns an array:
// ['isTest' => true]
$config = \Eddy\Config\Config::fromPath(__DIR__ . '/config');

// values can be retrieved with dot notation, where the filename is the parent
// key:
var_dump($config['test.isTrue']);

PLEASE NOTE YAML files will be ignored unless the symfony/yaml package is also installed.

You can also use the Config class constructor and provide your config values directly as the sole argument:

$config = new \Eddy\Config\Config([
    'someKey' => 'some value',
    'someMoreKeys' => [
        'anotherKey' => 'another value'
    ]
]);

Once the config object is created it can be accessed like an array, with dot notation to specify nested keys:

echo $config['someKey']; // 'some value'

// Using dot notation to access nested keys:
var_dump(isset($config['someMoreKeys.anotherKey'])); // true

Internally, array access methods use get($key) and has($key) methods. These methods can also be used directly:

echo $config->get('someKey'); // 'some value'

var_dump($config->has('someMoreKeys.anotherKey')); // true

The Eddy\Config\Config object is read only and values cannot be modified after the object is instantiated. For mutability use the Eddy\Confi\MutableConfig class, which adds set and remove methods, as well as properly implementing \ArrayAccess::offsetUnset and \ArrayAccess::offsetSet:

// MutableConfig can be modified after construction
$config = new \Eddy\Config\MutableConfig([
    'someKey' => 'some value',
    'someMoreKeys' => [
        'anotherKey' => 'another value'
    ]
]);

// Now offsetSet works
$config['anotherKey'] = 'another value';

// Which is the same as using the set method:
$config->set('andAnother', 'yet another value');
var_drump(isset($config['anotherKey'])); // true

// OffsetUnset works too!
unset($config['someKey']);

// Which is using the remove method:
$config->remove('andAnother');
var_dump($config->has('someKey')); // false
var_dump($config->has('andAnother')); // false

Of course, all setting and unsetting can utilise dot notation for nested values:

$config->set('parent.nested.nestedTwice.nestedThrice', 'nested value');

// returns an array: ['nested' => ['nestedTwice' => ['nestedThrice' => 'nested value']]]
var_dump($config->get('parent'));

unset($config['parent.nested.nestedTwice']);

// returns an array: ['nested' => []]
var_dump($config->get('parent'));

By design, the set method (and subsequently offsetSet) will merge nested key => value pairs where neccessary. If you want to completely overwrite the parent key you can use the overwrite method:

$config->set('parent.nested.nestedTwice', 'nested value');

// returns an array: ['nested' => ['nestedTwice' => 'nested value']]
var_dump($config->get('parent'));

$config->overwrite('parent.newThing', 'new value');

// returns ['newThing' => 'new value']
// 'nestedTwice' is no longer set as 'parent' has been overwritten
var_dump($config['parent']);

The config object contains a toArray method that returns all config values as an array. The returned array maintains config keys and structure.

Seriali(s/z)ing

The Config class implements both PHPs JsonSerializable interfaces and serializing magic methods to PHP 8 standards. No deprecation warnings here!

Supported Filetypes

The ConfigLoader supports the following filetypes:

  • PHP
  • JSON
  • CSV (parses as an array with the filename as key)
  • YAML (requires symfony/yaml as a peer dependency)

The given path can also be a directory containing supported files and subdirectories. The directory structure will be used to determine nesting.

simoneddy/config 适用场景与选型建议

simoneddy/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 simoneddy/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 simoneddy/config 我们能提供哪些服务?
定制开发 / 二次开发

基于 simoneddy/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-05