8000 Documented the new parseFile() method · symfony/symfony-docs@b55b03a · GitHub
[go: up one dir, main page]

Skip to content

Commit b55b03a

Browse files
committed
Documented the new parseFile() method
1 parent f8ddf1f commit b55b03a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

components/yaml.rst

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ other dumps a PHP array to a YAML string
9292
On top of these two classes, the :class:`Symfony\\Component\\Yaml\\Yaml` class
9393
acts as a thin wrapper that simplifies common uses.
9494

95-
Reading YAML Files
96-
~~~~~~~~~~~~~~~~~~
95+
Reading YAML Contents
96+
~~~~~~~~~~~~~~~~~~~~~
9797

9898
The :method:`Symfony\\Component\\Yaml\\Yaml::parse` method parses a YAML
9999
string and converts it to a PHP array:
@@ -102,13 +102,8 @@ string and converts it to a PHP array:
102102
103103
use Symfony\Component\Yaml\Yaml;
104104
105-
$value = Yaml::parse(file_get_contents('/path/to/file.yml'));
106-
107-
.. caution::
108-
109-
Because it is currently possible to pass a filename to this method, you
110-
must validate the input first. Passing a filename is deprecated in
111-
Symfony 2.2, and was removed in Symfony 3.0.
105+
$value = Yaml::parse("foo:\n 'bar'");
106+
// $value = array('foo' => 'bar')
112107
113108
If an error occurs during parsing, the parser throws a
114109
:class:`Symfony\\Component\\Yaml\\Exception\\ParseException` exception
@@ -120,11 +115,28 @@ error occurred:
120115
use Symfony\Component\Yaml\Exception\ParseException;
121116
122117
try {
123-
$value = Yaml::parse(file_get_contents('/path/to/file.yml'));
118+
$value = Yaml::parse('...');
124119
} catch (ParseException $e) {
125-
printf("Unable to parse the YAML string: %s", $e->getMessage());
120+
printf('Unable to parse the YAML string: %s', $e->getMessage());
126121
}
127122
123+
Reading YAML Files
124+
~~~~~~~~~~~~~~~~~~
125+
126+
The :method:`Symfony\\Component\\Yaml\\Yaml::parseFile` method parses the YAML
127+
contents of the given file path and converts them to a PHP array:
128+
129+
.. code-block:: php
130+
131+
use Symfony\Component\Yaml\Yaml;
132+
133+
$value = Yaml::parseFile(file_get_contents('/path/to/file.yml'));
134+
135+
.. versionadded:: 3.4
136+
The ``parseFile()`` method was introduced in Symfony 3.4.
137+
138+
If an error occurs during parsing, the parser throws a ``ParseException`` exception.
139+
128140
.. _components-yaml-dump:
129141

130142
Writing YAML Files

0 commit comments

Comments
 (0)
0