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 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
32 changes: 21 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: 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,26 @@ 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 value::

use Symfony\Component\Yaml\Yaml;

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

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

but it is static in the Yaml class (which is used here): https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Yaml/Yaml.php#L53 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, you're right!


.. 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