您好,欢迎访问一九零五行业门户网

什么是TOML?PHP中怎么配置使用TOML

本篇文章带大家聊聊php中的toml配置,介绍一下在 php 中如何使用 toml 配置文件格式语言,希望对大家有所帮助!
toml 是一个配置格式化语言,特色是简洁易读。 全称为 tom's obvious, minimal language 其中的 tom 为创建者 —— tom preston-werner (译者注:github ceo)。
来自其 github  reopo,toml 的目的如下:
toml 是一门简洁易用的配置信息格式化语言,高可读性来自于其优雅的语法。 toml 为哈希表数据结构量身定制的,在各种编程语言里皆可以轻松地将 toml 解析为各自的数据结构。
toml 和 php 在一起各种语言的 toml 解析器可以  在其项目 wiki 中找到。
我们将利用 yosymfony/toml: 一个 php 的 toml 解析器 来尝试下 toml 语言,在你的 php 7.1+ 项目里使用 composer:
composer require yosymfony/toml
toml 的项目 readme 里有一个示例配置信息,我们可以试着用起来:
## this is a toml document.title = toml example[owner]name = tom preston-wernerdob = 1979-05-27t07:32:00-08:00 # first class dates[database]server = 192.168.1.1ports = [ 8001, 8001, 8002 ]connection_max = 5000enabled = true[servers]  # indentation (tabs and/or spaces) is allowed but not required  [servers.alpha]  ip = 10.0.0.1  dc = eqdc10  [servers.beta]  ip = 10.0.0.2  dc = eqdc10[clients]data = [ [gamma, delta], [1, 2] ]# line breaks are ok when inside arrayshosts = [  alpha,  omega]
下面是 php 的解析和输出:
<?phpuse yosymfony\toml\toml;require __dir__ . '/vendor/autoload.php';$data = toml::parsefile(__dir__.'/example.toml');var_dump($data);// outputphp index.phpstring(10) "1979-05-27"toml-demo|⇒ php index.phparray(5) { ["title"]=> string(12) "toml example" ["owner"]=> array(2) { ["name"]=> string(18) "tom preston-werner" ["dob"]=> object(datetime)#243 (3) { ["date"]=> string(26) "1979-05-27 07:32:00.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "-08:00" } } ["database"]=> array(4) { ["server"]=> string(11) "192.168.1.1" ["ports"]=> array(3) { [0]=> int(8001) [1]=> int(8001) [2]=> int(8002) } ["connection_max"]=> int(5000) ["enabled"]=> bool(true) } ["servers"]=> array(2) { ["alpha"]=> array(2) { ["ip"]=> string(8) "10.0.0.1" ["dc"]=> string(6) "eqdc10" } ["beta"]=> array(2) { ["ip"]=> string(8) "10.0.0.2" ["dc"]=> string(6) "eqdc10" } } ["clients"]=> array(2) { ["data"]=> array(2) { [0]=> array(2) { [0]=> string(5) "gamma" [1]=> string(5) "delta" } [1]=> array(2) { [0]=> int(1) [1]=> int(2) } } ["hosts"]=> array(2) { [0]=> string(5) "alpha" [1]=> string(5) "omega" } }}
配置信息示例接下来我们试着将 laravel 的配置信息 config/database.php 解析为 toml ,做个对比。
需要注意的是,这只是一个示范,laravel 的配置系统要比 toml 高级很多,这里这样做的目的只是想在我们熟悉的配置信息里去理解 toml:
[database] default = "mysql" migrations = "migrations" [database.connections.sqlite] driver = "sqlite" database = "path/to/database.sqlite" prefix = "" [database.connections.mysql] driver = "mysql" host = "127.0.0.1" port = "3306" database = "forge" username = "forge" password = "" unix_socket = "" charset = "utf8mb4" collation = "utf8mb4_unicode_ci" prefix = "" strict = true [database.redis] client = "predis" [database.redis.default] host = "127.0.0.1" password = "" port = 6379 database = 0
目前来讲,toml 并不允许 nil 和 null 值,这在一些使用 null 作为默认值的场景下会变得很不方便。
缩进是允许的,但是不强求,上面的文件使用以下写法也不会有问题:
[database]default = "mysql"migrations = "migrations"[database.connections.sqlite]driver = "sqlite"database = "path/to/database.sqlite"prefix = ""# ...
构建一个 toml 配置文件扩展包 yosymfony/toml 除了提供解析 toml 文件和字串外,还提供了一个 tomlbuilder 类,用来实时构建 toml 配置信息,接下来我们还是使用 laravel 的 config/services.php 来作为例子讲解:
<?phpuse yosymfony\toml\tomlbuilder;require __dir__.'/vendor/autoload.php';$builder = new tomlbuilder();$services = $builder ->addcomment('third party services') ->addcomment('mailgun') ->addtable('services.mailgun') ->addvalue('domain', 'mg.example.com') ->addvalue('secret', 'mailgun-secret') ->addcomment('stripe') ->addtable('services.stripe') ->addvalue('model', 'app\user') ->addvalue('key', 'stripe-key') ->addvalue('secret', 'stripe-secret');file_put_contents(__dir__.'/services.toml', $services->gettomlstring());
生成的内容如下:
#third party services#mailgun[services.mailgun]domain = "mg.example.com"secret = "mailgun-secret"#stripe[services.stripe]model = "app\\user"key = "stripe-key"secret = "stripe-secret"
日期toml 支持 rfc 3339 制定的日期格式:
# offset date-timeodt1 = 1979-05-27t07:32:00zodt2 = 1979-05-27t00:32:00-07:00odt3 = 1979-05-27t00:32:00.999999-07:00# space permitted per the rfc 3339 specodt4 = 1979-05-27 07:32:00z# local date-timeldt1 = 1979-05-27t07:32:00# local dateld1 = 1979-05-27# local timelt1 = 07:32:00lt2 = 00:32:00.999999
在此篇文章编写时,上面大部分的格式都出现了错误,除了下面这一行:
dob = 1979-05-27t07:32:00-08:00
php 解析器会将解析成功输出为 datetime 实例:
array(1) { ["dob"]=> object(datetime)#128 (3) { ["date"]=> string(26) "1979-05-27 07:32:00.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "-08:00" }}
阅读更多前往官方项目页了解更多信息 ——  github -- toml-lang/toml: tom's obvious, minimal language。
项目 wiki 里可以找到各种语言的解析器: toml-lang/toml wiki 。
英文原文地址:https://laravel-news.com/toml-configuration-in-php
推荐学习:《php视频教程》
以上就是什么是toml?php中怎么配置使用toml的详细内容。
其它类似信息

推荐信息