internal/toml
PHP 8.1+ TOML 1.0.0/1.1.0 parser and encoder. Parse TOML strings/files into PHP arrays or an AST, modify documents, and serialize back to TOML with round-trip support.
Installation:
composer require internal/toml
Add to composer.json under require:
"internal/toml": "^1.1"
First Use Case: Parse a TOML config file into a PHP array:
use Internal\Toml\Toml;
$tomlContent = file_get_contents('config/app.toml');
$config = Toml::parseToArray($tomlContent);
Access values like a standard array:
$databaseHost = $config['database']['host'];
Where to Look First:
Toml::parseToArray() (decoding) and Toml::encode() (encoding).Toml::parse() for advanced manipulation (e.g., preserving comments or format).config/ arrays with TOML files.// config/app.toml
[database]
connection = "mysql"
host = "127.0.0.1"
port = 3306
// In a Service Provider
$config = Toml::parseToArray(file_get_contents(base_path('config/app.toml')));
$this->app->singleton('config', fn() => $config);
config() helper by binding the parsed TOML to the container:
$this->app->bind('config', fn() => Toml::parseToArray(file_get_contents(config_path('app.toml'))));
$k8sConfig = [
'apiVersion' => 'v1',
'kind' => 'Pod',
'metadata' => ['name' => 'my-pod'],
'spec' => ['containers' => [['name' => 'nginx', 'image' => 'nginx:latest']]],
];
file_put_contents('pod.toml', (string) Toml::encode($k8sConfig));
Stringable return type to chain with file_put_contents() or HTTP responses.$document = Toml::parse(file_get_contents('config.toml'));
$data = $document->toArray();
$data['new_key'] = 'value';
file_put_contents('config.toml', (string) Toml::encode($data));
Document object) to inspect/manipulate nodes before encoding.// In a custom Artisan command
protected $tomlContent;
protected function handle(): void
{
$this->tomlContent = $this->argument('config');
$config = Toml::parseToArray($this->tomlContent);
// Process config...
}
--toml argument with getArgument() and validate with Toml::parse().$configCache = cache()->remember('toml_config', now()->addHours(1), fn() =>
Toml::parseToArray(file_get_contents(config_path('app.toml')))
);
config/local.toml, config/prod.toml).$env = app()->environment();
$config = Toml::parseToArray(file_get_contents("config/{$env}.toml"));
// config/features.toml
[flags]
new_dashboard = true
experimental_api = false
// In a service
$flags = Toml::parseToArray(file_get_contents(config_path('features.toml')))['flags'];
if ($flags['new_dashboard']) { ... }
$jsonData = json_decode(file_get_contents('config.json'), true);
file_put_contents('config.toml', (string) Toml::encode($jsonData));
Toml::encode([
'date' => Carbon::now(),
'user' => new UserModel(),
]);
JsonSerializable on your objects or use a post-encode callback:
$data = ['date' => Carbon::now()];
$toml = Toml::encode(array_map(fn($value) => $value instanceof Carbon ? $value->toIso8601String() : $value, $data));
respect/validation).use Respect\Validation\Validator as v;
$config = Toml::parseToArray(file_get_contents('config.toml'));
$validator = v::key('database.host', v::stringType())
->key('database.port', v::numeric()->between(1, 65535));
$validator->assert($config['database']);
$template = <<<'TOML'
[database]
host = "{{HOST}}"
port = {{PORT}}
TOML;
$data = [
'HOST' => 'db.example.com',
'PORT' => 5432,
];
$toml = str_replace(
array_keys($data),
array_map(fn($v) => is_numeric($v) ? $v : "'{$v}'", $data),
$template
);
$config = Toml::parseToArray($toml);
strtr() or a templating engine like php-league/plates.PHP 8.1+ Requirement:
php -r "echo PHP_VERSION_ID >= 80100 || 'Upgrade PHP!';"
phpbrew).TOML 1.1 Quirks:
# Valid TOML 1.1 (no trailing comma)
items = [1, 2, 3]
Toml::parse() to catch syntax errors early:
try {
$document = Toml::parse($tomlString);
} catch (\Internal\Toml\Exception\ParseError $e) {
report($e);
}
DateTime Handling:
DateTime objects to ISO 8601 strings, but parsing expects specific formats (e.g., 1979-05-27T07:32:00Z).$data = ['created' => (new DateTime())->format(DateTime::ATOM)];
Comments and Whitespace:
$document = Toml::parse($tomlString);
How can I help you explore Laravel packages today?