mcustiel/php-simple-config

此包已被弃用,不再维护。未建议替代包。

一个简单库,用于管理不同格式的配置文件并将其缓存。

v2.0.0 2015-10-18 20:30 UTC

This package is auto-updated.

Last update: 2023-07-21 09:04:52 UTC


README

这是什么?

php-simple-config 是一个简单且可扩展的组件,允许开发者抽象应用配置管理。

php-simple-config 采用极简方法,支持不同类型的配置文件,当前支持的类型包括

  • PHP 文件(包含配置数组)。
  • INI 文件
  • JSON 文件
  • YAML 文件

该组件可以读取和写入这些配置格式。

此外,它还允许开发者将配置缓存起来,以提高访问时的性能,这通过 mcustiel/php-simple-cache 库实现。

安装

Composer

只需添加 Packagist 依赖项

    "require": {
	// ...
        "mcustiel/php-simple-config": ">=1.2.0"
    }	

或者,如果您想直接从 GitHub 获取,将以下内容添加到您的 composer.json 中应该足够了

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/mcustiel/php-simple-config"
        }
    ],
    "require": {
    	// ...
        "mcustiel/php-simple-config": "dev-master"
    }
}

或者直接下载代码。:D

如何使用?

读取配置

首先您需要创建一个配置文件,在这个例子中是一个 PHP 文件。如果格式是 PHP,您必须定义一个包含配置数组的变量并返回它,对于 JSON 或 INI,不需要任何特殊约定

<?php 
return array(
	'PRODUCTION' => array(
	    'DB' => array(
	        'user' => 'root',
	        'pass' => 'root',
	        'host' => 'localhost'
	    )
	),
	'STAGE' => array(
	    'DB' => array(
	        'user' => 'root',
	        'pass' => 'root',
	        'host' => 'localhost'
	    )
	),
	'LOCAL' => array(
	    'DB' => array(
	        'user' => 'root',
	        'pass' => 'root',
	        'host' => 'localhost'
	    )
	),
);

或者,如果您喜欢的话

<?php 
$config['PRODUCTION']['DB']['user'] = 'root';
$config['PRODUCTION']['DB']['pass'] = 'root';
$config['PRODUCTION']['DB']['host'] = 'localhost';
// ...
return $config;

然后您可以使用 Reader 对象从您的代码中访问配置

$reader = new Mcustiel\Config\Drivers\Reader\php\Reader();
$reader->read(__DIR__ . "/resources/test.php");
$config = $reader->getConfig();

或者使用库提供的 Loader 类

use Mcustiel\Config\Drivers\Reader\ini\Reader as IniReader;

$loader = new ConfigLoader("/test.ini", new IniReader());
$config = $loader->load();

访问配置

配置对象允许您访问配置文件中的信息

$config->getFullConfigAsArray(); // This will return the full configuration as an array.
$config->get('PRODUCTION'); // Will return a $config object to access the subkeys defined under "PRODUCTION"
$config->get('PRODUCTION')->get('DB')->get('user'); // Will return 'root'

缓存配置

php-simple-config 允许开发者创建配置的缓存版本以更快地打开和解析它。为此,您必须向 ConfigLoader 提供一个 CacheConfig 对象,如下面的代码块所示

use Mcustiel\Config\Drivers\Reader\ini\Reader as IniReader;
use Mcustiel\Config\CacheConfig;

use Mcustiel\SimpleCache\Drivers\memcache\Cache;

$cacheManager = new Cache();
$cacheManager->init();

$loader = new ConfigLoader(
    "/test.ini",
    new IniReader(),
    new CacheConfig($cacheManager, 'test.ini.cache', 3600000)
);

// If the file is already cached, then next sentence loads it from cache; otherwise it's loaded
// from original config file and then saved in the cached version.
$config = $loader->load();

CacheConfig 接收 \Mcustiel\SimpleCache\Interfaces\CacheInterface 的实例、要使用的密钥以及以毫秒为单位的有效期。

写入配置

要将配置写入文件,您需要一个 Writer 对象

$writer = new Mcustiel\Config\Drivers\Writer\ini\Writer($iniConfig);
$writer->write(__DIR__ . "/resources/test-written.ini");

注意 当使用 ini 或 yaml 格式写入时,库不能保证保留原始格式和项目顺序。但文件仍然可解析

关于注意的示例

原始 ini 文件

b = notAnArray
c = alsoNotAnArray
a.property = value
a.property.deeper = deeperValue

[PRODUCTION]
DB.user = root
DB.pass = root
DB.host = localhost
a.property.inside.production = test

[STAGE]
DB.user = root
DB.pass = root
DB.host = localhost

[LOCAL]
DB.user = root
DB.pass = root
DB.host = localhost

[TEST]
DB.user = root
DB.pass = root
DB.host = localhost

可能被转换为

b = notAnArray
c = alsoNotAnArray

[PRODUCTION]
DB.user = root
DB.pass = root
DB.host = localhost
a.property.inside.production = test

[STAGE]
DB.user = root
DB.pass = root
DB.host = localhost

[LOCAL]
DB.user = root
DB.pass = root
DB.host = localhost

[a]
property = value
property.deeper = deeperValue

[TEST]
DB.user = root
DB.pass = root
DB.host = localhost

示例

在单元和功能测试中,您可以查看php-simple-config的使用示例。