@@ -92,8 +92,8 @@ other dumps a PHP array to a YAML string
92
92
On top of these two classes, the :class: `Symfony\\ Component\\ Yaml\\ Yaml ` class
93
93
acts as a thin wrapper that simplifies common uses.
94
94
95
- Reading YAML Files
96
- ~~~~~~~~~~~~~~~~~~
95
+ Reading YAML Contents
96
+ ~~~~~~~~~~~~~~~~~~~~~
97
97
98
98
The :method: `Symfony\\ Component\\ Yaml\\ Yaml::parse ` method parses a YAML
99
99
string and converts it to a PHP array:
@@ -102,13 +102,8 @@ string and converts it to a PHP array:
102
102
103
103
use Symfony\Component\Yaml\Yaml;
104
104
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')
112
107
113
108
If an error occurs during parsing, the parser throws a
114
109
:class: `Symfony\\ Component\\ Yaml\\ Exception\\ ParseException ` exception
@@ -120,11 +115,28 @@ error occurred:
120
115
use Symfony\Component\Yaml\Exception\ParseException;
121
116
122
117
try {
123
- $value = Yaml::parse(file_get_contents('/path/to/file.yml') );
118
+ $value = Yaml::parse('...' );
124
119
} 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());
126
121
}
127
122
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
+
128
140
.. _components-yaml-dump :
129
141
130
142
Writing YAML Files
0 commit comments