Convert YAML file to PHP file

I have a bunch of Yaml configuration files that I want to convert to direct PHP files so that the application does not process Yaml files every time.

I know that there are many libraries that will allow you to convert Yaml to php array, but are there any libraries that will allow you to convert yaml file to php file?

What makes this complicated is that converting the yaml file to php usually creates a multidimensional array.

Thank.

UPDATE:

I just realized that the following code does what I think is needed. Note the second parameter in print_r. Pretty cool...

$test = print_r($yaml, true);
file_put_contents('test.php', $test);
+3
source share
3 answers

/ PHP, script :

$array = yaml_import(...);

$array = var_export($array, 1);
file_put_contens($fn, "<?php \$array = $array; ?>");
+6

Symfony YAML

Symfony2 Yaml : YAML (Parser), PHP YAML (Dumper).

, .

YAML

parse() YAML PHP:

use Symfony\Component\Yaml\Parser;

$yaml = new Parser();

$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
+4
file_put_contents('file.php', '<?php ' . var_export(your_yaml_parser_function_which_returns_php_array('file.yaml'), true));
+3
source

Source: https://habr.com/ru/post/1790239/


All Articles