Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Yaml;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Yaml\Exception\ParseException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Yaml offers convenience methods to load and dump YAML.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
20 *
|
Chris@14
|
21 * @final since version 3.4
|
Chris@0
|
22 */
|
Chris@0
|
23 class Yaml
|
Chris@0
|
24 {
|
Chris@0
|
25 const DUMP_OBJECT = 1;
|
Chris@0
|
26 const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
|
Chris@0
|
27 const PARSE_OBJECT = 4;
|
Chris@0
|
28 const PARSE_OBJECT_FOR_MAP = 8;
|
Chris@0
|
29 const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
|
Chris@0
|
30 const PARSE_DATETIME = 32;
|
Chris@0
|
31 const DUMP_OBJECT_AS_MAP = 64;
|
Chris@0
|
32 const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
|
Chris@0
|
33 const PARSE_CONSTANT = 256;
|
Chris@14
|
34 const PARSE_CUSTOM_TAGS = 512;
|
Chris@14
|
35 const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
|
Chris@14
|
36
|
Chris@14
|
37 /**
|
Chris@14
|
38 * @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable keys instead.
|
Chris@14
|
39 */
|
Chris@14
|
40 const PARSE_KEYS_AS_STRINGS = 2048;
|
Chris@14
|
41
|
Chris@14
|
42 /**
|
Chris@14
|
43 * Parses a YAML file into a PHP value.
|
Chris@14
|
44 *
|
Chris@17
|
45 * Usage:
|
Chris@17
|
46 *
|
Chris@17
|
47 * $array = Yaml::parseFile('config.yml');
|
Chris@17
|
48 * print_r($array);
|
Chris@14
|
49 *
|
Chris@14
|
50 * @param string $filename The path to the YAML file to be parsed
|
Chris@14
|
51 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@14
|
52 *
|
Chris@14
|
53 * @return mixed The YAML converted to a PHP value
|
Chris@14
|
54 *
|
Chris@14
|
55 * @throws ParseException If the file could not be read or the YAML is not valid
|
Chris@14
|
56 */
|
Chris@14
|
57 public static function parseFile($filename, $flags = 0)
|
Chris@14
|
58 {
|
Chris@14
|
59 $yaml = new Parser();
|
Chris@14
|
60
|
Chris@14
|
61 return $yaml->parseFile($filename, $flags);
|
Chris@14
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Parses YAML into a PHP value.
|
Chris@0
|
66 *
|
Chris@0
|
67 * Usage:
|
Chris@0
|
68 * <code>
|
Chris@0
|
69 * $array = Yaml::parse(file_get_contents('config.yml'));
|
Chris@0
|
70 * print_r($array);
|
Chris@0
|
71 * </code>
|
Chris@0
|
72 *
|
Chris@0
|
73 * @param string $input A string containing YAML
|
Chris@0
|
74 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return mixed The YAML converted to a PHP value
|
Chris@0
|
77 *
|
Chris@0
|
78 * @throws ParseException If the YAML is not valid
|
Chris@0
|
79 */
|
Chris@0
|
80 public static function parse($input, $flags = 0)
|
Chris@0
|
81 {
|
Chris@17
|
82 if (\is_bool($flags)) {
|
Chris@14
|
83 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
84
|
Chris@0
|
85 if ($flags) {
|
Chris@0
|
86 $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
87 } else {
|
Chris@0
|
88 $flags = 0;
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@17
|
92 if (\func_num_args() >= 3) {
|
Chris@14
|
93 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
94
|
Chris@0
|
95 if (func_get_arg(2)) {
|
Chris@0
|
96 $flags |= self::PARSE_OBJECT;
|
Chris@0
|
97 }
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@17
|
100 if (\func_num_args() >= 4) {
|
Chris@14
|
101 @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
102
|
Chris@0
|
103 if (func_get_arg(3)) {
|
Chris@0
|
104 $flags |= self::PARSE_OBJECT_FOR_MAP;
|
Chris@0
|
105 }
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 $yaml = new Parser();
|
Chris@0
|
109
|
Chris@0
|
110 return $yaml->parse($input, $flags);
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Dumps a PHP value to a YAML string.
|
Chris@0
|
115 *
|
Chris@0
|
116 * The dump method, when supplied with an array, will do its best
|
Chris@0
|
117 * to convert the array into friendly YAML.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param mixed $input The PHP value
|
Chris@0
|
120 * @param int $inline The level where you switch to inline YAML
|
Chris@0
|
121 * @param int $indent The amount of spaces to use for indentation of nested nodes
|
Chris@0
|
122 * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
123 *
|
Chris@0
|
124 * @return string A YAML string representing the original PHP value
|
Chris@0
|
125 */
|
Chris@0
|
126 public static function dump($input, $inline = 2, $indent = 4, $flags = 0)
|
Chris@0
|
127 {
|
Chris@17
|
128 if (\is_bool($flags)) {
|
Chris@14
|
129 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
130
|
Chris@0
|
131 if ($flags) {
|
Chris@0
|
132 $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
133 } else {
|
Chris@0
|
134 $flags = 0;
|
Chris@0
|
135 }
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@17
|
138 if (\func_num_args() >= 5) {
|
Chris@14
|
139 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
140
|
Chris@0
|
141 if (func_get_arg(4)) {
|
Chris@0
|
142 $flags |= self::DUMP_OBJECT;
|
Chris@0
|
143 }
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 $yaml = new Dumper($indent);
|
Chris@0
|
147
|
Chris@0
|
148 return $yaml->dump($input, $inline, 0, $flags);
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|