toml

TOML parser and serializer with tables, dotted keys, arrays and line-numbered errors.

v1.0.0Data formatsbundledimport "toml";
Overview Installation Documentation Examples

Overview

Practical, zero-dependency TOML parser and serializer for HolyPHP.

Supports tables ([section]), nested dotted keys (a.b.c = 1), array of tables ([[items]]), inline tables ({ x = 1, y = 2 }), basic/literal/multiline strings, integers (with underscores), floats (decimal & scientific), booleans, arrays (nested & multiline), comments, and clear line-numbered syntax errors.

bundled — works offline zero dependencies config parse

Quick start

import "toml";

$src = "title = \"My App\"\n"
    . "port = 8080\n"
    . "enabled = true\n"
    . "\n"
    . "[database]\n"
    . "host = \"localhost\"\n"
    . "ports = [5432, 5433]\n";

$data = toml_parse($src);
println($data["title"]);            // My App
println($data["database"]["host"]); // localhost

// Using the TomlDoc helper
$doc = TomlDoc::parse($src);
println($doc->get("database.host"));               // localhost
println($doc->get("database.user", "postgres"));   // postgres

$doc->set("database.pool.max", 16);
println($doc->toString());

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install toml

This adds the library to hphp.yaml, installs it into .hphp/packages/toml/ and pins the exact version in hphp.lock:

require:
  toml: "^1.0"

Use it

import "toml";

toml is bundled with the compiler, so import "toml"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install toml          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install toml
Packagetoml
Version1.0.0 (follows the compiler)
Entry filetoml.hphp
Sourcepackages/lib/toml ↗

Documentation

API Reference

Functions

Class TomlDoc

Error Handling

All parse and serialization errors throw Exception with a clear toml: prefix (and line N: for parser errors):

Examples

01Quick start

Bundled with the compiler — import "toml"; works offline in any file, or add it to a project with hphp install toml.

import "toml";

$src = "title = \"My App\"\n"
    . "port = 8080\n"
    . "enabled = true\n"
    . "\n"
    . "[database]\n"
    . "host = \"localhost\"\n"
    . "ports = [5432, 5433]\n";

$data = toml_parse($src);
println($data["title"]);            // My App
println($data["database"]["host"]); // localhost

// Using the TomlDoc helper
$doc = TomlDoc::parse($src);
println($doc->get("database.host"));               // localhost
println($doc->get("database.user", "postgres"));   // postgres

$doc->set("database.pool.max", 16);
println($doc->toString());
Try it
hphp install toml
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_toml.hphp.