microlib/config 问题修复 & 功能扩展

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

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

microlib/config

Composer 安装命令:

composer require microlib/config

包简介

A small, PHP, configuration library consisting mostly of functions.

README 文档

README

A small, PHP, configuration library consisting mostly of functions. Yep, functions, because working with configuration arrays should be simple and fast.

Features

  • Small library. No bloat. Easy-to-use functions.
  • Supports PHP array, JSON, INI, YAML, and XML config file formats.
  • Easy to apply defaults, enforce required settings, and filter out extraneous data.
  • Can specify a schema to recursively validate the data. Uses callables for validations and transformations.
  • Easily fetch values from nested config structure.

Usage

Validating Against a Schema

Let's say you have a config file that looks like this:

{
    "first_name": "Jeremy ",
    "last_name": "Lindblom",
    "phone": {"number": "5559997777"},
    "gender": "whocares"
}

In your PHP script…

Include the Config library.

require '/path/to/vendor/autoload.php';

use MicroLib\Config as cfg;

Load the config file.

$config = cfg\load('config.json');

Define a schema for the data. Using the schema function is optional but is useful for validating that the schema is defined correctly. (Note: The schema can be placed in a separate file and loaded with the load function like other config data.)

$schema = cfg\schema([
    'first_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'last_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'phone' => [
        'schema' => [
             'type' => [
                 'default'   => 'mobile',
                 'transform' => 'trim',
                 'validate'  => 'ctype_alpha',
             ],
             'number' => [
                 'required' => true,
                 'validate' => 'ctype_digit',
             ]
        ]
    ],
]);

Validate the configuration data with the schema. If no exception is thrown, then the data is valid.

$config = cfg\validate($config, $schema);

Fetch data from the config using get.

echo cfg\get($config, 'phone.number');
#> 5559997777

echo cfg\get($config, 'phone.type');
#> mobile

Simple Check for Required Keys

If you don't need the functionality of the schemas, there is still an easy way to enforce required settings and apply default values.

Let's say you have the following array of configuration data:

$config = [
    'class'  => 'rogue',
    'race'   => 'half-elf',
    'level'  => 5,
    'weapon' => 'short sword',
];

If you aren't concerned about all of the items, you can use the keep function to create a new array of only the items you want.

$config = cfg\keep($config, ['class', 'race', 'level']);

Then, using the create function, you can check requirements and apply defaults.

$config = cfg\create(
	$config,
	['class', 'race', 'level'],
	['status' => 'normal']
);

print_r($config);
#> Array
#> (
#>     [class] => rogue
#>     [race] => half-elf
#>     [level] => 5
#>     [status] => normal
#> )

统计信息

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

GitHub 信息

  • Stars: 15
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-09-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固