-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathYaml.php
More file actions
187 lines (149 loc) · 4.62 KB
/
Yaml.php
File metadata and controls
187 lines (149 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Statamic\Yaml;
use Exception;
use ReflectionProperty;
use Statamic\Facades\File;
use Statamic\Facades\Pattern;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Yaml\ParseException as StatamicParseException;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
class Yaml
{
protected $file;
protected $yaml;
public function __construct(SymfonyYaml $yaml)
{
$this->yaml = $yaml;
}
public function file($file)
{
$this->file = $file;
return $this;
}
/**
* Parse a string of raw YAML into an array.
*
* @param null|string $str The YAML string
* @return array
*/
public function parse($str = null)
{
$originalStr = $str;
if (func_num_args() === 0) {
throw_if(! $this->file, new Exception('Cannot parse YAML without a file or string.'));
$str = File::get($this->file);
}
if (empty($str)) {
$this->file = null;
return [];
}
$content = null;
if (Pattern::startsWith($str, '---')) {
$split = preg_split("/\n---/", $str, 2, PREG_SPLIT_NO_EMPTY);
$str = $split[0];
if (empty($content = ltrim(Arr::get($split, 1, '')))) {
$content = null;
}
}
try {
$yaml = $this->yaml->parse($str);
} catch (\Exception $e) {
throw $this->viewException($e, $str);
}
$this->validateString($yaml, $str);
$this->validateDocumentContent($yaml, $content, $originalStr);
$this->file = null;
return isset($content)
? $yaml + ['content' => $content]
: $yaml;
}
/**
* Dump some YAML.
*
* @param array $data
* @param string|bool $content
* @return string
*/
public function dump($data, $content = null)
{
if (! is_null($content)) {
if (is_string($content)) {
return $this->dumpFrontMatter($data, $content);
}
$data['content'] = $content;
}
return $this->yaml->dump($data, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
}
/**
* Dump some YAML with fenced front-matter.
*
* @param array $data
* @param string|bool $content
* @return string
*/
public function dumpFrontMatter($data, $content = null)
{
if (! is_null($content) && ! is_string($content)) {
$data['content'] = $content;
$content = '';
}
return '---'.PHP_EOL.rtrim($this->dump($data)).PHP_EOL.'---'.PHP_EOL.$content;
}
protected function viewException($e, $str, $line = null)
{
if ($this->file && File::exists($this->file)) {
$path = $this->file;
} else {
$path = $this->createTemporaryExceptionFile($str, $this->file);
}
$args = [
$e->getMessage(), 0, 1, $path,
$line ?? $e->getParsedLine(),
$e,
];
$exception = new StatamicParseException(...$args);
$trace = $exception->getTrace();
array_unshift($trace, [
'file' => $e->getFile(),
'line' => $e->getLine(),
'class' => StatamicParseException::class,
'args' => $args,
]);
$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setValue($exception, $trace);
return $exception;
}
protected function createTemporaryExceptionFile($string, $path = null)
{
$path = storage_path('statamic/tmp/yaml/'.($path ?? md5($string)));
File::put($path, $string);
app()->terminating(function () use ($path) {
File::delete($path);
});
return $path;
}
protected function validateDocumentContent($yaml, $content, $str)
{
if (! $content || ! isset($yaml['content'])) {
return;
}
$e = new StatamicParseException('You cannot have a YAML variable named "content" while document content is present');
foreach (collect(explode("\n", $str))->reverse() as $i => $text) {
if ($text === '---') {
$line = $i + 2;
break;
}
}
throw $this->viewException($e, $str, $line);
}
protected function validateString($parsed, $string)
{
if (! is_string($parsed)) {
return;
}
$snippet = Str::before($string, "\n");
$exception = new \Exception("Unable to parse (near \"$snippet\").");
throw $this->viewException($exception, $string, 1);
}
}