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

Skip to content

Commit d3a6ab1

Browse files
committed
feature #8523 Documented the new parseFile() method (javiereguiluz)
This PR was squashed before being merged into the 3.4 branch (closes #8523). Discussion ---------- Documented the new parseFile() method This fixes #8424. Commits ------- 647c117 Fixes b55b03a Documented the new parseFile() method
2 parents c99caa9 + 647c117 commit d3a6ab1

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

components/yaml.rst

Lines changed: 21 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: 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,26 @@ 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 value::
128+
129+
use Symfony\Component\Yaml\Yaml;
130+
131+
$value = Yaml::parseFile('/path/to/file.yml');
132+
133+
.. versionadded:: 3.4
134+
The ``parseFile()`` method was introduced in Symfony 3.4.
135+
136+
If an error occurs during parsing, the parser throws a ``ParseException`` exception.
137+
128138
.. _components-yaml-dump:
129139

130140
Writing YAML Files

0 commit comments

Comments
 (0)
0