项目作者: klausi

项目描述 :
YAML parser that also reads/writes comment lines
高级语言: PHP
项目地址: git://github.com/klausi/yaml_comments.git
创建时间: 2017-03-05T18:46:46Z
项目社区:https://github.com/klausi/yaml_comments

开源协议:GNU General Public License v2.0

下载


YamlComments

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

  1. use Klausi\YamlComments\YamlComments;
  2. $exampleYaml = <<<'EOF'
  3. name: Node
  4. type: module
  5. # Messing up line numbers with comments, yay!
  6. description: 'Allows content to be submitted to the site and displayed on pages.'
  7. package: Core
  8. version: VERSION
  9. core: 8.x
  10. configure: entity.node_type.collection
  11. dependencies:
  12. - text
  13. - drupal:rest
  14. - views
  15. # Some comment here.
  16. - rules
  17. EOF;
  18. $parseResult = YamlComments::parse($exampleYaml);

Get the comment lines of this document:

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

Get the line number of a particular key:

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