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 Parser.php |
36
|
|
|
* |
37
|
|
|
* The Template parser 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 http://www.iacademy.cf |
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\Configuration; |
53
|
|
|
use Platine\Template\Loader\LoaderInterface; |
54
|
|
|
use Platine\Template\Template; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Class Parser |
58
|
|
|
* @package Platine\Template\Parser |
59
|
|
|
*/ |
60
|
|
|
class Parser |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* The template instance |
64
|
|
|
* @var Template |
65
|
|
|
*/ |
66
|
|
|
protected Template $template; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The root of the node tree |
70
|
|
|
* @var Document |
71
|
|
|
*/ |
72
|
|
|
protected Document $root; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create new instance |
76
|
|
|
* @param Template $template |
77
|
|
|
*/ |
78
|
|
|
public function __construct(Template $template) |
79
|
|
|
{ |
80
|
|
|
$this->template = $template; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return the template instance |
85
|
|
|
* @return Template |
86
|
|
|
*/ |
87
|
|
|
public function getTemplate(): Template |
88
|
|
|
{ |
89
|
|
|
return $this->template; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the Loader instance |
94
|
|
|
* @return LoaderInterface |
95
|
|
|
*/ |
96
|
|
|
public function getLoader(): LoaderInterface |
97
|
|
|
{ |
98
|
|
|
return $this->template->getLoader(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the configuration instance |
103
|
|
|
* @return Configuration |
104
|
|
|
*/ |
105
|
|
|
public function getConfig(): Configuration |
106
|
|
|
{ |
107
|
|
|
return $this->template->getConfig(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the document instance |
112
|
|
|
* @return Document |
113
|
|
|
*/ |
114
|
|
|
public function getRoot(): Document |
115
|
|
|
{ |
116
|
|
|
return $this->root; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parse the template source and use the cached |
121
|
|
|
* content if is available |
122
|
|
|
* @param string $name |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function parse(string $name): self |
126
|
|
|
{ |
127
|
|
|
$hash = md5($name); |
128
|
|
|
|
129
|
|
|
/** @var Document|false $root */ |
130
|
|
|
$root = $this->template->getCache()->read($hash, true); |
131
|
|
|
|
132
|
|
|
if ($root === false || ($root->hasIncludes())) { |
133
|
|
|
$content = $this->getLoader()->read($name); |
134
|
|
|
$tokens = $this->tokenize($content); |
135
|
|
|
|
136
|
|
|
$this->root = new Document($tokens, $this); |
137
|
|
|
$this->template->getCache()->write($hash, $this->root, true); |
138
|
|
|
} else { |
139
|
|
|
$this->root = $root; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Render the template |
147
|
|
|
* @param string $name the template name |
148
|
|
|
* @param Context $context |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function render(string $name, Context $context): string |
152
|
|
|
{ |
153
|
|
|
return $this->parse($name) |
154
|
|
|
->getRoot() |
155
|
|
|
->render($context); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Render the template using string content |
160
|
|
|
* @param string $content the template name |
161
|
|
|
* @param Context $context |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function renderString(string $content, Context $context): string |
165
|
|
|
{ |
166
|
|
|
$tokens = $this->tokenize($content); |
167
|
|
|
$this->root = new Document($tokens, $this); |
168
|
|
|
|
169
|
|
|
return $this->getRoot() |
170
|
|
|
->render($context); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Parser the given source string to tokens |
175
|
|
|
* @param string $source |
176
|
|
|
* @return array<int, mixed> |
177
|
|
|
*/ |
178
|
|
|
public function tokenize(string $source): array |
179
|
|
|
{ |
180
|
|
|
if (empty($source)) { |
181
|
|
|
return []; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$tokens = preg_split( |
185
|
|
|
Token::TOKENIZATION_REGEXP, |
186
|
|
|
$source, |
187
|
|
|
-1, |
188
|
|
|
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $tokens !== false ? $tokens : []; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|