|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Template |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Template is a template engine that has taken a lot of inspiration from Django. |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine Template |
|
11
|
|
|
* Copyright (c) 2014 Guz Alexander, http://guzalexander.com |
|
12
|
|
|
* Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com |
|
13
|
|
|
* Copyright (c) 2006 Mateo Murphy |
|
14
|
|
|
* |
|
15
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
16
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
17
|
|
|
* in the Software without restriction, including without limitation the rights |
|
18
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
19
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
20
|
|
|
* furnished to do so, subject to the following conditions: |
|
21
|
|
|
* |
|
22
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
23
|
|
|
* copies or substantial portions of the Software. |
|
24
|
|
|
* |
|
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
31
|
|
|
* SOFTWARE. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @file Variable.php |
|
36
|
|
|
* |
|
37
|
|
|
* The Template Variable class |
|
38
|
|
|
* |
|
39
|
|
|
* @package Platine\Template\Parser |
|
40
|
|
|
* @author Platine Developers Team |
|
41
|
|
|
* @copyright Copyright (c) 2020 |
|
42
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
43
|
|
|
* @link https://www.platine-php.com |
|
44
|
|
|
* @version 1.0.0 |
|
45
|
|
|
* @filesource |
|
46
|
|
|
*/ |
|
47
|
|
|
|
|
48
|
|
|
declare(strict_types=1); |
|
49
|
|
|
|
|
50
|
|
|
namespace Platine\Template\Parser; |
|
51
|
|
|
|
|
52
|
|
|
use Platine\Template\Util\Helper; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @class Variable |
|
56
|
|
|
* @package Platine\Template\Parser |
|
57
|
|
|
*/ |
|
58
|
|
|
class Variable |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* The variable markup |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected string $markup; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The filters to execute on the variable |
|
68
|
|
|
* @var array<int, array<int, mixed>> |
|
69
|
|
|
*/ |
|
70
|
|
|
protected array $filters = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The name of the variable |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected string $name; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The parser instance |
|
80
|
|
|
* @var Parser |
|
81
|
|
|
*/ |
|
82
|
|
|
protected Parser $parser; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create new instance |
|
86
|
|
|
* @param string $markup |
|
87
|
|
|
* @param Parser $parser |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __construct(string $markup, Parser $parser) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->markup = $markup; |
|
92
|
|
|
$this->parser = $parser; |
|
93
|
|
|
|
|
94
|
|
|
$filterSeparatorRegex = '/' |
|
95
|
|
|
. Token::FILTER_SEPARATOR |
|
96
|
|
|
. '\s*(.*)/m'; |
|
97
|
|
|
$syntaxRegex = '/(' |
|
98
|
|
|
. Token::QUOTED_FRAGMENT |
|
99
|
|
|
. ')(.*)/m'; |
|
100
|
|
|
$filterRegex = '/(?:\s+|' |
|
101
|
|
|
. Token::QUOTED_FRAGMENT |
|
102
|
|
|
. '|' |
|
103
|
|
|
. Token::FILTER_METHOD_ARGS_SEPARATOR |
|
104
|
|
|
. ')+/'; |
|
105
|
|
|
$filterArgumentsRegex = '/(?:' |
|
106
|
|
|
. Token::FILTER_NAME_ARG_SEPARATOR |
|
107
|
|
|
. '|' |
|
108
|
|
|
. Token::FILTER_METHOD_ARGS_SEPARATOR |
|
109
|
|
|
. ')\s*((?:\w+\s*\:\s*)?' |
|
110
|
|
|
. Token::QUOTED_FRAGMENT |
|
111
|
|
|
. ')/'; |
|
112
|
|
|
|
|
113
|
|
|
$lexerFilterSeparator = new Lexer($filterSeparatorRegex); |
|
114
|
|
|
$lexerSyntax = new Lexer($syntaxRegex); |
|
115
|
|
|
$lexerFilter = new Lexer($filterRegex); |
|
116
|
|
|
$lexerFilterArguments = new Lexer($filterArgumentsRegex); |
|
117
|
|
|
|
|
118
|
|
|
$this->filters = []; |
|
119
|
|
|
if ($lexerSyntax->match($markup)) { |
|
120
|
|
|
$nameMarkup = $lexerSyntax->getStringMatch(1); |
|
121
|
|
|
$this->name = $nameMarkup; |
|
122
|
|
|
$filterMarkup = $lexerSyntax->getStringMatch(2); |
|
123
|
|
|
|
|
124
|
|
|
if ($lexerFilterSeparator->match($filterMarkup)) { |
|
125
|
|
|
$lexerFilter->matchAll($lexerFilterSeparator->getStringMatch(1)); |
|
126
|
|
|
|
|
127
|
|
|
foreach ($lexerFilter->getArrayMatch(0) as $filter) { |
|
128
|
|
|
$filter = trim($filter); |
|
129
|
|
|
$matches = []; |
|
130
|
|
|
if (preg_match('/\w+/', $filter, $matches)) { |
|
131
|
|
|
$filterName = $matches[0]; |
|
132
|
|
|
$lexerFilterArguments->matchAll($filter); |
|
133
|
|
|
|
|
134
|
|
|
$matches = Helper::arrayFlatten($lexerFilterArguments->getArrayMatch(1)); |
|
135
|
|
|
$this->filters[] = $this->parseFilterExpressions($filterName, $matches); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($this->parser->getConfig()->get('auto_escape')) { |
|
142
|
|
|
// if auto_escape is enabled, and |
|
143
|
|
|
// - there's no raw filter, and |
|
144
|
|
|
// - no escape filter |
|
145
|
|
|
// - no other standard html-adding filter |
|
146
|
|
|
// then |
|
147
|
|
|
// - add a mandatory escape filter |
|
148
|
|
|
$addEscapeFilter = true; |
|
149
|
|
|
|
|
150
|
|
|
foreach ($this->filters as $filter) { |
|
151
|
|
|
// with empty filters set we would just move along |
|
152
|
|
|
if (in_array($filter[0], ['escape', 'raw', 'nl2br', 'escape_once'])) { |
|
153
|
|
|
// if we have any raw-like filter, stop |
|
154
|
|
|
$addEscapeFilter = false; |
|
155
|
|
|
break; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ($addEscapeFilter) { |
|
160
|
|
|
$this->filters[] = ['escape', []]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return the name of variable |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getName(): string |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->name; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return the list of filter |
|
176
|
|
|
* @return array<int, array<int, mixed>|string> |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getFilters(): array |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->filters; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Renders the variable with the data in the context |
|
185
|
|
|
* @param Context $context |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
|
|
public function render(Context $context): mixed |
|
189
|
|
|
{ |
|
190
|
|
|
$output = $context->get($this->name); |
|
191
|
|
|
foreach ($this->filters as $filter) { |
|
192
|
|
|
list($filterName, $filterArgKeys) = $filter; |
|
193
|
|
|
|
|
194
|
|
|
$filterArgValues = []; |
|
195
|
|
|
$keywordArgValues = []; |
|
196
|
|
|
|
|
197
|
|
|
foreach ($filterArgKeys as $argKey) { |
|
198
|
|
|
if (is_array($argKey)) { |
|
199
|
|
|
foreach ($argKey as $keywordArgName => $keywordArgKey) { |
|
200
|
|
|
$keywordArgValues[$keywordArgName] = $context->get($keywordArgKey); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$filterArgValues[] = $keywordArgValues; |
|
204
|
|
|
} else { |
|
205
|
|
|
$filterArgValues[] = $context->get($argKey); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$output = $context->invokeFilter( |
|
210
|
|
|
$filterName, |
|
211
|
|
|
$output, |
|
212
|
|
|
$filterArgValues |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $output; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Parse filter expression |
|
221
|
|
|
* @param string $filterName |
|
222
|
|
|
* @param array<int, mixed> $args |
|
223
|
|
|
* @return array<int, array<int, mixed>|string> |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function parseFilterExpressions(string $filterName, array $args): array |
|
226
|
|
|
{ |
|
227
|
|
|
$filterArgs = []; |
|
228
|
|
|
$keywordArgs = []; |
|
229
|
|
|
$lexerTagAttr = new Lexer( |
|
230
|
|
|
'/\A' |
|
231
|
|
|
. trim(Token::TAG_ATTRIBUTES, '/') |
|
232
|
|
|
. '\z/' |
|
233
|
|
|
); |
|
234
|
|
|
|
|
235
|
|
|
foreach ($args as $arg) { |
|
236
|
|
|
if ($lexerTagAttr->match($arg)) { |
|
237
|
|
|
$keywordArgs[$lexerTagAttr->getStringMatch(1)] = $lexerTagAttr->getStringMatch(2); |
|
238
|
|
|
} else { |
|
239
|
|
|
$filterArgs[] = $arg; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if (count($keywordArgs) > 0) { |
|
244
|
|
|
$filterArgs[] = $keywordArgs; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return [$filterName, $filterArgs]; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|