bhargav2785/iniparser

本包最新版本(dev-master)的许可信息不可用。

PHP ini文件解析器。

dev-master 2014-06-06 05:56 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:04:35 UTC


README

Build Status

IniParser

IniParser是一个库,允许用户解析ini文件,并将结果输出为有意义的数组语法。IniFetcher是IniParser的包装器,它执行两个操作;调用IniParser解析ini文件,并提供getter方法轻松访问ini属性。

使用示例

您可以使用以下两种方式通过此库访问ini文件值。

使用IniParser类

$parser = new IniParser($fileJson);
$parser->setFormat($parser::OUTPUT_FORMAT_ARRAY);
$data = $parser->parse();
echo "Using IniParser: " . $data['json']['list']['creditcards']['amex']['prefix'] . '<br/>';

使用IniFetcher(推荐)

$fetcher = IniFetcher::getInstance($fileJson);
echo "Using IniFetcher: " . $fetcher::get('json.list.creditcards.amex.prefix');

支持

IniParser支持ini文件中的各种语法。下面是支持的语法。

继承

在您的ini文件中,您可以使用一个部分继承其他部分属性。该语法为[子部分 : 父部分]。下面是ini文件中继承的示例。

; For inheritance to work in ini files parent must be defined
; as a section before its child is defined. In other words parent
; needs to go on top and child needs to be at bottom.

; default set of properties
[common]
section = common
type = parent
site.url = http://example.com

; prod extends common and overrides same property found in common
[prod : common]
section = prod
type = child
site.url = http://prod.example.com

; test extends common and overrides same property found in common
[test : common]
section = test
type = child
site.url = http://test.example.com

; dev extends common and overrides same property found in common
[dev : common]
section = dev
type = child
site.url = http://dev.example.com

; 'bhargav' extends dev and overrides same property found in dev
[bhargav : dev]
section = bhargav

; lazydev is an alias of dev since it doesn't contain any self property
[lazydev : dev]

父部分中的每个属性都将扩展到子部分。如果键名相似,子部分键将覆盖父部分键。请注意,父部分需要先于子部分声明,因为我们无法控制ini文件部分的顺序。另外,不支持多重继承。

全局属性

如果键值对没有定义任何部分名称,它将被视为全局部分。这在您不确定将条目放在何处时特别有用。

system.section = global
system.includePath.public = "public/global"
system.includePath.components = "components/"
system.phpSettings.display_errors = 0
system.phpSettings.display_warning = 1
site.url = http://bhargavvadher.com/about
[section]
key1 = parentValue1
key3 = parentValue3

数组字面量

IniParser支持数组字面量。这意味着您可以为键传递数组字面量值。这在您有多个可能的键值时特别有用,例如,所有管理员用户system.admins = [user1,user2,user3]。如果您使用数组字面量进行继承,子类将具有比父类更高的优先级,如以下示例所示。

; example of a array like literal's ini file

; some global just for fun
site.url.primary = 'http://bhargavvadher.com'
site.url.secondary = 'http://bhargav.me'

[array]
system.users = [user1,user2,user3]
system.section = [prod]
system.admins = ['user1',user2,1234,12.34]

[empty1]
[empty2 : empty1]
[empty3]

请注意,ini文件中的所有空部分都将被忽略。在上面的示例中,所有empty1empty2empty3都将被忽略,因为empty1empty3实际上都是空的,而empty2是继承自empty1的,但继承后的部分又是空的。

注释

注释不会被解析,但它是ini文件的重要组成部分。它有助于正确记录ini文件供未来使用。IniParser提供了各种注释样式,如下所示。请注意,这些注释行都不会被解析为值。

; prod only properties
[prod]
system.section = prod

; test only properties
[test : prod]
system.section = test

; dev only properties
[dev : test]
system.section = dev

; # tab comment, php will ignore this
#   space comment
#        tab-space comment
###   multiple hash comment
#   comment starting and ending with hash #

############################################################################
################   @author Bhargav Vadher           ########################
################   @site http://bhargavvadher.com   ########################
############################################################################

所有以;#开头的行都将被视为注释,并且不会被解析。解析的第一遍后,ini文件将看起来像下面这样。请注意,空行也将被忽略。

[prod]
system.section = prod
[test : prod]
system.section = test
[dev : test]
system.section = dev

Json字符串

这在您有一个希望用于配置系统的值json结构时非常有用。例如,您有一个作业,在每次部署时从数据库中获取配置字段,然后将其创建为json。现在您可以直接将那个json blob放入ini文件中,IniParser将把它分解成多维数组结构。请参阅以下示例以获取更多详细信息。

# global json property section
[json]
list = '{
	"colors" : [
		{
			"colorName" : "red",
			"hexValue" : "#f00"
		},
		{
			"colorName" : "green",
			"hexValue" : "#0f0"
		},
		{
			"colorName" : "blue",
			"hexValue" : "#00f"
		}
	],
	"creditcards" : {
		"amex" : {"name": "American Express","prefix": "34","length": 15},
		"bankcard" : {"name": "Bankcard","prefix": "5610","length": 16},
		"chinaunion" : {"name": "China UnionPay","prefix": "62","length": 16},
		"dccarte" : {"name": "Diners Club Carte Blanche","prefix": "300","length": 14},
		"dcenroute" : {"name": "Diners Club enRoute","prefix": "2014","length": 15},
		"dcintl" : {"name": "Diners Club International","prefix": "36","length": 14},
		"dcusc" : {"name": "Diners Club United States & Canada","prefix": "54","length": 16},
		"discover" : {"name": "Discover Card","prefix": "6011","length": 16},
		"instapay" : {"name": "Insta Payment","prefix": "637","length": 16},
		"jcb" : {"name": "JCB","prefix": "3528","length": 16},
		"laser" : {"name": "Laser","prefix": "6304","length": 16},
		"maestro" : {"name": "Maestro","prefix": "5018","length": 16},
		"mc" : {"name": "Mastercard","prefix": "51","length": 16},
		"solo" : {"name": "Solo","prefix": "6334","length": 16},
		"switch" : {"name": "Switch","prefix": "4903","length": 16},
		"visa" : {"name": "Visa","prefix": "4","length": 16},
		"electron" : {"name": "Visa Electron","prefix": "4026","length": 16}
	}
}'

# regular ini property section
[people]
name.first = Bhargav
name.last = Vadher