Drop-in replacement for Symfony's Yaml component with comments support.

dev-main 2025-07-30 11:36 UTC

This package is auto-updated.

Last update: 2025-07-31 00:28:48 UTC


README

Yaml logo

Drop-in replacement for Symfony's Yaml component with comments support.

GitHub Issues GitHub Pull Requests Test PHP codecov GitHub release (latest by date) LICENSE Renovate

Why

Symfony's Yaml component is a powerful tool for parsing and dumping YAML files, but it does not preserve comments when modifying YAML content. This can be problematic when you want to maintain the original context and documentation within your YAML files.

consolidation/comments helps to preserve comments when parsing and dumping YAML files, but it does not provide a drop-in replacement for Symfony's Yaml component. It also has some issues with duplicated comments.

This package makes a best-effort attempt to address these issues, though it may not fully resolve all cases. We use before/after fixtures in tests to address specific scenarios.

Features

  • Drop-in replacement: Compatible with Symfony's Yaml component API
  • Comment preservation: Maintains YAML comments when parsing and dumping
  • Post-processing: Automatically unquotes unnecessary strings and removes duplicate lines

Installation

composer require alexskrypnyk/yaml

Usage

use AlexSkrypnyk\Yaml\Yaml;

// Parse YAML string.
$data = Yaml::parse($yamlString);

// Parse and modify.
$data = Yaml::parseFile('config.yml');

$data['config']['database']['host'] = 'newhost';

// Dump PHP array to YAML with preserved comments.
$output = Yaml::dump($data, 4, 2);

Maintenance

composer install
composer lint
composer test

This repository was created using the Scaffold project template