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