|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace TheAentMachine\Yaml; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Yaml\Inline; |
|
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Dumper dumps PHP variables to YAML strings. |
|
19
|
|
|
* |
|
20
|
|
|
* Shamelessly stolen and customized from symfony/yaml in order to add support for comments. |
|
21
|
|
|
* The code still uses the Yaml\Inline class which is marked as internal. In order to avoid any issue, |
|
22
|
|
|
* the symfony/yaml version is therefore hard-coded in composer.json. |
|
23
|
|
|
* |
|
24
|
|
|
* You can use a CommentedItem instance instead of any value in the Yaml input and a comment will be displayed |
|
25
|
|
|
* on top of the value. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Fabien Potencier <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @final |
|
30
|
|
|
*/ |
|
31
|
|
|
class Dumper |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* The amount of spaces to use for indentation of nested nodes. |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $indentation; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(int $indentation = 4) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($indentation < 1) { |
|
43
|
|
|
throw new \InvalidArgumentException('The indentation must be greater than zero.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->indentation = $indentation; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Dumps a PHP value to YAML. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $input The PHP value |
|
53
|
|
|
* @param int $inline The level where you switch to inline YAML |
|
54
|
|
|
* @param int $indent The level of indentation (used internally) |
|
55
|
|
|
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string |
|
56
|
|
|
* |
|
57
|
|
|
* @return string The YAML representation of the PHP value |
|
58
|
|
|
*/ |
|
59
|
|
|
public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string |
|
60
|
|
|
{ |
|
61
|
|
|
if ($input instanceof CommentedItem) { |
|
62
|
|
|
//$comment = $input->getComment(); |
|
63
|
|
|
$output = '# '.$input->getComment()."\n"; |
|
64
|
|
|
$input = $input->getItem(); |
|
65
|
|
|
} else { |
|
66
|
|
|
//$comment = null; |
|
67
|
|
|
$output = ''; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// $output = ''; |
|
71
|
|
|
$prefix = $indent ? str_repeat(' ', $indent) : ''; |
|
72
|
|
|
$dumpObjectAsInlineMap = true; |
|
73
|
|
|
|
|
74
|
|
|
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) { |
|
75
|
|
|
$dumpObjectAsInlineMap = empty((array) $input); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) { |
|
79
|
|
|
$output .= $prefix.Inline::dump($input, $flags); |
|
80
|
|
|
} else { |
|
81
|
|
|
$dumpAsMap = Inline::isHash($input); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($input as $key => $value) { |
|
84
|
|
|
if ($value instanceof CommentedItem) { |
|
85
|
|
|
$comment = $value->getComment(); |
|
86
|
|
|
$value = $value->getItem(); |
|
87
|
|
|
} else { |
|
88
|
|
|
$comment = null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) { |
|
92
|
|
|
// If the first line starts with a space character, the spec requires a blockIndicationIndicator |
|
93
|
|
|
// http://www.yaml.org/spec/1.2/spec.html#id2793979 |
|
94
|
|
|
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; |
|
95
|
|
|
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); |
|
96
|
|
|
|
|
97
|
|
|
$pregSplit = preg_split('/\n|\r\n/', $value); |
|
98
|
|
|
if ($pregSplit === false) { |
|
99
|
|
|
throw new \RuntimeException('An error occured in preg_split'); |
|
100
|
|
|
} |
|
101
|
|
|
foreach ($pregSplit as $row) { |
|
102
|
|
|
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$dumpObjectAsInlineMap = true; |
|
109
|
|
|
|
|
110
|
|
|
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) { |
|
111
|
|
|
$dumpObjectAsInlineMap = empty((array) $value); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value); |
|
115
|
|
|
|
|
116
|
|
|
if ($comment) { |
|
117
|
|
|
$output .= sprintf("%s# %s\n", $prefix, $comment); |
|
118
|
|
|
} |
|
119
|
|
|
$output .= sprintf( |
|
120
|
|
|
'%s%s%s%s', |
|
121
|
|
|
$prefix, |
|
122
|
|
|
$dumpAsMap ? Inline::dump($key, $flags).':' : '-', |
|
123
|
|
|
$willBeInlined ? ' ' : "\n", |
|
124
|
|
|
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags) |
|
125
|
|
|
).($willBeInlined ? "\n" : ''); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $output; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|