tbela99 / yaml
PHP Yaml Parser and Dumper that preserves comments
v5.2.1
2020-06-24 21:04 UTC
Requires
- php: ^7.4
- ext-json: *
- symfony/deprecation-contracts: ^2.1
- symfony/polyfill-ctype: ~1.8
Requires (Dev)
- symfony/console: ^4.4|^5.0
Suggests
- symfony/console: For validating YAML files using the lint command
Conflicts
- symfony/console: <4.4
- dev-master / 5.2.x-dev
- v5.2.1
- v5.2.0
- v5.1.2
- 5.1.1
- v5.1.0-BETA1
- 5.0.x-dev
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-RC1
- v5.0.0-BETA2
- v5.0.0-BETA1
- 4.4.x-dev
- v4.4.8
- v4.4.7
- v4.4.6
- v4.4.5
- v4.4.4
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.4.0-RC1
- v4.4.0-BETA2
- v4.4.0-BETA1
- 4.3.x-dev
- v4.3.11
- v4.3.10
- v4.3.9
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.3.0-RC1
- v4.3.0-BETA2
- v4.3.0-BETA1
- 4.2.x-dev
- v4.2.12
- v4.2.11
- v4.2.10
- v4.2.9
- v4.2.8
- v4.2.7
- v4.2.6
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.2.0-RC1
- v4.2.0-BETA2
- v4.2.0-BETA1
- 4.1.x-dev
- v4.1.12
- v4.1.11
- v4.1.10
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.1.0-BETA3
- v4.1.0-BETA2
- v4.1.0-BETA1
- 4.0.x-dev
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v4.0.0-RC2
- v4.0.0-RC1
- v4.0.0-BETA4
- v4.0.0-BETA3
- v4.0.0-BETA2
- v4.0.0-BETA1
- 3.4.x-dev
- v3.4.40
- v3.4.39
- v3.4.38
- v3.4.37
- v3.4.36
- v3.4.35
- v3.4.34
- v3.4.33
- v3.4.32
- v3.4.31
- v3.4.30
- v3.4.29
- v3.4.28
- v3.4.27
- v3.4.26
- v3.4.25
- v3.4.24
- v3.4.23
- v3.4.22
- v3.4.21
- v3.4.20
- v3.4.19
- v3.4.18
- v3.4.17
- v3.4.16
- v3.4.15
- v3.4.14
- v3.4.13
- v3.4.12
- v3.4.11
- v3.4.10
- v3.4.9
- v3.4.8
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.4.0-RC2
- v3.4.0-RC1
- v3.4.0-BETA4
- v3.4.0-BETA3
- v3.4.0-BETA2
- v3.4.0-BETA1
- 3.3.x-dev
- v3.3.18
- v3.3.17
- v3.3.16
- v3.3.15
- v3.3.14
- v3.3.13
- v3.3.12
- v3.3.11
- v3.3.10
- v3.3.9
- v3.3.8
- v3.3.7
- v3.3.6
- v3.3.5
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.3.0-RC1
- v3.3.0-BETA1
- 3.2.x-dev
- v3.2.14
- v3.2.13
- v3.2.12
- v3.2.11
- v3.2.10
- v3.2.9
- v3.2.8
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.2.0-RC2
- v3.2.0-RC1
- v3.2.0-BETA1
- 3.1.x-dev
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.1.0-RC1
- v3.1.0-BETA1
- 3.0.x-dev
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-BETA1
- 2.8.x-dev
- v2.8.52
- v2.8.50
- v2.8.49
- v2.8.48
- v2.8.47
- v2.8.46
- v2.8.45
- v2.8.44
- v2.8.43
- v2.8.42
- v2.8.41
- v2.8.40
- v2.8.39
- v2.8.38
- v2.8.37
- v2.8.36
- v2.8.35
- v2.8.34
- v2.8.33
- v2.8.32
- v2.8.31
- v2.8.30
- v2.8.29
- v2.8.28
- v2.8.27
- v2.8.26
- v2.8.25
- v2.8.24
- v2.8.23
- v2.8.22
- v2.8.21
- v2.8.20
- v2.8.19
- v2.8.18
- v2.8.17
- v2.8.16
- v2.8.15
- v2.8.14
- v2.8.13
- v2.8.12
- v2.8.11
- v2.8.10
- v2.8.9
- v2.8.8
- v2.8.7
- v2.8.6
- v2.8.5
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- v2.8.0-BETA1
- 2.7.x-dev
- v2.7.51
- v2.7.50
- v2.7.49
- v2.7.48
- v2.7.47
- v2.7.46
- v2.7.45
- v2.7.44
- v2.7.43
- v2.7.42
- v2.7.41
- v2.7.40
- v2.7.39
- v2.7.38
- v2.7.37
- v2.7.36
- v2.7.35
- v2.7.34
- v2.7.33
- v2.7.32
- v2.7.31
- v2.7.30
- v2.7.29
- v2.7.28
- v2.7.27
- v2.7.26
- v2.7.25
- v2.7.24
- v2.7.23
- v2.7.22
- v2.7.21
- v2.7.20
- v2.7.19
- v2.7.18
- v2.7.17
- v2.7.16
- v2.7.15
- v2.7.14
- v2.7.13
- v2.7.12
- v2.7.11
- v2.7.10
- v2.7.9
- v2.7.8
- v2.7.7
- v2.7.6
- v2.7.5
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.7.0-BETA2
- v2.7.0-BETA1
- 2.6.x-dev
- v2.6.13
- v2.6.12
- v2.6.11
- v2.6.10
- v2.6.9
- v2.6.8
- v2.6.7
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.6.0-BETA2
- v2.6.0-BETA1
- 2.5.x-dev
- v2.5.12
- v2.5.11
- v2.5.10
- v2.5.9
- v2.5.8
- v2.5.7
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.5.0-RC1
- v2.5.0-BETA2
- v2.5.0-BETA1
- 2.4.x-dev
- v2.4.10
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.4.0-RC1
- v2.4.0-BETA2
- v2.4.0-BETA1
- 2.3.x-dev
- v2.3.42
- v2.3.41
- v2.3.40
- v2.3.39
- v2.3.38
- v2.3.37
- v2.3.36
- v2.3.35
- v2.3.34
- v2.3.33
- v2.3.32
- v2.3.31
- v2.3.30
- v2.3.29
- v2.3.28
- v2.3.27
- v2.3.26
- v2.3.25
- v2.3.24
- v2.3.23
- v2.3.22
- v2.3.21
- v2.3.20
- v2.3.19
- v2.3.18
- v2.3.17
- v2.3.16
- v2.3.15
- v2.3.14
- v2.3.13
- v2.3.12
- v2.3.11
- v2.3.10
- v2.3.9
- v2.3.8
- v2.3.7
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- 2.2.x-dev
- v2.2.11
- v2.2.10
- v2.2.9
- v2.2.8
- v2.2.7
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- 2.1.x-dev
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- 2.0.x-dev
- v2.0.25
- v2.0.24
- v2.0.23
- v2.0.22
- v2.0.21
- v2.0.20
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.10
- v2.0.9
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- dev-merge_yaml_nodes
- dev-issue-8145
This package is auto-updated.
Last update: 2025-01-25 07:36:30 UTC
README
Based on the Symphony Yaml Component
Installation
$ composer require tbela99/yaml
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
/** * @var array $data */ $data = $ast->getValue();
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
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
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
$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' ``