Skip to content

Commit

Permalink
minor #6598 [Yaml] use static Yaml API (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] use static Yaml API

I don't see a good reasons to explicitly describe the non-static API. If you ever really need to use the `Parser` and `Dumper` classes directly, you will be able to easily figure out how to use them yourself.

Commits
-------

7dd4bae use static Yaml API
  • Loading branch information
wouterj committed May 21, 2016
2 parents e6e5fa2 + 7dd4bae commit b646db0
Showing 1 changed file with 13 additions and 45 deletions.
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-dump:

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 comments on commit b646db0

Please sign in to comment.