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 ExtendsTag.php |
36
|
|
|
* |
37
|
|
|
* The "extends" Template tag class |
38
|
|
|
* |
39
|
|
|
* @package Platine\Template\Tag |
40
|
|
|
* @author Platine Developers Team |
41
|
|
|
* @copyright Copyright (c) 2020 |
42
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
43
|
|
|
* @link http://www.iacademy.cf |
44
|
|
|
* @version 1.0.0 |
45
|
|
|
* @filesource |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
declare(strict_types=1); |
49
|
|
|
|
50
|
|
|
namespace Platine\Template\Tag; |
51
|
|
|
|
52
|
|
|
use Platine\Template\Exception\ParseException; |
53
|
|
|
use Platine\Template\Parser\AbstractTag; |
54
|
|
|
use Platine\Template\Parser\Context; |
55
|
|
|
use Platine\Template\Parser\Document; |
56
|
|
|
use Platine\Template\Parser\Lexer; |
57
|
|
|
use Platine\Template\Parser\Parser; |
58
|
|
|
use Platine\Template\Parser\Token; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Class ExtendsTag |
62
|
|
|
* @package Platine\Template\Tag |
63
|
|
|
*/ |
64
|
|
|
class ExtendsTag extends AbstractTag |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The name of the template |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected string $templateName; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The Document that represents the extends template |
75
|
|
|
* @var Document |
76
|
|
|
*/ |
77
|
|
|
protected Document $document; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The source template Hash |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected string $hash; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function __construct(string $markup, &$tokens, Parser $parser) |
89
|
|
|
{ |
90
|
|
|
$lexer = new Lexer('/("[^"]+"|\'[^\']+\')?/'); |
91
|
|
|
if ($lexer->match($markup) && $lexer->isMatchNotNull(1)) { |
92
|
|
|
$this->templateName = substr( |
93
|
|
|
$lexer->getStringMatch(1), |
94
|
|
|
1, |
95
|
|
|
strlen($lexer->getStringMatch(1)) - 2 |
96
|
|
|
); |
97
|
|
|
} else { |
98
|
|
|
throw new ParseException(sprintf( |
99
|
|
|
'Syntax Error in "%s" - Valid syntax: extends "template name"', |
100
|
|
|
'extends' |
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
parent::__construct($markup, $tokens, $parser); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function parse(&$tokens): void |
111
|
|
|
{ |
112
|
|
|
$source = $this->parser->getLoader()->read($this->templateName); |
113
|
|
|
$mainTokens = $this->parser->tokenize($source); |
114
|
|
|
|
115
|
|
|
$lexerExtends = new Lexer( |
116
|
|
|
'/^' . Token::BLOCK_OPEN |
117
|
|
|
. '\s*extends (.*)?' |
118
|
|
|
. Token::BLOCK_CLOSE . '$/' |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$match = null; |
122
|
|
|
foreach ($mainTokens as $mainToken) { |
123
|
|
|
if ($lexerExtends->match($mainToken)) { |
124
|
|
|
$match = $lexerExtends->getStringMatch(1); |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$result = []; |
130
|
|
|
if ($match !== null) { |
131
|
|
|
$result = array_merge($mainTokens, $tokens); |
132
|
|
|
} else { |
133
|
|
|
$childrenTokens = $this->findBlocks($tokens); |
134
|
|
|
|
135
|
|
|
$lexerBlockStart = new Lexer( |
136
|
|
|
'/^' . Token::BLOCK_OPEN |
137
|
|
|
. '\s*block (\w+)\s*(.*)?' |
138
|
|
|
. Token::BLOCK_CLOSE . '$/' |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$lexerBlockEnd = new Lexer( |
142
|
|
|
'/^' . Token::BLOCK_OPEN |
143
|
|
|
. '\s*endblock\s*?' |
144
|
|
|
. Token::BLOCK_CLOSE . '$/' |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$name = null; |
|
|
|
|
148
|
|
|
$keep = false; |
149
|
|
|
$count = count($mainTokens); |
150
|
|
|
for ($i = 0; $i < $count; $i++) { |
151
|
|
|
if ($lexerBlockStart->match($mainTokens[$i])) { |
152
|
|
|
$name = $lexerBlockStart->getStringMatch(1); |
153
|
|
|
if (isset($childrenTokens[$name])) { |
154
|
|
|
$keep = true; |
155
|
|
|
array_push($result, $mainTokens[$i]); |
156
|
|
|
foreach ($childrenTokens[$name] as $item) { |
157
|
|
|
array_push($result, $item); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!$keep) { |
163
|
|
|
array_push($result, $mainTokens[$i]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($lexerBlockEnd->match($mainTokens[$i]) && $keep) { |
167
|
|
|
$keep = false; |
168
|
|
|
array_push($result, $mainTokens[$i]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$cache = $this->parser->getTemplate()->getCache(); |
174
|
|
|
|
175
|
|
|
$this->hash = md5($this->templateName); |
176
|
|
|
|
177
|
|
|
/** @var Document|false $document */ |
178
|
|
|
$document = $cache->read($this->hash, true); |
179
|
|
|
|
180
|
|
|
if ($document === false || ($document->hasIncludes())) { |
181
|
|
|
$this->document = new Document($result, $this->parser); |
182
|
|
|
$cache->write($this->hash, $this->document, true); |
183
|
|
|
} else { |
184
|
|
|
$this->document = $document; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function render(Context $context): string |
193
|
|
|
{ |
194
|
|
|
$context->push(); |
195
|
|
|
$result = $this->document->render($context); |
196
|
|
|
$context->pop(); |
197
|
|
|
|
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check for cached includes; |
203
|
|
|
* if there are - do not use cache |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function hasIncludes(): bool |
207
|
|
|
{ |
208
|
|
|
if ($this->document->hasIncludes()) { |
209
|
|
|
return true; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$cache = $this->parser->getTemplate()->getCache(); |
213
|
|
|
$hash = md5($this->templateName); |
214
|
|
|
if ($cache->exists($hash) && $this->hash === $hash) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Find all defined blocks |
223
|
|
|
* @param array<int, mixed> $tokens |
224
|
|
|
* @return array<string, array<string>> |
225
|
|
|
*/ |
226
|
|
|
protected function findBlocks(array $tokens): array |
227
|
|
|
{ |
228
|
|
|
$lexerBlockStart = new Lexer( |
229
|
|
|
'/^' . Token::BLOCK_OPEN |
230
|
|
|
. '\s*block (\w+)\s*(.*)?' |
231
|
|
|
. Token::BLOCK_CLOSE . '$/' |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$lexerBlockEnd = new Lexer( |
235
|
|
|
'/^' . Token::BLOCK_OPEN |
236
|
|
|
. '\s*endblock\s*?' |
237
|
|
|
. Token::BLOCK_CLOSE . '$/' |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$result = []; |
241
|
|
|
$name = null; |
242
|
|
|
foreach ($tokens as $token) { |
243
|
|
|
if ($lexerBlockStart->match($token)) { |
244
|
|
|
$name = $lexerBlockStart->getStringMatch(1); |
245
|
|
|
$result[$name] = []; |
246
|
|
|
} elseif ($lexerBlockEnd->match($token)) { |
247
|
|
|
$name = null; |
248
|
|
|
} else { |
249
|
|
|
if ($name !== null) { |
250
|
|
|
array_push($result[$name], $token); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $result; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|