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

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

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

phlak/config

Composer 安装命令:

composer require phlak/config

包简介

Config loading and management

README 文档

README

Config

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License

PHP library for simple configuration management -- Created by Chris Kankiewicz (@PHLAK.dev)

Introduction

Config is a simple PHP configuration management library supporting multiple configuration file formats and an expressive API.

Supported file formats:

  • PHP
  • INI
  • JSON
  • TOML
  • YAML
  • XML

Requirements

Install with Composer

composer require phlak/config

Initializing the Client

First, import Config:

use PHLAK\Config\Config;

Then instantiate the class:

$config = new Config($context, $prefix = null);

Where $context is one of the following:

  • A path to a supported file type
  • A directory path containing one or more supported file types
  • An array of configuration options

And $prefix is a string to be used as a key prefix for options of this Config object.

Configuration File Formats

PHP

A PHP configuration file must have the .php file extension, be a valid PHP file and and return a valid PHP array.

Example PHP file
<?php

return [
    'driver' => 'mysql',
    'drivers' => [
        'sqlite' => [
            'database' => 'database.sqlite',
            'prefix' => ''
        ],
        'mysql' => [
            'host' => 'localhost',
            'database' => 'blog',
            'username' => 'blogger',
            'password' => 'hunter2',
            'charset' => 'utf8',
            'prefix' => ''
        ]
    ]
];

INI

An INI configuration file must have the .ini file extension and be a valid INI file.

Example INI file
driver = mysql

[drivers]

sqlite[database] =  database.sqlite
sqlite[prefix] =

mysql[host] = localhost
mysql[database] = blog
mysql[username] = blogger
mysql[password] = hunter2
mysql[charset] = utf8
mysql[prefix] =

JSON

A JSON configuration file must have the .json file extension and contain a valid JSON object.

Example JSON file
{
    "driver": "mysql",
    "drivers": {
        "sqlite": {
            "database": "database.sqlite",
            "prefix": ""
        },
        "mysql": {
            "host": "localhost",
            "database": "blog",
            "username": "blogger",
            "password": "hunter2",
            "charset": "utf8",
            "prefix": ""
        }
    }
}

TOML

A TOML configuration file must have the .toml file extension and be a valid TOML file.

Example TOML file
driver = 'mysql'

[drivers.sqlite]
database = 'database.sqlite'
prefix = ''

[drivers.mysql]
host = 'localhost'
database = 'blog'
username = 'blogger'
password = 'hunter2'
charset = 'utf8'
prefix = ''

YAML

A YAML configuration file must have the .yaml or .yml file extension and be a valid YAML file.

Example YAML file
driver: mysql

drivers:

  sqlite:
    database: database.sqlite
    prefix:

  mysql:
    host: localhost
    database: blog
    username: blogger
    password: hunter2
    charset: utf8
    prefix:

XML

A XML configuration file must have the .xml file extension and contain valid XML.

Example XML file
<?xml version='1.0'?>

<database>
    <driver>mysql</driver>
    <drivers>
        <sqlite>
            <database>database.sqlite</database>
            <prefix></prefix>
        </sqlite>
        <mysql>
            <host>localhost</host>
            <database>blog</database>
            <username>blogger</username>
            <password>hunter2</password>
            <charset>utf8</charset>
            <prefix></prefix>
        </mysql>
    </drivers>
</database>

Usage

__construct

Create a new Config object.

Config::__construct( mixed $context [, string $prefix = null ] ) : Config
$context
Raw array of configuration options or path to a configuration file or directory containing one or more configuration files
$prefix
A key under which the loaded config will be nested

Examples

Create a new Config object from a YAML file.

$config = new Config('path/to/config.yaml');

Create a new Config object from a directory of config files.

$config = new Config('path/to/configs/');

Create a new Config object from an array.

$config = new Config([
    'hostname' => 'localhost',
    'port' => 12345
]);

set

Store a config value with a specified key.

Config::set( string $key, mixed $value ) : bool
$key
Unique configuration option key
$value
Config item value

Example

$config->set('hostname', 'localhost');
$config->set('port', 12345);

get

Retrieve a configuration option via a provided key.

Config::get( string $key [, mixed $default = null ] ) : mixed
$key
Unique configuration option key
$value
Config item value

Examples

// Return the hostname option value or null if not found.
$config->get('hostname');

Define a default value to return if the option is not set.

// Returns 'localhost' if hostname option is not set
$config->get('hostname', 'localhost');

has

Check for the existence of a configuration item.

Config::has( string $key ) : bool
$key
Unique configuration option key

Example

$config = new Config([
    'hostname' => 'localhost'
]);

$config->has('hostname'); // Returns true
$config->has('port');     // Returns false

append

Append a value onto an existing array configuration option.

Config::append( string $key, mixed $value ) : bool
$key
Unique configuration option key
$value
Config item value

Example

Append baz to the tags config item array.

$config->set('tags', ['foo', 'bar']);
$config->append('tags', 'baz'); // ['foo', 'bar', 'baz']

prepend

Prepend a value onto an existing array configuration option.

Config::prepend( string $key, mixed $value ) : bool
$key
Unique configuration option key
$value
Config item value

Example

Prepend baz to the tags config item array.

$config->set('tags', ['foo', 'bar'])
$config->prepend('tags', 'baz'); // ['baz', 'foo', 'bar']

unset

Unset a configuration option via a provided key.

Config::unset( string $key ) : bool
$key
Unique configuration option key

Example

$config->unset('hostname');

load

Load configuration options from a file or directory.

Config::load( string $path [, string $prefix = null [, bool $override = true ]] ) : self
$path
Path to configuration file or directory
$prefix
A key under which the loaded config will be nested
$override
Whether or not to override existing options with values from the loaded file

Examples

Load a single additional file.

$config->load('path/to/config.php');

Load an additional file with a prefix.

$config->load('database.php', 'database');

Load an additional file without overriding existing values.

$config->load('additional-options.php', null, false);

merge

Merge another Config object into this one.

Config::merge( ConfigInterface $config [, bool $override = true ] ) : self
$config
Instance of ConfigInterface
$override
Whether or not to override existing options with values from the merged config object

Examples

Merge $anotherConfig into $config and override values in $config with values from $anotherConfig.

$anotherConfig = new Config('some/config.php');

$config->merge($anotherConfig);

Merge $anotherConfig into $config without overriding any values. Duplicate values in $anotherConfig will be lost.

$anotherConfig = new Config('some/config.php');

$config->merge($anotherConfig, false);

split

Split a sub-array of configuration options into its own Config object.

Config::split( string $key ) : Config
$key
Unique configuration option key

Example

$config = new Config([
    'foo' => 'foo',
    'bar' => [
        'baz' => 'barbaz'
    ],
]);

$barConfig = $config->split('bar');

$barConfig->get('baz');  // Returns 'barbaz'

toArray

Return the entire configuration as an array.

Config::toArray( void ) : array

Example

$config = new Config(['foo' => 'foo']);

$config->toArray(); // Returns ['foo' => 'foo']

Troubleshooting

For general help and support join our GitHub Discussion or reach out on Bluesky.

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is licensed under the MIT License.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 330.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 59
  • 点击次数: 11
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 58
  • Watchers: 4
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-21