klausi/yaml_comments

This package is abandoned and no longer maintained. No replacement package was suggested.

YAML parser that also reads/writes comment lines

0.x-dev 2017-03-19 22:20 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:06:07 UTC


README

Parses YAML and provides comments with their line numbers as well as the line number of any given key of the document structure.

The parser was copied and modified from the Symfony YAML component, MIT license Copyright (c) 2004-2017 Fabien Potencier

Usage

use Klausi\YamlComments\YamlComments;

$exampleYaml = <<<'EOF'
name: Node
type: module
# Messing up line numbers with comments, yay!
description: 'Allows content to be submitted to the site and displayed on pages.'
package: Core
version: VERSION
core: 8.x
configure: entity.node_type.collection
dependencies:
  - text
  - drupal:rest
  - views
  # Some comment here.
  - rules
EOF;

$parseResult = YamlComments::parse($exampleYaml);

Get the comment lines of this document:

print_r($parseResult->getComments());
Array
(
    [3] => # Messing up line numbers with comments, yay!
    [13] =>   # Some comment here.
)

Get the line number of a particular key:

print $parseResult->getLineNumber('name');
1
print $parseResult->getLineNumber('description');
4
print $parseResult->getLineNumber(['dependencies', 3]);
14