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
|
48 |
|
public function __construct( |
19
|
|
|
protected readonly Template $template, |
20
|
|
|
protected array $context, |
21
|
|
|
public readonly array $whitelist, |
22
|
|
|
public readonly bool $autoescape, |
23
|
|
|
) { |
24
|
48 |
|
} |
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
|
46 |
|
public function context(array $values = []): array |
34
|
|
|
{ |
35
|
46 |
|
return array_map( |
36
|
46 |
|
[$this, 'wrapIf'], |
37
|
46 |
|
array_merge($this->context, $values) |
38
|
46 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function add(string $key, mixed $value): mixed |
42
|
|
|
{ |
43
|
1 |
|
$this->context[$key] = $value; |
44
|
|
|
|
45
|
1 |
|
return Wrapper::wrap($value); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
public function e( |
49
|
|
|
string|Value $value, |
50
|
|
|
int $flags = self::ESCAPE_FLAGS, |
51
|
|
|
string $encoding = self::ESCAPE_ENCODING, |
52
|
|
|
): string { |
53
|
3 |
|
if ($value instanceof Value) { |
54
|
1 |
|
return htmlspecialchars((string)$value->unwrap(), $flags, $encoding); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return htmlspecialchars($value, $flags, $encoding); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function escape( |
61
|
|
|
string|Value $value, |
62
|
|
|
int $flags = self::ESCAPE_FLAGS, |
63
|
|
|
string $encoding = self::ESCAPE_ENCODING, |
64
|
|
|
): string { |
65
|
2 |
|
return $this->e($value, $flags, $encoding); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function clean( |
69
|
|
|
string $value, |
70
|
|
|
HtmlSanitizerConfig $config = null, |
71
|
|
|
bool $removeEmptyLines = true, |
72
|
|
|
): string { |
73
|
1 |
|
return Sanitizer::clean($value, $config, $removeEmptyLines); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public function url(string|Value $value): string |
77
|
|
|
{ |
78
|
2 |
|
return Url::clean($value instanceof Value ? (string)$value->unwrap() : $value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @psalm-param non-empty-string $path |
83
|
|
|
*/ |
84
|
13 |
|
public function layout(string $path, ?array $context = null): void |
85
|
|
|
{ |
86
|
13 |
|
$this->template->setLayout(new LayoutValue($path, $context)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Includes another template into the current template. |
91
|
|
|
* |
92
|
|
|
* If no context is passed it shares the context of the calling template. |
93
|
|
|
* |
94
|
|
|
* @psalm-param non-empty-string $path |
95
|
|
|
*/ |
96
|
1 |
|
public function insert(string $path, array $context = []): void |
97
|
|
|
{ |
98
|
1 |
|
$path = $this->template->engine->getFile($path); |
99
|
1 |
|
$template = new Template( |
100
|
1 |
|
$path, |
101
|
1 |
|
sections: $this->template->sections, |
102
|
1 |
|
engine: $this->template->engine, |
103
|
1 |
|
); |
104
|
|
|
|
105
|
1 |
|
echo $template->render( |
106
|
1 |
|
$this->context($context), |
107
|
1 |
|
$this->whitelist, |
108
|
1 |
|
$this->autoescape |
109
|
1 |
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
public function begin(string $name): void |
113
|
|
|
{ |
114
|
3 |
|
$this->template->sections->begin($name); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
public function append(string $name): void |
118
|
|
|
{ |
119
|
2 |
|
$this->template->sections->append($name); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
public function prepend(string $name): void |
123
|
|
|
{ |
124
|
2 |
|
$this->template->sections->prepend($name); |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
public function end(): void |
128
|
|
|
{ |
129
|
4 |
|
$this->template->sections->end(); |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
public function section(string $name, string $default = ''): string |
133
|
|
|
{ |
134
|
4 |
|
if (func_num_args() > 1) { |
135
|
2 |
|
return $this->template->sections->getOr($name, $default); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
return $this->template->sections->get($name); |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
public function has(string $name): bool |
142
|
|
|
{ |
143
|
2 |
|
return $this->template->sections->has($name); |
144
|
|
|
} |
145
|
|
|
|
146
|
35 |
|
protected function wrapIf(mixed $value): mixed |
147
|
|
|
{ |
148
|
35 |
|
if ($value instanceof ValueInterface) { |
149
|
9 |
|
return $value; |
150
|
|
|
} |
151
|
|
|
|
152
|
35 |
|
if (is_object($value)) { |
153
|
14 |
|
foreach ($this->whitelist as $whitelisted) { |
154
|
|
|
if ( |
155
|
2 |
|
$value::class === $whitelisted |
156
|
2 |
|
|| is_subclass_of($value::class, $whitelisted) |
157
|
|
|
) { |
158
|
2 |
|
return $value; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
35 |
|
return Wrapper::wrap($value); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths