PHPackages                             tbela99/yaml - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. tbela99/yaml

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

tbela99/yaml
============

PHP Yaml Parser and Dumper that preserves comments

v5.2.1(5y ago)012MITPHPPHP ^7.4

Since Sep 26Pushed 5y agoCompare

[ Source](https://github.com/tbela99/yaml)[ Packagist](https://packagist.org/packages/tbela99/yaml)[ Docs](https://github.com/tbela99/yaml)[ RSS](/packages/tbela99-yaml/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (3)Versions (473)Used By (0)

PHP Yaml Parser and Dumper
--------------------------

[](#php-yaml-parser-and-dumper)

Based on the Symphony Yaml Component

Installation
------------

[](#installation)

```
$ composer require tbela99/yaml
```

Modify Yaml And Preserve Comments
---------------------------------

[](#modify-yaml-and-preserve-comments)

```
# this is a comment
version: "3.8"
services:
  redis:
    image: redis:alpine
    ports:
      - "6379"
    networks:
      - frontend
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  db:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend
    deploy:
      placement:
        constraints:
          - "node.role==manager"

networks:
  frontend:
  backend:

volumes:
  db-data:
# this is the last comment
```

```
use Symfony\Component\Yaml\Ast\Node;

$ast = new Node();

$data = $ast->parse($yaml);

var_dump(isset($ast['services.redis.image'])); // true

echo $ast['services.redis.image']; // redis:alpine

unset($ast['services']);

echo $ast;
```

```
# this is a comment
version: 3.8
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
```

```
$ast['service.db'] = [
  'image' => 'mariadb',
  'environment' => [
    'NODE' => true,
    'SIZE' => '2G'
  ]
];

$ast['service.db.image']->addComment('mariadb image?');
$ast['service.db.environment']->addComment('environment variables');
$ast['service.db.environment.NODE']->addComment('killer app');
$ast['service.db.environment.SIZE']->addComment('going big here ...');

echo $ast;
```

```
# this is a comment
version: 3.8
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
service:
  db:
    # mariadb image?
    image: mariadb
    # environment variables
    environment:
      # killer app
      NODE: true
      # going big here ...
      SIZE: 2G
```

```
$ast['service.db.environment.NODE'] = false;
$ast['service.db.environment.NODE']->setComments(['killer bee']);
$ast['service.db.environment.SIZE'] = '4G';
$ast['service.db.environment.RANGE'] = '100m';

echo $ast;
```

```
# this is a comment
version: 3.8
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
service:
  db:
    # mariadb image?
    image: mariadb
    # environment variables
    environment:
      # killer bee
      NODE: false
      # going big here ...
      SIZE: 4G
      RANGE: 100m
```

```
// binary value
$ast['service.db.environment.BIN'] = hex2bin('abcf');
// multiline string
$ast['service.db.environment.secret'] = "correct horse
battery staple";
```

```
# this is a comment
version: 3.8
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
service:
  db:
    # mariadb image?
    image: mariadb
    # environment variables
    environment:
      # killer bee
      NODE: false
      # going big here ...
      SIZE: 4G
      RANGE: 100m
      BIN: !!binary q88=
      secret: "correct horse \nbattery staple"
```

Get parsed data
---------------

[](#get-parsed-data)

```
/**
 * @var array $data
 */

$data = $ast->getValue();
```

Manipulate Data
---------------

[](#manipulate-data)

```
/**
 * @var array $data
 */

$ast['version'] = 3.7;
$ast['author'] = 'a random guy';
$ast['authors.list'] = ['John', 'Raymond', 'Michael'];
```

```
# this is a comment
version: 3.7
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
service:
  db:
    # mariadb image?
    image: mariadb
    # environment variables
    environment:
      # killer bee
      NODE: false
      # going big here ...
      SIZE: 4G
      RANGE: 100m
      BIN: !!binary s6P2WISy5A2WFdyUiHtxemncqdEBpVT+JQm2g5fCtN8=
      secret: "correct horse \nbattery staple"
author: "a random guy"
authors:
  list:
    - John
    - Raymond
    - Michael
```

```
unset($ast['service.db']);
echo $ast;
```

```
# this is a comment
version: 3.7
networks:
  frontend:
  backend:
volumes:
  db-data:
# this is the last comment
service:

author: "a random guy"
authors:
  list:
    - John
    - Raymond
    - Michael
```

Parsing Yaml
------------

[](#parsing-yaml)

```
use Symfony\Component\Yaml\Ast\Node;
use Symfony\Component\Yaml\Ast\Value;

$ast = new Node();

// parse Yaml string
$ast->parse($yaml);
//or parse Yaml file
$ast->parseFile($file);

// alter the ast
$ast['version'] = '1.0';
$ast['version']->addComment('this comment is associated to the version number');
// or
$ast->appendValue(1.0, 'version', ['this comment is associated to the version number']);
// or
$ast->appendNode(new Value(1.0), 'version', ['this comment is associated to the version number']);

// render the ast
$yaml = (string) $ast;

// do something useful with the output
file_put_contents('configuration.yaml', $yaml);
```

Using '.' In Key
----------------

[](#using--in-key)

By default '.' is used and path delimiter. You must escape the key to avoid interpretation

```
$ast = new Node();

$ast['path.to.data'] = "user name";

// use '.' in the key name
$ast[$ast->escapeKey('v0.1')] = [

  'description' => 'first stable release',
  'download' => 'https://example.com'
];

$ast['versions'] = [
  'v0.1' => [

    'description' => 'first stable release',
    'download' => 'https://example.com'
  ]
];

echo $ast;
```

result

```
path:
  to:
    data: "user name"
v0.1:
  description: "first stable release"
  download: "https://example.com"
versions:
  v0.1:
    description: "first stable release"
    download: "https://example.com"
```

Recursively Merge Two Yaml Nodes
--------------------------------

[](#recursively-merge-two-yaml-nodes)

```
$data =
'path:
 to:
  name: "John Henry"
versions:
 v0.2:
  description: "second stable release"
  download: "https://example.com/2"';

$node = new Node();

$node->parse($data);

$ast->merge($node);

echo "$ast\n\n";
```

result

```
# this comment is associated to the version number
version: '1.0'
path:
  to:
    data: 'user name'
    name: 'John Henry'
v0.1:
  description: 'first stable release'
  download: 'https://example.com'
versions:
  v0.1:
    description: 'first stable release'
    download: 'https://example.com'
  v0.2:
    description: 'second stable release'
    download: 'https://example.com/2'
``
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~12 days

Total

470

Last Release

2150d ago

Major Versions

v4.4.7 → v5.0.72020-03-30

v3.4.40 → v4.4.82020-04-28

v4.4.8 → v5.0.82020-04-28

3.4.x-dev → 4.4.x-dev2020-05-05

4.4.x-dev → 5.0.x-dev2020-05-05

PHP version history (9 changes)2.0.4PHP &gt;=5.3.2

v2.1.1PHP &gt;=5.3.3

v2.7.0-BETA1PHP &gt;=5.3.9

v3.0.0-BETA1PHP &gt;=5.5.9

v3.3.9PHP ^5.5.9|&gt;=7.0.8

v4.0.0-BETA1PHP ^7.1.3

v5.0.0-RC1PHP ^7.2.9

v5.0.1PHP ^7.2.5

5.1.1PHP ^7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/58517?v=4)[Thierry Bela Nanga](/maintainers/tbela99)[@tbela99](https://github.com/tbela99)

---

Top Contributors

[![fabpot](https://avatars.githubusercontent.com/u/47313?v=4)](https://github.com/fabpot "fabpot (491 commits)")[![nicolas-grekas](https://avatars.githubusercontent.com/u/243674?v=4)](https://github.com/nicolas-grekas "nicolas-grekas (411 commits)")[![xabbuh](https://avatars.githubusercontent.com/u/1957048?v=4)](https://github.com/xabbuh "xabbuh (154 commits)")[![tbela99](https://avatars.githubusercontent.com/u/58517?v=4)](https://github.com/tbela99 "tbela99 (20 commits)")[![Tobion](https://avatars.githubusercontent.com/u/610090?v=4)](https://github.com/Tobion "Tobion (15 commits)")[![chalasr](https://avatars.githubusercontent.com/u/7502063?v=4)](https://github.com/chalasr "chalasr (11 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (11 commits)")[![fancyweb](https://avatars.githubusercontent.com/u/3658119?v=4)](https://github.com/fancyweb "fancyweb (8 commits)")[![alexpott](https://avatars.githubusercontent.com/u/769634?v=4)](https://github.com/alexpott "alexpott (7 commits)")[![jderusse](https://avatars.githubusercontent.com/u/578547?v=4)](https://github.com/jderusse "jderusse (7 commits)")[![keradus](https://avatars.githubusercontent.com/u/2716794?v=4)](https://github.com/keradus "keradus (6 commits)")[![peterrehm](https://avatars.githubusercontent.com/u/2010989?v=4)](https://github.com/peterrehm "peterrehm (6 commits)")[![GuilhemN](https://avatars.githubusercontent.com/u/6871899?v=4)](https://github.com/GuilhemN "GuilhemN (6 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (6 commits)")[![ogizanagi](https://avatars.githubusercontent.com/u/2211145?v=4)](https://github.com/ogizanagi "ogizanagi (5 commits)")[![saro0h](https://avatars.githubusercontent.com/u/667519?v=4)](https://github.com/saro0h "saro0h (5 commits)")[![sun](https://avatars.githubusercontent.com/u/41992?v=4)](https://github.com/sun "sun (5 commits)")[![yceruto](https://avatars.githubusercontent.com/u/2028198?v=4)](https://github.com/yceruto "yceruto (4 commits)")[![derrabus](https://avatars.githubusercontent.com/u/1506493?v=4)](https://github.com/derrabus "derrabus (4 commits)")[![dunglas](https://avatars.githubusercontent.com/u/57224?v=4)](https://github.com/dunglas "dunglas (4 commits)")

---

Tags

phpparseryamlcommentsdumper

### Embed Badge

![Health badge](/badges/tbela99-yaml/health.svg)

```
[![Health](https://phpackages.com/badges/tbela99-yaml/health.svg)](https://phpackages.com/packages/tbela99-yaml)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M702](/packages/league-commonmark)[simplehtmldom/simplehtmldom

A fast, simple and reliable HTML document parser for PHP.

1921.3M14](/packages/simplehtmldom-simplehtmldom)[corveda/php-sandbox

A PHP library that can be used to run PHP code in a sandboxed environment

23483.5k2](/packages/corveda-php-sandbox)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[leonelquinteros/php-toml

PHP parser for TOML language ( https://github.com/toml-lang/toml )

266.7k](/packages/leonelquinteros-php-toml)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
