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 /**
|
Chris@0
|
15 * Dumper dumps PHP variables to YAML strings.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class Dumper
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The amount of spaces to use for indentation of nested nodes.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var int
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $indentation;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @param int $indentation
|
Chris@0
|
30 */
|
Chris@0
|
31 public function __construct($indentation = 4)
|
Chris@0
|
32 {
|
Chris@0
|
33 if ($indentation < 1) {
|
Chris@0
|
34 throw new \InvalidArgumentException('The indentation must be greater than zero.');
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 $this->indentation = $indentation;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Sets the indentation.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param int $num The amount of spaces to use for indentation of nested nodes
|
Chris@12
|
44 *
|
Chris@12
|
45 * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
|
Chris@0
|
46 */
|
Chris@0
|
47 public function setIndentation($num)
|
Chris@0
|
48 {
|
Chris@0
|
49 @trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED);
|
Chris@0
|
50
|
Chris@0
|
51 $this->indentation = (int) $num;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Dumps a PHP value to YAML.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param mixed $input The PHP value
|
Chris@0
|
58 * @param int $inline The level where you switch to inline YAML
|
Chris@0
|
59 * @param int $indent The level of indentation (used internally)
|
Chris@0
|
60 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return string The YAML representation of the PHP value
|
Chris@0
|
63 */
|
Chris@0
|
64 public function dump($input, $inline = 0, $indent = 0, $flags = 0)
|
Chris@0
|
65 {
|
Chris@0
|
66 if (is_bool($flags)) {
|
Chris@0
|
67 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
68
|
Chris@0
|
69 if ($flags) {
|
Chris@0
|
70 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
71 } else {
|
Chris@0
|
72 $flags = 0;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 if (func_num_args() >= 5) {
|
Chris@0
|
77 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
78
|
Chris@0
|
79 if (func_get_arg(4)) {
|
Chris@0
|
80 $flags |= Yaml::DUMP_OBJECT;
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 $output = '';
|
Chris@0
|
85 $prefix = $indent ? str_repeat(' ', $indent) : '';
|
Chris@12
|
86 $dumpObjectAsInlineMap = true;
|
Chris@0
|
87
|
Chris@12
|
88 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
|
Chris@12
|
89 $dumpObjectAsInlineMap = empty((array) $input);
|
Chris@12
|
90 }
|
Chris@12
|
91
|
Chris@12
|
92 if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
|
Chris@0
|
93 $output .= $prefix.Inline::dump($input, $flags);
|
Chris@0
|
94 } else {
|
Chris@12
|
95 $dumpAsMap = Inline::isHash($input);
|
Chris@0
|
96
|
Chris@0
|
97 foreach ($input as $key => $value) {
|
Chris@0
|
98 if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
|
Chris@12
|
99 $output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
|
Chris@0
|
100
|
Chris@0
|
101 foreach (preg_split('/\n|\r\n/', $value) as $row) {
|
Chris@0
|
102 $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 continue;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@12
|
108 $dumpObjectAsInlineMap = true;
|
Chris@12
|
109
|
Chris@12
|
110 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
|
Chris@12
|
111 $dumpObjectAsInlineMap = empty((array) $value);
|
Chris@12
|
112 }
|
Chris@12
|
113
|
Chris@12
|
114 $willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
|
Chris@0
|
115
|
Chris@0
|
116 $output .= sprintf('%s%s%s%s',
|
Chris@0
|
117 $prefix,
|
Chris@12
|
118 $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
|
Chris@0
|
119 $willBeInlined ? ' ' : "\n",
|
Chris@0
|
120 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
|
Chris@0
|
121 ).($willBeInlined ? "\n" : '');
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 return $output;
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|