-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathView.php
More file actions
executable file
·342 lines (288 loc) · 8.19 KB
/
Copy pathView.php
File metadata and controls
executable file
·342 lines (288 loc) · 8.19 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace Utopia;
use Exception;
class View
{
public const FILTER_ESCAPE = 'escape';
public const FILTER_NL2P = 'nl2p';
/**
* @var self|null
*/
protected ?self $parent = null;
/**
* @var string
*/
protected string $path = '';
/**
* @var bool
*/
protected bool $rendered = false;
/**
* @var array
*/
protected array $params = [];
/**
* @var array
*/
protected array $filters = [];
/**
* Constructor
*
* You can optionally initialize the View object with a template path, although this can also be set later using the $this->setPath($path) method
*
* @param string $path
*
* @throws Exception
*/
public function __construct(string $path = '')
{
$this->setPath($path);
$this
->addFilter(self::FILTER_ESCAPE, function (string $value) {
return \htmlentities($value, ENT_QUOTES, 'UTF-8');
})
->addFilter(self::FILTER_NL2P, function (string $value) {
$paragraphs = '';
foreach (\explode("\n\n", $value) as $line) {
if (\trim($line)) {
$paragraphs .= '<p>'.$line.'</p>';
}
}
$paragraphs = \str_replace("\n", '<br />', $paragraphs);
return $paragraphs;
});
}
/**
* Set param
*
* Assign a parameter by key
*
* @param string $key
* @param mixed $value
*
* @throws Exception
*/
public function setParam(string $key, mixed $value, bool $escapeHtml = true): static
{
if (\strpos($key, '.') !== false) {
throw new Exception('$key can\'t contain a dot "." character');
}
if (is_string($value) && $escapeHtml) {
$value = \htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
$this->params[$key] = $value;
return $this;
}
/**
* Set parent View object conatining this object
*
* @param self $view
*/
public function setParent(self $view): static
{
$this->parent = $view;
return $this;
}
/**
* Return a View instance of the parent view containing this view
*
* @return self|null
*/
public function getParent(): ?self
{
if (!empty($this->parent)) {
return $this->parent;
}
return null;
}
/**
* Get param
*
* Returns an assigned parameter by its key or $default if param key doesn't exists
*
* @param string $path
* @param mixed $default (optional)
* @return mixed
*/
public function getParam(string $path, mixed $default = null): mixed
{
$path = \explode('.', $path);
$temp = $this->params;
foreach ($path as $key) {
$temp = (isset($temp[$key])) ? $temp[$key] : null;
if (null !== $temp) {
$value = $temp;
} else {
return $default;
}
}
return $value;
}
/**
* Set path
*
* Set object template path that will be used to render view output
*
* @param string $path
*
* @throws Exception
*/
public function setPath(string $path): static
{
$this->path = $path;
return $this;
}
/**
* Set rendered
*
* By enabling rendered state to true, the object will not render its template and will return an empty string instead
*
* @param bool $state
*/
public function setRendered(bool $state = true): static
{
$this->rendered = $state;
return $this;
}
/**
* Is rendered
*
* Return whether current View rendering state is set to true or false
*
* @return bool
*/
public function isRendered(): bool
{
return $this->rendered;
}
/**
* Add Filter
*
* @param string $name
* @param callable $callback
*/
public function addFilter(string $name, callable $callback): static
{
$this->filters[$name] = $callback;
return $this;
}
/**
* Output and filter value
*
* @param mixed $value
* @param string|array $filter
* @return mixed
*
* @throws Exception
*/
public function print(mixed $value, string|array $filter = ''): mixed
{
if (!empty($filter)) {
if (\is_array($filter)) {
foreach ($filter as $callback) {
if (!isset($this->filters[$callback])) {
throw new Exception('Filter "'.$callback.'" is not registered');
}
$value = $this->filters[$callback]($value);
}
} else {
if (!isset($this->filters[$filter])) {
throw new Exception('Filter "'.$filter.'" is not registered');
}
$value = $this->filters[$filter]($value);
}
}
return $value;
}
/**
* Render
*
* Render view .phtml template file if template has not been set as rendered yet using $this->setRendered(true).
* In case path is not readable throws Exception.
*
* @param bool $minify
* @return string
*
* @throws Exception
*/
public function render(bool $minify = true): string
{
if ($this->rendered) { // Don't render any template
return '';
}
\ob_start(); //Start of build
if (\is_readable($this->path)) {
/**
* Include template file
*
* @psalm-suppress UnresolvableInclude
*/
include $this->path;
} else {
\ob_end_clean();
throw new Exception('"'.$this->path.'" view template is not readable');
}
$html = \ob_get_contents();
\ob_end_clean(); //End of build
if ($minify) {
// Searching textarea and pre
\preg_match_all('#\<textarea.*\>.*\<\/textarea\>#Uis', $html, $foundTxt);
\preg_match_all('#\<pre.*\>.*\<\/pre\>#Uis', $html, $foundPre);
// replacing both with <textarea>$index</textarea> / <pre>$index</pre>
$html = \str_replace($foundTxt[0], \array_map(function ($el) {
return '<textarea>'.$el.'</textarea>';
}, \array_keys($foundTxt[0])), $html);
$html = \str_replace($foundPre[0], \array_map(function ($el) {
return '<pre>'.$el.'</pre>';
}, \array_keys($foundPre[0])), $html);
// your stuff
$search = [
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
];
$replace = [
'>',
'<',
'\\1',
];
$html = \preg_replace($search, $replace, $html);
// Replacing back with content
$html = \str_replace(\array_map(function ($el) {
return '<textarea>'.$el.'</textarea>';
}, \array_keys($foundTxt[0])), $foundTxt[0], $html);
$html = \str_replace(\array_map(function ($el) {
return '<pre>'.$el.'</pre>';
}, \array_keys($foundPre[0])), $foundPre[0], $html);
}
return $html;
}
/* View Helpers */
/**
* Exec
*
* Exec child View components
*
* @param array|self $view
* @return string
*
* @throws Exception
*/
public function exec($view): string
{
$output = '';
if (\is_array($view)) {
foreach ($view as $node) { /* @var $node self */
if ($node instanceof self) {
$node->setParent($this);
$output .= $node->render();
}
}
}
if ($view instanceof self) {
$view->setParent($this);
$output = $view->render();
}
return $output;
}
}