-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHTMLDocumentProcessor.php
More file actions
144 lines (124 loc) · 3.57 KB
/
HTMLDocumentProcessor.php
File metadata and controls
144 lines (124 loc) · 3.57 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
<?php
namespace GT\WebEngine\Logic;
use GT\Dom\HTMLDocument;
use GT\DomTemplate\ComponentExpander;
use GT\DomTemplate\PartialContent;
use GT\DomTemplate\PartialContentDirectoryNotFoundException;
use GT\DomTemplate\PartialExpander;
use GT\Routing\Assembly;
use GT\Routing\Path\DynamicPath;
use GT\WebEngine\View\HeaderFooterPartialConflictException;
class HTMLDocumentProcessor extends ViewModelProcessor {
function processDynamicPath(
HTMLDocument $model,
DynamicPath $dynamicPath,
):void {
$dynamicUri = $dynamicPath->getUrl("page/");
$dynamicUri = str_replace("/", "--", $dynamicUri);
$dynamicUri = str_replace("@", "_", $dynamicUri);
$model->body->classList->add("uri" . $dynamicUri);
$bodyDirClass = "dir";
foreach(explode("--", $dynamicUri) as $i => $pathPart) {
if($i === 0) {
continue;
}
$bodyDirClass .= "--$pathPart";
$model->body->classList->add($bodyDirClass);
}
}
function processPartialContent(
HTMLDocument $model,
?Assembly $viewAssembly = null,
):LogicAssemblyComponentList {
if($viewAssembly
&& $this->containsPartialExtends($model)
&& $this->containsHeaderOrFooterView($viewAssembly)) {
throw new HeaderFooterPartialConflictException(
"Header/footer view files cannot be combined with partial views."
);
}
$componentList = new LogicAssemblyComponentList();
try {
// TODO: Handle other model types in sub-functions.
$partial = new PartialContent(implode(DIRECTORY_SEPARATOR, [
getcwd(),
$this->componentDirectory,
]));
$componentExpander = new ComponentExpander(
$model,
$partial,
);
foreach($componentExpander->expand() as $componentElement) {
$filePath = $this->componentDirectory;
$filePath .= DIRECTORY_SEPARATOR;
$filePath .= strtolower($componentElement->tagName);
$filePath .= ".php";
if(!is_file($filePath)) {
// TODO: Log that a component has been detected but there's no HTML file to load.
continue;
}
$componentAssembly = new Assembly();
$componentAssembly->add($filePath);
$componentList->addAssemblyComponent(
$componentAssembly,
$componentElement,
);
}
}
catch(PartialContentDirectoryNotFoundException) {}
try {
$partial = new PartialContent(implode(DIRECTORY_SEPARATOR, [
getcwd(),
$this->partialDirectory,
]));
$partialExpander = new PartialExpander(
$model,
$partial,
);
$partialExpander->expand();
}
catch(PartialContentDirectoryNotFoundException) {}
return $componentList;
}
private function containsHeaderOrFooterView(Assembly $viewAssembly):bool {
foreach($viewAssembly as $viewFile) {
$fileName = pathinfo($viewFile, PATHINFO_FILENAME);
if($fileName === "_header" || $fileName === "_footer") {
return true;
}
}
return false;
}
private function containsPartialExtends(HTMLDocument $model):bool {
return $this->containsPartialExtendsInNode($model->documentElement);
}
/** @return ?array<string, array<string, string>|string> */
private function parseCommentIni(string $data):?array {
set_error_handler(
static fn() => true
);
try {
$parsed = parse_ini_string($data, true);
}
finally {
restore_error_handler();
}
return is_array($parsed)
? $parsed
: null;
}
private function containsPartialExtendsInNode(\DOMNode $node):bool {
if($node->nodeType === XML_COMMENT_NODE) {
$parsed = $this->parseCommentIni(trim($node->textContent));
if(isset($parsed["extends"])) {
return true;
}
}
foreach($node->childNodes as $childNode) {
if($this->containsPartialExtendsInNode($childNode)) {
return true;
}
}
return false;
}
}