"Fossies" - the Fresh Open Source Software Archive

Member "4.6.1/vendor/yaml/doc/01-Usage.markdown" (8 Apr 2021, 2939 Bytes) of package /linux/www/studip-4.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

Using Symfony YAML

The Symfony YAML library is very simple and consists of two main classes: one to parse YAML strings (sfYamlParser), and the other to dump a PHP array to a YAML string (sfYamlDumper).

On top of these two core classes, the main sfYaml class acts as a thin wrapper and simplifies common uses.

Reading YAML Files

The sfYamlParser::parse() method parses a YAML string and converts it to a PHP array:

[php]
$yaml = new sfYamlParser();
$value = $yaml->parse(file_get_contents('/path/to/file.yaml'));

If an error occurs during parsing, the parser throws an exception indicating the error type and the line in the original YAML string where the error occurred:

[php]
try
{
  $value = $yaml->parse(file_get_contents('/path/to/file.yaml'));
}
catch (InvalidArgumentException $e)
{
  // an error occurred during parsing
  echo "Unable to parse the YAML string: ".$e->getMessage();
}

TIP As the parser is reentrant, you can use the same parser object to load different YAML strings.

When loading a YAML file, it is sometimes better to use the sfYaml::load() wrapper method:

[php]
$loader = sfYaml::load('/path/to/file.yml');

The sfYaml::load() static method takes a YAML string or a file containing YAML. Internally, it calls the sfYamlParser::parse() method, but with some added bonuses:

Writing YAML Files

The sfYamlDumper dumps any PHP array to its YAML representation:

[php]
$array = array('foo' => 'bar', 'bar' => array('foo' => 'bar', 'bar' => 'baz'));

$dumper = new sfYamlDumper();
$yaml = $dumper->dump($array);
file_put_contents('/path/to/file.yaml', $yaml);

NOTE Of course, the Symfony YAML dumper is not able to dump resources. Also, even if the dumper is able to dump PHP objects, it is to be considered an alpha feature.

If you only need to dump one array, you can use the sfYaml::dump() static method shortcut:

[php]
$yaml = sfYaml::dump($array, $inline);

The YAML format supports two kind of representation for arrays, the expanded one, and the inline one. By default, the dumper uses the inline representation:

[yml]
{ foo: bar, bar: { foo: bar, bar: baz } }

The second argument of the dump() method customizes the level at which the output switches from the expanded representation to the inline one:

[php]
echo $dumper->dump($array, 1);