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