locomotivemtl/charcoal-config 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

locomotivemtl/charcoal-config

Composer 安装命令:

composer require locomotivemtl/charcoal-config

包简介

Charcoal component for configuration data and object modeling

README 文档

README

License Latest Stable Version Code Quality Coverage Status SensioLabs Insight Build Status

A Charcoal component for organizing configuration data and designing object data models.

This component is the glue for much of the Charcoal framework.

Table of Contents

Installation

The preferred (and only supported) method is with Composer:

$ composer require locomotivemtl/charcoal-config

Requirements

PSR

Entity & Config

The Config component simplifies access to object data. It provides a property-based user interface for retrieving and storing arbitrary data within application code. Data is organized into two primary object types: Entity and Config.

Entity

Entities represent simple data-object containers designed as a flexible foundation for domain model objects.
Examples: a single result from a repository or serve as the basis for each component of an MVC system.

Config

Configs are advanced Entities designed for runtime configuration values with support for loading files and storing hierarchical data.
Examples: application preferences, service options, and factory settings.

  • Class: Charcoal\Config\AbstractConfig
    • IteratorAggregate
    • Psr\Container\ContainerInterface
  • Methods: defaults, merge, addFile
  • Interface: Charcoal\Config\ConfigInterface
    • Charcoal\Config\EntityInterface
    • Charcoal\Config\FileAwareInterface
    • Charcoal\Config\SeparatorAwareInterface
    • Charcoal\Config\DelegatesAwareInterface

Features

File Loader

The Config container currently supports four file formats: INI, JSON, PHP, and YAML.

A configuration file can be imported into a Config object via the addFile($path) method, or by direct instantiation:

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config('config.json');
$cfg->addFile('config.yml');

The file's extension will be used to determine how to import the file. The file will be parsed and, if its an array, will be merged into the container.

If you want to load a configuration file without adding its content to the Config, use loadFile($path) instead. The file will be parsed and returned regardless if its an array.

$data = $cfg->loadFile('config.php');

Check out the documentation and examples for more information.

Key Separator Lookup

It is possible to lookup, retrieve, assign, or merge values in multi-dimensional arrays using key separators.

In Config objects, the default separator is the period character (.). The token can be retrieved with the separator() method and customized using the setSeparator() method.

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config();
$cfg->setSeparator('/');
$cfg->setData([
    'database' => [
        'params' => [
            'name' => 'mydb',
            'user' => 'myname',
            'pass' => 'secret',
        ]
    ]
]);

echo $cfg['database/params/name']; // "mydb"

Check out the documentation for more information.

Delegates Lookup

Delegates allow several objects to share values and act as fallbacks when the current object cannot resolve a given data key.

In Config objects, delegate objects are regsitered to an internal stack. If a data key cannot be resolved, the Config iterates over each delegate in the stack and stops on the first match containing a value that is not NULL.

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config([
    'driver' => null,
    'host'   => 'localhost',
]);
$delegate = new Config([
    'driver' => 'pdo_mysql',
    'host'   => 'example.com',
    'port'   => 11211,
]);

$cfg->addDelegate($delegate);

echo $cfg['driver']; // "pdo_mysql"
echo $cfg['host']; // "localhost"
echo $cfg['port']; // 11211

Check out the documentation for more information.

Array Access

The Entity object implements the ArrayAccess interface and therefore can be used with array style:

$cfg = new \Charcoal\Config\GenericConfig();

// Assigns a value to "foobar"
$cfg['foobar'] = 42;

// Returns 42
echo $cfg['foobar'];

// Returns TRUE
isset($cfg['foobar']);

// Returns FALSE
isset($cfg['xyzzy']);

// Invalidates the "foobar" key
unset($cfg['foobar']);

👉 A data key MUST be a string otherwise InvalidArgumentException is thrown.

Interoperability

The Config object implements PSR-11: Psr\Container\ContainerInterface.

This interface exposes two methods: get() and has(). These methods are implemented by the Entity object as aliases of ArrayAccess::offsetGet() and ArrayAccess::offsetExists().

$config = new \Charcoal\Config\GenericConfig([
    'foobar' => 42
]);

// Returns 42
$config->get('foobar');

// Returns TRUE
$config->has('foobar');

// Returns FALSE
$config->has('xyzzy');

👉 A call to the get() method with a non-existing key DOES NOT throw an exception.

Configurable Objects

Also provided in this package is a Configurable mixin:

  • Charcoal\Config\ConfigrableInterface
  • Charcoal\Config\ConfigurableTrait

Configurable objects (which could have been called "Config Aware") can have an associated Config object that can help define various properties, states, or other.

The Config object can be assigned with setConfig() and retrieved with config().

An added benefit of ConfigurableTrait is the createConfig($data) method which is used to create a Config object if one is not assigned. This method can be overridden in sub-classes to customize the instance returned and whatever initial state might be needed.

Check out the documentation for examples and more information.

Development

To install the development environment:

$ composer install

To run the scripts (phplint, phpcs, and phpunit):

$ composer test

API Documentation

Development Dependencies

Coding Style

The charcoal-config module follows the Charcoal coding-style:

Coding style validation / enforcement can be performed with composer phpcs. An auto-fixer is also available with composer phpcbf.

This module should also throw no error when running phpstan analyse -l7 src/ 👍.

Credits

License

Charcoal is licensed under the MIT license. See LICENSE for details.

locomotivemtl/charcoal-config 适用场景与选型建议

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

它主要适用于以下技术方向: 「yaml」 「json」 「xml」 「configuration」 「ini」 「yml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-25