- How do I parse a TOML config file in Laravel using internal/toml?
- Use `Toml::parseToArray(file_get_contents('path/to/config.toml'))` to convert TOML into a PHP array. Store the result in Laravel’s config cache with `config(['toml-key' => $parsedArray])` or merge it into `config/app.php` via a service provider.
- Does internal/toml support TOML 1.1 features like bare keys or table arrays?
- Yes, the package fully supports TOML 1.1 (verified via spec compliance). It handles bare keys, dotted keys, table arrays, and other 1.1 features without breaking legacy TOML 1.0 files. Tested with real-world TOML from Terraform and Pulumi.
- Can I use internal/toml to replace Laravel’s PHP config files with TOML?
- Absolutely. Create a custom `ConfigReader` to load TOML files from `config/toml/` and merge them into Laravel’s config system. The package’s `parseToArray()` method outputs standard PHP arrays, so integration is seamless. Example: `config(['database' => Toml::parseToArray('config/toml/database.toml')])`.
- How does internal/toml perform compared to spatie/toml or YAML parsers?
- internal/toml is optimized for PHP 8.1+ and TOML 1.1, offering faster parsing for large configs (e.g., 10KB+) than YAML/JSON. Benchmark against `spatie/toml` (TOML 0.5.0) to confirm, but expect better performance for complex TOML structures like nested tables or arrays. AST-based parsing adds minimal overhead.
- What’s the difference between `parseToArray()` and `parse()` in internal/toml?
- `parseToArray()` directly converts TOML to a PHP array for simplicity, while `parse()` returns an AST (Abstract Syntax Tree) for advanced use cases like validation, transformation, or schema enforcement. Use AST when you need to inspect/modify TOML nodes before encoding back to TOML.
- Can I use internal/toml with Laravel Forge or Envoyer for deployment configs?
- Yes. Deploy TOML configs alongside PHP assets via Forge/Envoyer. Use `Toml::parseToArray()` in a service provider to load configs during deployment, or cache parsed TOML in Laravel’s cache drivers (e.g., `cache()->forever('toml:config', $parsedArray)`) for performance.
- How do I validate TOML files before parsing in Laravel?
- Use the AST (`Toml::parse()`) to validate TOML structure before conversion. Check for required keys, data types, or schema compliance by traversing nodes (e.g., `foreach ($document->nodes as $node)`). For strict validation, integrate with `symfony/yaml` or `webonyx/graphql-php` for schema enforcement.
- Will internal/toml work with PHP 8.2 or later?
- The package targets PHP 8.1+, but monitor updates for PHP 8.2+ compatibility. Test early in CI/CD pipelines. If issues arise, consider a polyfill or fallback to `spatie/toml` (though it lacks TOML 1.1 support). The maintainers track PHP version changes closely.
- How can I round-trip TOML (parse → modify → encode) without losing formatting?
- Use `Toml::parse()` to get an AST, modify nodes (e.g., update values or add tables), then cast the document to a string: `(string) $document`. This preserves TOML formatting (comments, trailing commas, hex/octal numbers) during round-trip serialization.
- Are there alternatives to internal/toml for Laravel TOML support?
- The main alternative is `spatie/toml`, but it only supports TOML 0.5.0 and lacks AST features. For TOML 1.1, `internal/toml` is the best choice. If you need YAML/JSON fallback, combine it with `symfony/yaml` or `league/commonmark` for hybrid config systems.