pear/config_lite

轻量级的用于ini风格配置/设置文本文件的包

v0.2.6 2017-07-04 10:28 UTC

This package is auto-updated.

Last update: 2024-09-05 23:23:13 UTC


README

Build Status

描述

这是一个简单、轻量且快速的类,用于ini风格的配置文件,底层使用PHP的`parse_ini_file`函数。

Config_Lite受到Python的ConfigParser的启发。

"Config_Lite"文件由全局键值对(KVP)条目和可选的节("[section]"),随后是"name = value"(KVP)条目组成。

安装

Pear

pear install Config_Lite-beta

Composer

创建一个composer.json文件并运行 composer install

{
    "repositories": [
        {
            "type": "pear",
            "url": "pear.php.net"
        }
    ],
    "require": {
        "pear-pear.php.net/config_lite": "*"
    }
}

Composer自动加载示例使用

<?php
require __DIR__ . '/../vendor/autoload.php';

$config = new Config_Lite;

示例

一个简单的配置文件:`test.ini'


public_key_file =  "~/.ssh/id_rsa.pub"
debug = yes

[general]
lang = "en"

[db]
user = "dionysis"
password = "c2oiVnY!f8sf"

读取配置文件

<?php

require_once 'Config/Lite.php';

$config = new Config_Lite('test.ini');

echo $config->get('db', 'user'); // dionysis
echo $config->get(null, 'public_key_file'); // ~/.ssh/id_rsa.pub

if (true === $config->getBool(null, 'debug', true)) {
	echo $config;
}

// read with ArrayAccess
echo $config['db']['password']; // c2oiVnY!f8sf

保存配置文件

<?php

require_once 'Config/Lite.php';

// write with file locking
$config = new Config_Lite('test.ini', LOCK_EX);

$config->set('db', 'user', 'JohnDoe')
	->set('db', 'password', 'd0g1tcVs$HgIn1');

// set with ArrayAccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';
$config['general'] = array('lang' => 'de');

// save object to file
$config->save();

创建配置文件

$config = new Config_Lite('test.ini');
$config->set('db', 'user', 'JohnDoe')
	->set('db', 'password', 'd0g1tcVs$HgIn1');

// set global bool 'debug' 
$config->set(null, 'debug', false);

// save object to file
try {
	$config->save();
} catch (Config_Lite_Exception $e) {
    echo "\n", 'Exception Message: ', $e->getMessage();
}

使用写入创建替代文件

<?php

require_once 'Config/Lite.php';

$filename = 'test.ini';

$config = new Config_Lite();

try {
	$config->write($filename, array(
			'public_key_file' =>  "~/.ssh/id_rsa.pub",
			'general' => array(
				'lang' => 'fr'
			),
			'db' => array(
				'user' => 'dionysis',
				'password' => 'd0g1tcVs$HgIn1'
			)
		)
	);
} catch (Config_Lite_Exception $exception) {
    printf("Failed to write file: %s.\n", $filename);
    printf("Exception Message: %s\n", $exception->getMessage());
    printf("Exception Stracktrace: %s\n", $exception->getTraceAsString());
}

无文件配置 - 流、过滤器或stdout

<?php

require_once 'Config/Lite.php';

$config = new Config_Lite();

$filename = sprintf(
    "php://filter/write=string.rot13/resource=%s", "test.ini"
);

$config->write($filename, array(
	    'public_key_file' =>  "~/.ssh/id_rsa.pub",
	    'general' => array(
	    'lang' => 'fr'
	),
	'db' => array(
		'user' => 'dionysis',
		'password' => 'd0g1tcVs$HgIn1'
		)
	)
);

// Writing to stdout
$config->write("php://stdout", array(
	    'public_key_file' =>  "~/.ssh/id_rsa.pub",
	    'general' => array(
	    'lang' => 'fr'
	),
	'db' => array(
		'user' => 'dionysis',
		'password' => 'd0g1tcVs$HgIn1'
		)
	)
);

全局配置选项(没有节)

$config->set(null, 'private_key_file', '~/.ssh/id_rsa');
// set with arrayaccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';

$config->sync();

echo $config->get(null, 'public_key_file');
// get with arrayaccess
echo $config['private_key_file'];

实现了IteratorAggregate,允许使用foreach迭代对象

$config = new Config_Lite($filename);

foreach ($config as $section => $name) {
	if (is_array($name)) {
		$s .= sprintf("[%s]\n", $section);
		foreach ($name as $key => $val) {
			$s .= sprintf("\t%s = %s\n", $key, $val);
		}
	} else {
		$s .= sprintf("%s=%s\n", $section, $name);
	}
}

echo $s;

选项

setProcessSections(bool)

设置是否处理节。如果为true,每个节的值将放入该节对应的子数组中。如果为false,所有值都将放在全局作用域中。

setQuoteStrings(bool)

设置是否双引号。如果为true,除了bool和数字值之外的所有内容都将被双引号包围。

注意与限制

  • Config_Lite是parse_ini_file和编写ini文件的OO前端,但如果你只想将数组作为ini文件写入,也可以使用公共方法write
  • 使用getString和setString保存和读取带有双引号和单引号的字符串
  • 如果你需要真正的bool类型,例如用于严格相等比较,请使用getBool
  • setget方法保持值不变,但write方法将"bool"值规范化为人类可读的表示形式,双引号字符串和没有引号的数字值
  • 换行符默认为"\n",可以使用`setLinebreak`进行编辑
  • 在读取后写入时删除注释
  • 在双引号值中的反斜杠将被解析,要避免这种情况,请使用$config->read('/test.cfg', INI_SCANNER_RAW);
  • 不支持注释和多行字符串,因为使用parse_ini_file读取时不受支持

如果你想保存用户输入,如图像,我建议使用get配合base64_decodeset配合base64_encode。对于正则表达式,也可以使用setSingleTickDelimiter()

保存正则表达式(作为全局选项)为base64编码

<?php

require_once 'Config/Lite.php';

$config = new Config_Lite('regex-test.ini');

$regex = '/Hello \"(.*?)\"/';
$config->set(null, 'regex', base64_encode($regex));
// save object, here sync to read it back, just to test
$config->sync();
// in 'regex-test.ini': regex = "L0hlbGxvIFwiKC4qPylcIi8="
$regex = base64_decode($config->get(null, 'regex'));
if (preg_match($regex, 'Hello "World"!')) {
    printf("matched. regex:%s", $regex);
} else {
    printf("no match found. regex:%s", $regex);
}

想法

致谢

贡献

欢迎贡献补丁!

为您的分支创建一个带有链接的问题。

https://github.com/pce/config_lite

或在此处报告错误:http://pear.php.net/package/Config_Lite