8000 [Yaml] use static Yaml API by xabbuh · Pull Request #6598 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] use static Yaml API #6598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 13 additions & 45 deletions components/yaml/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,20 @@ acts as a thin wrapper that simplifies common uses.
Reading YAML Files
~~~~~~~~~~~~~~~~~~

The :method:`Symfony\\Component\\Yaml\\Parser::parse` method parses a YAML
The :method:`Symfony\\Component\\Yaml\\Yaml::parse` method parses a YAML
string and converts it to a PHP array:

.. code-block:: php

use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

$value = Yaml::parse(file_get_contents('/path/to/file.yml'));

$yaml = new Parser();
.. caution::

$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
Because it is currently possible to pass a filename to this method, you
must validate the input first. Passing a filename is deprecated in
Symfony 2.2, and was removed in Symfony 3.0.

If an error occurs during parsing, the parser throws a
:class:`Symfony\\Component\\Yaml\\Exception\\ParseException` exception
Expand All @@ -116,71 +120,35 @@ error occurred:
use Symfony\Component\Yaml\Exception\ParseException;

try {
$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
$value = Yaml::parse(file_get_contents('/path/to/file.yml'));
} catch (ParseException $e) {
printf("Unable to parse the YAML string: %s", $e->getMessage());
}

.. tip::

As the parser is re-entrant, you can use the same parser object to load
different YAML strings.

It may also be convenient to use the
:method:`Symfony\\Component\\Yaml\\Yaml::parse` wrapper method:

.. code-block:: php

use Symfony\Component\Yaml\Yaml;

$yaml = Yaml::parse(file_get_contents('/path/to/file.yml'));

The :method:`Symfony\\Component\\Yaml\\Yaml::parse` static method takes a YAML
string or a file containing YAML. Internally, it calls the
:method:`Symfony\\Component\\Yaml\\Parser::parse` method, but enhances the
error if something goes wrong by adding the filename to the message.

.. caution::

Because it is currently possible to pass a filename to this method, you
must validate the input first. Passing a filename is deprecated in
Symfony 2.2, and will be removed in Symfony 3.0.

.. _components-yaml-du 8000 mp:

Writing YAML Files
~~~~~~~~~~~~~~~~~~

The :method:`Symfony\\Component\\Yaml\\Dumper::dump` method dumps any PHP
The :method:`Symfony\\Component\\Yaml\\Yaml::dump` method dumps any PHP
array to its YAML representation:

.. code-block:: php

use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Yaml;

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

$dumper = new Dumper();

$yaml = $dumper->dump($array);
$yaml = Yaml::dump($array);

file_put_contents('/path/to/file.yml', $yaml);

If an error occurs during the dump, the parser throws a
:class:`Symfony\\Component\\Yaml\\Exception\\DumpException` exception.

If you only need to dump one array, you can use the
:method:`Symfony\\Component\\Yaml\\Yaml::dump` static method shortcut:

.. code-block:: php

use Symfony\Component\Yaml\Yaml;

$yaml = Yaml::dump($array);

Array Expansion and Inlining
............................

Expand All @@ -192,7 +160,7 @@ representation:

{ foo: bar, bar: { foo: bar, bar: baz } }

The second argument of the :method:`Symfony\\Component\\Yaml\\Dumper::dump`
The second argument of the :method:`Symfony\\Component\\Yaml\\Yaml::dump`
method customizes the level at which the output switches from the expanded
representation to the inline one:

Expand Down
0