Skip to content

Commit 7dd4bae

Browse files
committed
use static Yaml API
1 parent 3d98b28 commit 7dd4bae

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

components/yaml/introduction.rst

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,20 @@ acts as a thin wrapper that simplifies common uses.
9595
Reading YAML Files
9696
~~~~~~~~~~~~~~~~~~
9797

98-
The :method:`Symfony\\Component\\Yaml\\Parser::parse` method parses a YAML
98+
The :method:`Symfony\\Component\\Yaml\\Yaml::parse` method parses a YAML
9999
string and converts it to a PHP array:
100100

101101
.. code-block:: php
102102
103-
use Symfony\Component\Yaml\Parser;
103+
use Symfony\Component\Yaml\Yaml;
104+
105+
$value = Yaml::parse(file_get_contents('/path/to/file.yml'));
104106
105-
$yaml = new Parser();
107+
.. caution::
106108

107-
$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
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.
108112

109113
If an error occurs during parsing, the parser throws a
110114
:class:`Symfony\\Component\\Yaml\\Exception\\ParseException` exception
@@ -116,71 +120,35 @@ error occurred:
116120
use Symfony\Component\Yaml\Exception\ParseException;
117121
118122
try {
119-
$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
123+
$value = Yaml::parse(file_get_contents('/path/to/file.yml'));
120124
} catch (ParseException $e) {
121125
printf("Unable to parse the YAML string: %s", $e->getMessage());
122126
}
123127
124-
.. tip::
125-
126-
As the parser is re-entrant, you can use the same parser object to load
127-
different YAML strings.
128-
129-
It may also be convenient to use the
130-
:method:`Symfony\\Component\\Yaml\\Yaml::parse` wrapper method:
131-
132-
.. code-block:: php
133-
134-
use Symfony\Component\Yaml\Yaml;
135-
136-
$yaml = Yaml::parse(file_get_contents('/path/to/file.yml'));
137-
138-
The :method:`Symfony\\Component\\Yaml\\Yaml::parse` static method takes a YAML
139-
string or a file containing YAML. Internally, it calls the
140-
:method:`Symfony\\Component\\Yaml\\Parser::parse` method, but enhances the
141-
error if something goes wrong by adding the filename to the message.
142-
143-
.. caution::
144-
145-
Because it is currently possible to pass a filename to this method, you
146-
must validate the input first. Passing a filename is deprecated in
147-
Symfony 2.2, and will be removed in Symfony 3.0.
148-
149128
.. _components-yaml-dump:
150129

151130
Writing YAML Files
152131
~~~~~~~~~~~~~~~~~~
153132

154-
The :method:`Symfony\\Component\\Yaml\\Dumper::dump` method dumps any PHP
133+
The :method:`Symfony\\Component\\Yaml\\Yaml::dump` method dumps any PHP
155134
array to its YAML representation:
156135

157136
.. code-block:: php
158137
159-
use Symfony\Component\Yaml\Dumper;
138+
use Symfony\Component\Yaml\Yaml;
160139
161140
$array = array(
162141
'foo' => 'bar',
163142
'bar' => array('foo' => 'bar', 'bar' => 'baz'),
164143
);
165144
166-
$dumper = new Dumper();
167-
168-
$yaml = $dumper->dump($array);
145+
$yaml = Yaml::dump($array);
169146
170147
file_put_contents('/path/to/file.yml', $yaml);
171148
172149
If an error occurs during the dump, the parser throws a
173150
:class:`Symfony\\Component\\Yaml\\Exception\\DumpException` exception.
174151

175-
If you only need to dump one array, you can use the
176-
:method:`Symfony\\Component\\Yaml\\Yaml::dump` static method shortcut:
177-
178-
.. code-block:: php
179-
180-
use Symfony\Component\Yaml\Yaml;
181-
182-
$yaml = Yaml::dump($array);
183-
184152
Array Expansion and Inlining
185153
............................
186154

@@ -192,7 +160,7 @@ representation:
192160
193161
{ foo: bar, bar: { foo: bar, bar: baz } }
194162
195-
The second argument of the :method:`Symfony\\Component\\Yaml\\Dumper::dump`
163+
The second argument of the :method:`Symfony\\Component\\Yaml\\Yaml::dump`
196164
method customizes the level at which the output switches from the expanded
197165
representation to the inline one:
198166

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy