Skip to content

Documented the new parseFile() method #8523

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 2 commits into from
Oct 29, 2017
Merged
Changes from 1 commit
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
34 changes: 23 additions & 11 deletions components/yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ other dumps a PHP array to a YAML string
On top of these two classes, the :class:`Symfony\\Component\\Yaml\\Yaml` class
acts as a thin wrapper that simplifies common uses.

Reading YAML Files
~~~~~~~~~~~~~~~~~~
Reading YAML Contents
~~~~~~~~~~~~~~~~~~~~~

The :method:`Symfony\\Component\\Yaml\\Yaml::parse` method parses a YAML
string and converts it to a PHP array:
Expand All @@ -102,13 +102,8 @@ string and converts it to a PHP array:

use Symfony\Component\Yaml\Yaml;

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

.. 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 was removed in Symfony 3.0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should already remove this block in the 3.3 docs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #8554

$value = Yaml::parse("foo:\n 'bar'");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the newline here looks wrong to me, I would just use $value = Yaml::parse('foo: bar');

// $value = array('foo' => 'bar')

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

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

Reading YAML Files
~~~~~~~~~~~~~~~~~~

The :method:`Symfony\\Component\\Yaml\\Yaml::parseFile` method parses the YAML
contents of the given file path and converts them to a PHP array:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result must not necessarily be an array


.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be omitted when terminating the previous paragraph with two colons


use Symfony\Component\Yaml\Yaml;

$value = Yaml::parseFile(file_get_contents('/path/to/file.yml'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_get_contents() must be omitted here


.. versionadded:: 3.4
The ``parseFile()`` method was introduced in Symfony 3.4.

If an error occurs during parsing, the parser throws a ``ParseException`` exception.

.. _components-yaml-dump:

Writing YAML Files
Expand Down