承接 hassankhan/config 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hassankhan/config

Composer 安装命令:

composer require hassankhan/config

包简介

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

README 文档

README

Latest version Software License Build Status Coverage Status Quality Score Total Downloads Gitter

Config is a file configuration loader that supports PHP, INI, XML, JSON, YML, Properties and serialized files and strings.

Requirements

Config requires PHP 7.4+.

IMPORTANT: If you want to use YAML files or strings, require the Symfony Yaml component in your composer.json.

Installation

The supported way of installing Config is via Composer.

$ composer require hassankhan/config

Usage

Config is designed to be very simple and straightforward to use. All you can do with it is load, get, and set.

Loading files

The Config object can be created via the factory method load(), or by direct instantiation:

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;

// Load a single file
$conf = Config::load('config.json');
$conf = new Config('config.json');

// Load values from multiple files
$conf = new Config(['config.json', 'config.xml']);

// Load all supported files in a directory
$conf = new Config(__DIR__ . '/config');

// Load values from optional files
$conf = new Config(['config.dist.json', '?config.json']);

// Load a file using specified parser
$conf = new Config('configuration.config', new Json);

Files are parsed and loaded depending on the file extension or specified parser. If the parser is specified, it will be used for all files. Note that when loading multiple files, entries with duplicate keys will take on the value from the last loaded file.

When loading a directory, the path is globed and files are loaded in by name alphabetically.

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code.

Loading string

Configuration from string can be created via the factory method load() or by direct instantiation, with argument $string set to true:

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Noodlehaus\Parser\Yaml;

$settingsJson = <<<FOOBAR
{
  "application": {
    "name": "configuration",
    "secret": "s3cr3t"
  },
  "host": "localhost",
  "port": 80,
  "servers": [
    "host1",
    "host2",
    "host3"
  ]
}
FOOBAR;

$settingsYaml = <<<FOOBAR
application:
    name: configuration
    secret: s3cr3t
host: localhost
port: 80
servers:
- host1
- host2
- host3

FOOBAR;

$conf = Config::load($settingsJson, new Json, true);
$conf = new Config($settingsYaml, new Yaml, true);

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code.

Getting values

Getting values can be done in three ways. One, by using the get() method:

// Get value using key
$debug = $conf->get('debug');

// Get value using nested key
$secret = $conf->get('security.secret');

// Get a value with a fallback
$ttl = $conf->get('app.timeout', 3000);

The second method, is by using it like an array:

// Get value using a simple key
$debug = $conf['debug'];

// Get value using a nested key
$secret = $conf['security.secret'];

// Get nested value like you would from a nested array
$secret = $conf['security']['secret'];

The third method, is by using the all() method:

// Get all values
$data = $conf->all();

Setting values

Although Config supports setting values via set() or, via the array syntax, any changes made this way are NOT reflected back to the source files. By design, if you need to make changes to your configuration files, you have to do it manually.

$conf = Config::load('config.json');

// Sample value from our config file
assert($conf['secret'] == '123');

// Update config value to something else
$conf['secret'] = '456';

// Reload the file
$conf = Config::load('config.json');

// Same value as before
assert($conf['secret'] == '123');

// This will fail
assert($conf['secret'] == '456');

Saving config

It is possible to save the config back to a file in any of the supported formats except PHP.

$config = Config::load('config.json');

$ini = $config->toString(new Ini()); // Encode to string if you want to save the file yourself

$config->toFile('config.yaml');
$config->toFile('config.txt', new Serialize()); // you can also force the writer

Using with default values

Sometimes in your own projects you may want to use Config for storing application settings, without needing file I/O. You can do this by extending the AbstractConfig class and populating the getDefaults() method:

class MyConfig extends AbstractConfig
{
    protected function getDefaults()
    {
        return [
            'host' => 'localhost',
            'port'    => 80,
            'servers' => [
                'host1',
                'host2',
                'host3'
            ],
            'application' => [
                'name'   => 'configuration',
                'secret' => 's3cr3t'
            ]
        ];
    }
}

Merging instances

You may want merging multiple Config instances:

$conf1 = Config::load('conf1.json');
$conf2 = Config::load('conf2.json');
$conf1->merge($conf2);

Examples of supported configuration files

Examples of simple, valid configuration files can be found here.

Performance

Please note that there is a significant drop in performance with configurations in the yaml format: Load configuration was 1ms in both json and php while the same configuration was 5ms with yaml.

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email contact@hassankhan.me instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

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

hassankhan/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.71M 次下载、GitHub Stars 达 976, 最近一次更新时间为 2015 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 13.71M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1026
  • 点击次数: 30
  • 依赖项目数: 215
  • 推荐数: 0

GitHub 信息

  • Stars: 976
  • Watchers: 33
  • Forks: 134
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-21