nette/neon
Nette Neon is a human-friendly configuration format for PHP, similar to YAML but simpler and safer. This package provides a fast NEON encoder/decoder with support for comments, multi-line strings, and rich data types, ideal for configs and app settings.
Install the package via Composer: composer require nette/neon. Then use the two core static methods: Neon::decode(string $code) to parse NEON strings into PHP types, and Neon::encode(mixed $value, int $indentation = 2) to serialize PHP data back to NEON. For reading config files, use the atomic Neon::decodeFile(string $file) — it safely reads files without partial reads and returns the decoded structure directly. First-time users should validate PHP 8.2+ compatibility (v3.4.7 requires 8.2–8.5) and ensure ext-json is enabled. Start with a simple config.neon file like:
parameters:
debugMode: true
database:
host: localhost
port: 5432
Then load it with Neon::decodeFile('config.neon') — no manual file I/O or regex parsing needed.
decodeFile() in Laravel config loaders (e.g., wrap in a NeonConfigLoader service) to replace JSON/YAML for Nette-dependent apps. It’s safe for concurrent deploys and handles atomic updates.Neon::decode() to get a typed AST (Neon\Ast\Node[]). Use Neon\Ast\Traverser with enter/leave callbacks to lint, validate (e.g., enforce schema rules), or transform values — e.g., inject environment variables by mutating scalar nodes.vendor/bin/neon-lint config/*.neon in CI pipelines (v3.3.1+ includes this). It catches subtle issues like mixed indentation, trailing comments, or unclosed strings that decode() might silently accept.Neon::encode($config, indentation: 4) for readability in generated configs, or Neon::encode($data) for compact inline arrays. v3.3.0+ ensures consistent formatting via AST-based encoding.on/off literals and \xAA escapes (use \u00AA). v3.3.x renamed Node::startPos to startTokenPos. Always update incrementally and run neon-lint after upgrades.U+FFFD (v3.4.7+). If strict validation is needed (e.g., user uploads), pre-validate with mb_check_encoding() — don’t rely on Neon::decode() alone.(string) only, and validate range before use.encode() throws on INF, NAN, and control characters (v3.4.4+). Pre-filter values (e.g., is_finite() for floats) or use fallback like json_encode($data) for edge cases.null from a callback deletes nodes, but Traverser::DONT_TRAVERSE_CHILDREN (note the camelCase) skips descent without deleting. To avoid corruption, clone nodes before mutation or use immutable transformation patterns.neon-lint is stricter than Neon::decode() — e.g., it rejects trailing commas in block arrays and mixed indents. Always use neon-lint in CI, even if decodeFile() passes locally.text blocks) can cause parsing failures (v3.4.6 fixes some cases, but ensure consistent whitespace). Use encode(..., flags: Neon::BLOCK_LITERAL) only when necessary, and validate output with neon-lint.How can I help you explore Laravel packages today?