|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Viewer; |
|
4
|
|
|
|
|
5
|
|
|
trait Helper |
|
6
|
|
|
{ |
|
7
|
|
|
use JScriptTrait; |
|
8
|
|
|
|
|
9
|
|
|
protected string $treat = ''; |
|
10
|
|
|
protected bool $especial = true; |
|
11
|
|
|
protected array $data = []; |
|
12
|
|
|
|
|
13
|
|
|
protected function getOB(string $require, ?array $data = []): string |
|
14
|
|
|
{ |
|
15
|
|
|
$this->data = array_merge($this->data, $data); |
|
16
|
|
|
|
|
17
|
|
|
foreach($this->data as $variable => $_){ |
|
18
|
|
|
$$variable = $_; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$_ = (isset($data['_'])) ?? $data['_']; |
|
22
|
|
|
|
|
23
|
|
|
if(!file_exists($require)){ |
|
24
|
|
|
$require = basename($require); |
|
25
|
|
|
throw new \RuntimeException("Importation file does not exist: {$require}"); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
ob_start(); |
|
29
|
|
|
require($require); |
|
30
|
|
|
$response = ob_get_contents(); |
|
31
|
|
|
ob_end_clean(); |
|
32
|
|
|
|
|
33
|
|
|
return $this->treatHTML($response); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function treatHTML(string $html): string |
|
37
|
|
|
{ |
|
38
|
|
|
$arrayHtml = explode(PHP_EOL, $html); |
|
39
|
|
|
$html = []; |
|
40
|
|
|
|
|
41
|
|
|
$inScript = false; |
|
42
|
|
|
$inComment = false; |
|
43
|
|
|
|
|
44
|
|
|
foreach($arrayHtml as $index => $value){ |
|
45
|
|
|
$inScript = $this->checkInScript($inScript, $value); |
|
46
|
|
|
|
|
47
|
|
|
if($inScript){ |
|
48
|
|
|
$inComment = $this->checkCommentInScript($inComment, $value); |
|
49
|
|
|
|
|
50
|
|
|
if(!$this->checkScriptNeed($inComment, $value)){ |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$value = $this->treatScript($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$html[$index] = ltrim($value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return implode('',$html); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getVars(string $buffer, ?bool $treat = true): string |
|
64
|
|
|
{ |
|
65
|
|
|
$this->treat = ($treat) ? '' : '!!'; |
|
66
|
|
|
$this->especial = $treat; |
|
67
|
|
|
return $this->replaceVars($buffer, $this->data); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function replaceVars(string $buffer, array $vars, ?string $prefix = ''): string |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($vars as $key => $value) { |
|
73
|
|
|
switch(gettype($value)){ |
|
74
|
|
|
case 'array': |
|
75
|
|
|
$buffer = $this->replaceArray($buffer, $value, $prefix, $key); |
|
76
|
|
|
break; |
|
77
|
|
|
case 'object': |
|
78
|
|
|
$buffer = $this->replaceObject($buffer, $value, $prefix, $key); |
|
79
|
|
|
break; |
|
80
|
|
|
default: |
|
81
|
|
|
$buffer = (is_scalar($value)) ? $this->replaceValue($buffer, $value, $prefix, $key) : $buffer; |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $buffer; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function replaceValue(string $buffer, $value, ?string $prefix, string $key): string |
|
90
|
|
|
{ |
|
91
|
|
|
if(is_scalar($value)){ |
|
92
|
|
|
while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key} {$this->treat}}}")){ |
|
93
|
|
|
$buffer = str_replace("{{{$this->treat} \${$prefix}{$key} {$this->treat}}}", (($this->especial) ? htmlspecialchars($value) : $value) ,$buffer); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $buffer; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function replaceObject(string $buffer, object $obj, ?string $prefix, string $key): string |
|
101
|
|
|
{ |
|
102
|
|
|
$vars = method_exists($obj,'getVars') ? $obj->getVars() : []; |
|
103
|
|
|
$vars = array_merge($vars, get_object_vars($obj)); |
|
104
|
|
|
|
|
105
|
|
|
foreach($vars as $field => $val){ |
|
106
|
|
|
$buffer = $this->replaceValue($buffer, $val, $key.'.' , $field); |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$buffer = $this->checkArray($buffer, $obj->$field, $key.'.'.$field.'.'); |
|
110
|
|
|
$buffer = $this->checkObject($buffer, $obj->$field, $key.'.'.$field.'.', $key.'.'.$field); |
|
111
|
|
|
|
|
112
|
|
|
while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")){ |
|
113
|
|
|
$buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($obj->$field) : $obj->$field) ,$buffer); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
return $buffer; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function checkArray(string $buffer, $obj, string $prefix): string |
|
121
|
|
|
{ |
|
122
|
|
|
if(is_array($obj)){ |
|
123
|
|
|
$buffer = $this->replaceVars($buffer, $obj, $prefix); |
|
124
|
|
|
} |
|
125
|
|
|
return $buffer; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function checkObject(string $buffer, $obj, string $prefix, string $key): string |
|
129
|
|
|
{ |
|
130
|
|
|
if(is_object($obj)){ |
|
131
|
|
|
$buffer = $this->replaceObject($buffer, $obj, $prefix, $key); |
|
132
|
|
|
} |
|
133
|
|
|
return $buffer; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
protected function replaceArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
|
137
|
|
|
{ |
|
138
|
|
|
foreach($array as $field => $val){ |
|
139
|
|
|
$buffer = $this->replaceValue($buffer, $val, $key.'.', $field); |
|
140
|
|
|
|
|
141
|
|
|
while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")){ |
|
142
|
|
|
$buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($val) : $val) ,$buffer); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
return $buffer; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function removeComments(string $buffer): string |
|
149
|
|
|
{ |
|
150
|
|
|
while(strstr($buffer,'<!--')){ |
|
151
|
|
|
$comment = substr( |
|
152
|
|
|
$buffer, |
|
153
|
|
|
strpos($buffer,'<!--'), |
|
154
|
|
|
strpos(strstr($buffer,'<!--'),'-->')+3 |
|
155
|
|
|
); |
|
156
|
|
|
$buffer = str_replace($comment,'',$buffer); |
|
157
|
|
|
} |
|
158
|
|
|
return $buffer; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|