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@14
|
45 * Usage:
|
Chris@14
|
46 * <code>
|
Chris@14
|
47 * $array = Yaml::parseFile('config.yml');
|
Chris@14
|
48 * print_r($array);
|
Chris@14
|
49 * </code>
|
Chris@14
|
50 *
|
Chris@14
|
51 * @param string $filename The path to the YAML file to be parsed
|
Chris@14
|
52 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@14
|
53 *
|
Chris@14
|
54 * @return mixed The YAML converted to a PHP value
|
Chris@14
|
55 *
|
Chris@14
|
56 * @throws ParseException If the file could not be read or the YAML is not valid
|
Chris@14
|
57 */
|
Chris@14
|
58 public static function parseFile($filename, $flags = 0)
|
Chris@14
|
59 {
|
Chris@14
|
60 $yaml = new Parser();
|
Chris@14
|
61
|
Chris@14
|
62 return $yaml->parseFile($filename, $flags);
|
Chris@14
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Parses YAML into a PHP value.
|
Chris@0
|
67 *
|
Chris@0
|
68 * Usage:
|
Chris@0
|
69 * <code>
|
Chris@0
|
70 * $array = Yaml::parse(file_get_contents('config.yml'));
|
Chris@0
|
71 * print_r($array);
|
Chris@0
|
72 * </code>
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param string $input A string containing YAML
|
Chris@0
|
75 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@0
|
76 *
|
Chris@0
|
77 * @return mixed The YAML converted to a PHP value
|
Chris@0
|
78 *
|
Chris@0
|
79 * @throws ParseException If the YAML is not valid
|
Chris@0
|
80 */
|
Chris@0
|
81 public static function parse($input, $flags = 0)
|
Chris@0
|
82 {
|
Chris@0
|
83 if (is_bool($flags)) {
|
Chris@14
|
84 @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
|
85
|
Chris@0
|
86 if ($flags) {
|
Chris@0
|
87 $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
88 } else {
|
Chris@0
|
89 $flags = 0;
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 if (func_num_args() >= 3) {
|
Chris@14
|
94 @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
|
95
|
Chris@0
|
96 if (func_get_arg(2)) {
|
Chris@0
|
97 $flags |= self::PARSE_OBJECT;
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 if (func_num_args() >= 4) {
|
Chris@14
|
102 @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
|
103
|
Chris@0
|
104 if (func_get_arg(3)) {
|
Chris@0
|
105 $flags |= self::PARSE_OBJECT_FOR_MAP;
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 $yaml = new Parser();
|
Chris@0
|
110
|
Chris@0
|
111 return $yaml->parse($input, $flags);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * Dumps a PHP value to a YAML string.
|
Chris@0
|
116 *
|
Chris@0
|
117 * The dump method, when supplied with an array, will do its best
|
Chris@0
|
118 * to convert the array into friendly YAML.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @param mixed $input The PHP value
|
Chris@0
|
121 * @param int $inline The level where you switch to inline YAML
|
Chris@0
|
122 * @param int $indent The amount of spaces to use for indentation of nested nodes
|
Chris@0
|
123 * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
124 *
|
Chris@0
|
125 * @return string A YAML string representing the original PHP value
|
Chris@0
|
126 */
|
Chris@0
|
127 public static function dump($input, $inline = 2, $indent = 4, $flags = 0)
|
Chris@0
|
128 {
|
Chris@0
|
129 if (is_bool($flags)) {
|
Chris@14
|
130 @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
|
131
|
Chris@0
|
132 if ($flags) {
|
Chris@0
|
133 $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
134 } else {
|
Chris@0
|
135 $flags = 0;
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 if (func_num_args() >= 5) {
|
Chris@14
|
140 @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
|
141
|
Chris@0
|
142 if (func_get_arg(4)) {
|
Chris@0
|
143 $flags |= self::DUMP_OBJECT;
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 $yaml = new Dumper($indent);
|
Chris@0
|
148
|
Chris@0
|
149 return $yaml->dump($input, $inline, 0, $flags);
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|