|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Dallgoot\Yaml; |
|
5
|
|
|
|
|
6
|
|
|
use Dallgoot\Yaml as Y; |
|
7
|
|
|
|
|
8
|
|
|
final class Loader |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
//public |
|
11
|
|
|
public $errors = []; |
|
12
|
|
|
|
|
13
|
|
|
public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
14
|
|
|
public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
15
|
|
|
public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
16
|
|
|
public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
17
|
|
|
//privates |
|
18
|
|
|
private $content;/* @var null|string */ |
|
|
|
|
|
|
19
|
|
|
private $filePath;/* @var null|string */ |
|
|
|
|
|
|
20
|
|
|
private $debug = 0;//TODO: determine levels |
|
|
|
|
|
|
21
|
|
|
private $options = 0;/* @var int */ |
|
|
|
|
|
|
22
|
|
|
//Exceptions messages |
|
23
|
|
|
private const INVALID_VALUE = self::class.": at line %d"; |
|
24
|
|
|
private const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
|
25
|
|
|
private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
|
26
|
|
|
private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($absolutePath = null, $options = null, $debug = 0) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$this->debug = is_int($debug) ? min($debug, 3) : 1; |
|
31
|
|
|
$this->options = is_int($options) ? $options : $this->options; |
|
32
|
|
|
if (is_string($absolutePath)) { |
|
33
|
|
|
$this->load($absolutePath); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* load a file and save its content as $content |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @param string $absolutePath The absolute path of a file |
|
|
|
|
|
|
41
|
|
|
* |
|
42
|
|
|
* @throws \Exception if file don't exist OR reading failed |
|
|
|
|
|
|
43
|
|
|
* |
|
44
|
|
|
* @return self ( returns the same Loader ) |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
public function load(string $absolutePath):Loader |
|
47
|
|
|
{ |
|
48
|
|
|
$this->debug > 1 && var_dump($absolutePath); |
|
|
|
|
|
|
49
|
|
|
$this->filePath = $absolutePath; |
|
50
|
|
|
if (!file_exists($absolutePath)) { |
|
51
|
|
|
throw new \Exception(sprintf(self::EXCEPTION_NO_FILE, $absolutePath)); |
|
52
|
|
|
} |
|
53
|
|
|
$adle = "auto_detect_line_endings"; |
|
54
|
|
|
$prevADLE = ini_get($adle); |
|
55
|
|
|
!$prevADLE && ini_set($adle, "true"); |
|
56
|
|
|
$content = file($absolutePath, FILE_IGNORE_NEW_LINES); |
|
57
|
|
|
!$prevADLE && ini_set($adle, "false"); |
|
58
|
|
|
if (is_bool($content)) { |
|
59
|
|
|
throw new \Exception(sprintf(self::EXCEPTION_READ_ERROR, $absolutePath)); |
|
60
|
|
|
} |
|
61
|
|
|
$this->content = $content; |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Parse Yaml lines into an hierarchy of Node |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $strContent The Yaml string or null to parse loaded content |
|
|
|
|
|
|
69
|
|
|
* @throws \Exception if content is not available as $strContent or as $this->content (from file) |
|
|
|
|
|
|
70
|
|
|
* @throws \ParseError if any error during parsing or building |
|
|
|
|
|
|
71
|
|
|
* |
|
72
|
|
|
* @return array|YamlObject the hierarchy built an array of YamlObject or just YamlObject |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
|
|
public function parse($strContent = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$source = $this->content; |
|
77
|
|
|
if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
|
|
|
|
|
78
|
|
|
//TODO : be more permissive on $strContent values |
|
79
|
|
|
if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
|
|
|
|
|
80
|
|
|
$previous = $root = new Node(); |
|
81
|
|
|
$emptyLines = []; |
|
82
|
|
|
$specialTypes = Y\LITTERALS | Y\BLANK; |
|
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
foreach ($source as $lineNb => $lineString) { |
|
85
|
|
|
$n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
|
86
|
|
|
$deepest = $previous->getDeepestNode(); |
|
87
|
|
|
if ($deepest->type & Y\PARTIAL) { |
|
|
|
|
|
|
88
|
|
|
//TODO:verify this edge case |
|
89
|
|
|
// if ($n->type === Y\KEY && $n->indent === $previous->indent) { |
|
90
|
|
|
// throw new \ParseError(sprintf(self::INVALID_VALUE, $lineNb), 1); |
|
91
|
|
|
// } |
|
92
|
|
|
$deepest->parse($deepest->value.' '.ltrim($lineString)); |
|
93
|
|
|
} else { |
|
94
|
|
|
if (($n->type & $specialTypes) && $this->onSpecialType($n, $previous, $emptyLines)) { |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
if ($n->type & Y\SCALAR) { |
|
|
|
|
|
|
98
|
|
|
foreach ($emptyLines as $blankNode) { |
|
99
|
|
|
$blankNode->getParent()->add($blankNode); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
$emptyLines = []; |
|
103
|
|
|
switch ($n->indent <=> $previous->indent) { |
|
104
|
|
|
case -1: $previous->getParent($n->indent)->add($n);break; |
|
|
|
|
|
|
105
|
|
|
case 0: $previous->getParent()->add($n);break; |
|
|
|
|
|
|
106
|
|
|
default: |
|
|
|
|
|
|
107
|
|
|
if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
|
|
|
|
|
|
108
|
|
|
$previous->add($n); |
|
109
|
|
|
} |
|
110
|
|
|
$previous = $n; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
if ($this->debug === 2) { |
|
|
|
|
|
|
114
|
|
|
var_dump("\033[33mParsed Structure\033[0m\n", $root); |
|
|
|
|
|
|
115
|
|
|
die("Debug of root structure requested (remove debug level to suppress this)"); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
$out = Builder::buildContent($root, $this->debug); |
|
|
|
|
|
|
118
|
|
|
return $out; |
|
119
|
|
|
} catch (\Error|\Exception|\ParseError $e) { |
|
120
|
|
|
$file = basename($this->filePath); |
|
121
|
|
|
$message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb+1)."\n"; |
|
|
|
|
|
|
122
|
|
|
if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
|
123
|
|
|
trigger_error($message, E_USER_WARNING); |
|
124
|
|
|
$this->errors[] = $message; |
|
125
|
|
|
var_dump($root); |
|
126
|
|
|
} |
|
127
|
|
|
throw new \Exception($message, 3); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
private function onSpecialType(&$n, &$previous, &$emptyLines):bool |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$deepest = $previous->getDeepestNode(); |
|
134
|
|
|
if ($n->type & Y\KEY && $previous->type & Y\ITEM) { |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
if ($n->type & Y\LITTERALS) { |
|
138
|
|
|
if ($deepest->type & Y\KEY && is_null($deepest->value)) { |
|
139
|
|
|
$deepest->add($n); |
|
140
|
|
|
$previous = $n; |
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
if ($n->type & Y\BLANK) { |
|
|
|
|
|
|
145
|
|
|
if ($previous->type & Y\SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
|
|
|
|
|
146
|
|
|
if ($deepest->type & Y\LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private function onDeepestType(&$n, &$previous, $lineString):bool |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$deepest = $previous->getDeepestNode(); |
|
155
|
|
|
// if ($deepest->type & Y\LITTERALS) { |
|
156
|
|
|
// $n->value = trim($lineString);//fall through |
|
157
|
|
|
// } |
|
158
|
|
|
if (($n->type & Y\SCALAR) && ($deepest->type & (Y\LITTERALS | Y\REF_DEF | Y\SET_VALUE)) && is_null($deepest->value)) { |
|
|
|
|
|
|
159
|
|
|
$previous = $deepest; |
|
160
|
|
|
} |
|
161
|
|
|
if (($deepest->type & Y\TAG) && is_null($deepest->value)) { |
|
|
|
|
|
|
162
|
|
|
$previous = $deepest; |
|
163
|
|
|
} |
|
164
|
|
|
// // if ($previous->type & Y\ITEM && $n->type & Y\KEY) { |
|
165
|
|
|
// // $previous |
|
166
|
|
|
// // } |
|
167
|
|
|
if ($deepest->type & (Y\BLANK | Y\SCALAR) ) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
|
|
|
|
|
168
|
|
|
$previous = $deepest->getParent(); |
|
169
|
|
|
// // if ($n->type === Y\SCALAR && ($deepest->getParent()->type & Y\LITTERALS)) { |
|
|
|
|
|
|
170
|
|
|
// // // $deepest->type = Y\SCALAR; |
|
|
|
|
|
|
171
|
|
|
// // // $deepest->value .= "\n".$n->value; |
|
|
|
|
|
|
172
|
|
|
// // // ->add($n); |
|
|
|
|
|
|
173
|
|
|
// // // return true; |
|
|
|
|
|
|
174
|
|
|
// // } else { |
|
|
|
|
|
|
175
|
|
|
// // if ($previous->type & (Y\ITEM | Y\SET_KEY)) { |
|
|
|
|
|
|
176
|
|
|
// // $parent = $deepest->getParent(); |
|
|
|
|
|
|
177
|
|
|
// // } |
|
|
|
|
|
|
178
|
|
|
// // } |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|