|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Conia\Boiler; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; |
|
8
|
|
|
|
|
9
|
|
|
/** @psalm-api */ |
|
10
|
|
|
class TemplateContext |
|
11
|
|
|
{ |
|
12
|
|
|
private const ESCAPE_FLAGS = ENT_QUOTES | ENT_SUBSTITUTE; |
|
13
|
|
|
private const ESCAPE_ENCODING = 'UTF-8'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @psalm-param list<class-string> $whitelist |
|
17
|
|
|
*/ |
|
18
|
49 |
|
public function __construct( |
|
19
|
|
|
protected readonly Template $template, |
|
20
|
|
|
public readonly array $context, |
|
21
|
|
|
public readonly array $whitelist, |
|
22
|
|
|
public readonly bool $autoescape, |
|
23
|
|
|
) { |
|
24
|
49 |
|
} |
|
25
|
|
|
|
|
26
|
3 |
|
public function __call(string $name, array $args): mixed |
|
27
|
|
|
{ |
|
28
|
3 |
|
$callable = $this->template->getMethods()->get($name); |
|
29
|
|
|
|
|
30
|
2 |
|
return $callable(...$args); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
47 |
|
public function context(array $values = []): array |
|
34
|
|
|
{ |
|
35
|
47 |
|
return array_map( |
|
36
|
47 |
|
function ($value): mixed { |
|
37
|
37 |
|
if (is_object($value)) { |
|
38
|
20 |
|
foreach ($this->whitelist as $whitelisted) { |
|
39
|
|
|
if ( |
|
40
|
3 |
|
$value::class === $whitelisted |
|
41
|
3 |
|
|| is_subclass_of($value::class, $whitelisted) |
|
42
|
|
|
) { |
|
43
|
3 |
|
return $value; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
37 |
|
if (is_scalar($value) && !is_string($value)) { |
|
49
|
1 |
|
return $value; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
37 |
|
if (is_null($value)) { |
|
53
|
1 |
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
37 |
|
return Wrapper::wrap($value); |
|
57
|
47 |
|
}, |
|
58
|
47 |
|
array_merge($this->context, $values) |
|
59
|
47 |
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function e( |
|
63
|
|
|
string|Value $value, |
|
64
|
|
|
int $flags = self::ESCAPE_FLAGS, |
|
65
|
|
|
string $encoding = self::ESCAPE_ENCODING, |
|
66
|
|
|
): string { |
|
67
|
3 |
|
if ($value instanceof Value) { |
|
68
|
1 |
|
return htmlspecialchars((string)$value->unwrap(), $flags, $encoding); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return htmlspecialchars($value, $flags, $encoding); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function escape( |
|
75
|
|
|
string|Value $value, |
|
76
|
|
|
int $flags = self::ESCAPE_FLAGS, |
|
77
|
|
|
string $encoding = self::ESCAPE_ENCODING, |
|
78
|
|
|
): string { |
|
79
|
2 |
|
return $this->e($value, $flags, $encoding); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function clean( |
|
83
|
|
|
string $value, |
|
84
|
|
|
HtmlSanitizerConfig $config = null, |
|
85
|
|
|
bool $removeEmptyLines = true, |
|
86
|
|
|
): string { |
|
87
|
1 |
|
return Sanitizer::clean($value, $config, $removeEmptyLines); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function url(string|Value $value): string |
|
91
|
|
|
{ |
|
92
|
2 |
|
return Url::clean($value instanceof Value ? (string)$value->unwrap() : $value); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @psalm-param non-empty-string $path |
|
97
|
|
|
*/ |
|
98
|
13 |
|
public function layout(string $path, ?array $context = null): void |
|
99
|
|
|
{ |
|
100
|
13 |
|
$this->template->setLayout(new LayoutValue($path, $context)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Includes another template into the current template. |
|
105
|
|
|
* |
|
106
|
|
|
* If no context is passed it shares the context of the calling template. |
|
107
|
|
|
* |
|
108
|
|
|
* @psalm-param non-empty-string $path |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function insert(string $path, array $context = []): void |
|
111
|
|
|
{ |
|
112
|
1 |
|
$path = $this->template->engine->getFile($path); |
|
113
|
1 |
|
$template = new Template( |
|
114
|
1 |
|
$path, |
|
115
|
1 |
|
sections: $this->template->sections, |
|
116
|
1 |
|
engine: $this->template->engine, |
|
117
|
1 |
|
); |
|
118
|
|
|
|
|
119
|
1 |
|
if (func_num_args() > 1) { |
|
120
|
1 |
|
echo $template->render($context, $this->whitelist, $this->autoescape); |
|
121
|
|
|
} else { |
|
122
|
1 |
|
echo $template->render($this->context, $this->whitelist, $this->autoescape); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
3 |
|
public function begin(string $name): void |
|
127
|
|
|
{ |
|
128
|
3 |
|
$this->template->sections->begin($name); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
public function append(string $name): void |
|
132
|
|
|
{ |
|
133
|
1 |
|
$this->template->sections->append($name); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function prepend(string $name): void |
|
137
|
|
|
{ |
|
138
|
1 |
|
$this->template->sections->prepend($name); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
public function end(): void |
|
142
|
|
|
{ |
|
143
|
3 |
|
$this->template->sections->end(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
public function section(string $name): string |
|
147
|
|
|
{ |
|
148
|
2 |
|
return $this->template->sections->get($name); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
public function has(string $name): bool |
|
152
|
|
|
{ |
|
153
|
2 |
|
return $this->template->sections->has($name); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|