-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplate.class.php
448 lines (419 loc) · 14.3 KB
/
Template.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Mecanismo de Template para PHP5
*
* Mecanismos de Template permitem manter o código HTML em arquivos externos
* que ficam completamente livres de código PHP. Dessa forma, consegue-se manter
* a lógica de programação (PHP) separada da estrutura visual (HTML ou XML, CSS, etc).
*
* Se você já é familiar ao uso de mecanismos de template PHP, esta classe inclui
* algumas melhorias: suporte à objetos, automaticamente detecta blocos, mantém uma
* lista interna das variáveis que existem, limpa automaticamente blocos "filhos",
* avisando quando tentamos chamar blocos ou variáveis que são existem, avisando quando
* criamos blocos "mal formados", e outras pequenas ajudas.
*
* @author Rael G.C. ([email protected])
* @version 1.92
*/
class Template {
/**
* A list of existent document variables.
*
* @var array
*/
private $vars = array();
/**
* A hash with vars and values setted by the user.
*
* @var array
*/
private $values = array();
/**
* A hash of existent object properties variables in the document.
*
* @var array
*/
private $properties = array();
/**
* A hash of the object instances setted by the user.
*
* @var array
*/
private $instances = array();
/**
* A list of all automatic recognized blocks.
*
* @var array
*/
private $blocks = array();
/**
* A list of all blocks that contains at least a "child" block.
*
* @var array
*/
private $parents = array();
/**
* Describes the replace method for blocks. See the Template::setFile()
* method for more details.
*
* @var boolean
*/
private $accurate;
/**
* Regular expression to find var and block names.
* Only alfa-numeric chars and the underscore char are allowed.
*
* @var string
*/
private static $REG_NAME = "([[:alnum:]]|_)+";
/**
* Cria um novo template, usando $filename como arquivo principal
*
* Quando o parâmetro $accurate é true, a substituição dos blocos no arquivo
* final será perfeitamente fiel ao arquivo original, isto é, todas as tabulações
* serão removidas. Isso vai ter um pequeno prejuízo na performance, que pode variar
* de acordo com a versão do PHP em uso. Mas é útil quando estamos usando tags HTML
* como <pre> ou <code>. Em outros casos, ou melhor, quase sempre,
* nunca se mexe no valor de $accurate.
*
* @param string $filename caminho do arquivo que será lido
* @param booelan $accurate true para fazer substituição fiel das tabulações
*/
public function __construct($filename, $accurate = false){
$this->accurate = $accurate;
$this->loadfile(".", $filename);
}
/**
* Adiciona o conteúdo do arquivo identificado por $filename na variável de template
* identificada por $varname
*
* @param string $varname uma variável de template existente
* @param string $filename arquivo a ser carregado
*/
public function addFile($varname, $filename){
if(!$this->exists($varname)) throw new InvalidArgumentException("addFile: var $varname não existe");
$this->loadfile($varname, $filename);
}
/**
* Não use este método, ele serve apenas para podemos acessar as variáveis
* de template diretamente.
*
* @param string $varname template var name
* @param mixed $value template var value
*/
public function __set($varname, $value){
if(!$this->exists($varname)) throw new RuntimeException("var $varname não existe");
$stringValue = $value;
if(is_object($value)){
$this->instances[$varname] = $value;
if(!array_key_exists($varname, $this->properties)) $this->properties[$varname] = array();
if(method_exists($value, "__toString")) $stringValue = $value->__toString();
else $stringValue = "Object";
}
$this->setValue($varname, $stringValue);
return $value;
}
/**
* Não use este método, ele serve apenas para podemos acessar as variáveis
* de template diretamente.
*
* @param string $varname template var name
*/
public function __get($varname){
if (isset($this->values["{".$varname."}"])) return $this->values["{".$varname."}"];
throw new RuntimeException("var $varname não existe");
}
/**
* Verifica se uma variável de template existe.
*
* Retorna true se a variável existe. Caso contrário, retorna false.
*
* @param string $varname template var name
*/
public function exists($varname){
return in_array($varname, $this->vars);
}
/**
* Loads a file identified by $filename.
*
* The file will be loaded and the file's contents will be assigned as the
* variable's value.
* Additionally, this method call Template::recognize() that identifies
* all blocks and variables automatically.
*
* @param string $varname contains the name of a variable to load
* @param string $filename file name to be loaded
*
* @return void
*/
private function loadfile($varname, $filename) {
if (!file_exists($filename)) throw new InvalidArgumentException("arquivo $filename não existe");
// If it's PHP file, parse it
if($this->isPHP($filename)){
ob_start();
require $filename;
$str = ob_get_contents();
ob_end_clean();
$this->setValue($varname, $str);
} else {
// Reading file and hiding comments
$str = preg_replace("/<!---.*?--->/smi", "", file_get_contents($filename));
$this->setValue($varname, $str);
$blocks = $this->recognize($str, $varname);
if (empty($str)) throw new InvalidArgumentException("arquivo $filename está vazio");
$this->createBlocks($blocks);
}
}
/**
* Check if file is a .php
*/
private function isPHP($filename){
foreach(array('.php', '.php5', '.cgi') as $php){
if(0 == strcasecmp($php, substr($filename, strripos($filename, $php)))) return true;
}
return false;
}
/**
* Identify all blocks and variables automatically and return them.
*
* All variables and blocks are already identified at the moment when
* user calls Template::setFile(). This method calls Template::identifyVars()
* and Template::identifyBlocks() methods to do the job.
*
* @param string $content file content
* @param string $varname contains the variable name of the file
*
* @return array an array where the key is the block name and the value is an
* array with the children block names.
*/
private function recognize(&$content, $varname){
$blocks = array();
$queued_blocks = array();
foreach (explode("\n", $content) as $line) {
if (strpos($line, "{")!==false) $this->identifyVars($line);
if (strpos($line, "<!--")!==false) $this->identifyBlocks($line, $varname, $queued_blocks, $blocks);
}
return $blocks;
}
/**
* Identify all user defined blocks automatically.
*
* @param string $line contains one line of the content file
* @param string $varname contains the filename variable identifier
* @param string $queued_blocks contains a list of the current queued blocks
* @param string $blocks contains a list of all identified blocks in the current file
*
* @return void
*/
private function identifyBlocks(&$line, $varname, &$queued_blocks, &$blocks){
$reg = "/<!--\s*BEGIN\s+(".self::$REG_NAME.")\s*-->/sm";
preg_match($reg, $line, $m);
if (1==preg_match($reg, $line, $m)){
if (0==sizeof($queued_blocks)) $parent = $varname;
else $parent = end($queued_blocks);
if (!isset($blocks[$parent])){
$blocks[$parent] = array();
}
$blocks[$parent][] = $m[1];
$queued_blocks[] = $m[1];
}
$reg = "/<!--\s*END\s+(".self::$REG_NAME.")\s*-->/sm";
if (1==preg_match($reg, $line)) array_pop($queued_blocks);
}
/**
* Identifies all variables defined in the document.
*
* @param string $line contains one line of the content file
*/
private function identifyVars(&$line){
$r = preg_match_all("/{(".self::$REG_NAME.")((\-\>(".self::$REG_NAME."))*)?}/", $line, $m);
if ($r){
for($i=0; $i<$r; $i++){
// Object var detected
if($m[3][$i] && (!array_key_exists($m[1][$i], $this->properties) || !in_array($m[3][$i], $this->properties[$m[1][$i]]))){
$this->properties[$m[1][$i]][] = $m[3][$i];
}
if(!in_array($m[1][$i], $this->vars)) $this->vars[] = $m[1][$i];
}
}
}
/**
* Create all identified blocks given by Template::identifyBlocks().
*
* @param array $blocks contains all identified block names
* @return void
*/
private function createBlocks(&$blocks) {
$this->parents = array_merge($this->parents, $blocks);
foreach($blocks as $parent => $block){
foreach($block as $chield){
if(in_array($chield, $this->blocks)) throw new UnexpectedValueException("bloco duplicado: $chield");
$this->blocks[] = $chield;
$this->setBlock($parent, $chield);
}
}
}
/**
* A variable $parent may contain a variable block defined by:
* <!-- BEGIN $varname --> content <!-- END $varname -->.
*
* This method removes that block from $parent and replaces it with a variable
* reference named $block. The block is inserted into the varKeys and varValues
* hashes.
* Blocks may be nested.
*
* @param string $parent contains the name of the parent variable
* @param string $block contains the name of the block to be replaced
* @return void
*/
private function setBlock($parent, $block) {
$name = "B_".$block;
$str = $this->getVar($parent);
if($this->accurate){
$str = str_replace("\r\n", "\n", $str);
$reg = "/\t*<!--\s*BEGIN\s+$block\s+-->\n*(\s*.*?\n?)\t*<!--\s+END\s+$block\s*-->\n?/sm";
}
else $reg = "/<!--\s*BEGIN\s+$block\s+-->\s*(\s*.*?\s*)<!--\s+END\s+$block\s*-->\s*/sm";
if(1!==preg_match($reg, $str, $m)) throw new UnexpectedValueException("bloco $block está mal formado");
$this->setValue($name, '');
$this->setValue($block, $m[1]);
$this->setValue($parent, preg_replace($reg, "{".$name."}", $str));
}
/**
* Internal setValue() method.
*
* The main difference between this and Template::__set() method is this
* method cannot be called by the user, and can be called using variables or
* blocks as parameters.
*
* @param string $varname constains a varname
* @param string $value constains the new value for the variable
* @return void
*/
private function setValue($varname, $value) {
$this->values["{".$varname."}"] = $value;
}
/**
* Returns the value of the variable identified by $varname.
*
* @param string $varname the name of the variable to get the value of
* @return string the value of the variable passed as argument
*/
private function getVar($varname) {
return $this->values['{'.$varname.'}'];
}
/**
* Limpa o valor de uma variável
*
* O mesmo que $this->setValue($varname, "");
*
* @param string $varname nome da variável
*/
public function clear($varname) {
$this->setValue($varname, "");
}
/**
* Fill in all the variables contained within the variable named
* $varname. The resulting value is returned as the function result and the
* original value of the variable varname is not changed. The resulting string
* is not "finished", that is, the unresolved variable name policy has not been
* applied yet.
*
* @param string $varname the name of the variable within which variables are to be substituted
* @return string the value of the variable $varname with all variables substituted.
*/
private function subst($varname) {
$s = $this->getVar($varname);
// Common variables replacement
$s = str_replace(array_keys($this->values), $this->values, $s);
// Object variables replacement
foreach($this->instances as $var => $instance){
foreach($this->properties[$var] as $properties){
if(false!==strpos($s, "{".$var.$properties."}")){
$pointer = $instance;
$property = explode("->", $properties);
for($i = 1; $i < sizeof($property); $i++){
$obj = str_replace('_', '', $property[$i]);
// Non boolean accessor
if(method_exists($pointer, "get$obj")){
$pointer = $pointer->{"get$obj"}();
}
// Boolean accessor
elseif(method_exists($pointer, "is$obj")){
$pointer = $pointer->{"is$obj"}();
}
// Magic __get accessor
elseif(method_exists($pointer, "__get")){
$pointer = $pointer->__get($property[$i]);
}
// Accessor dot not exists: throw Exception
else {
$className = $property[$i-1] ? $property[$i-1] : get_class($instance);
$class = is_null($pointer) ? "NULL" : get_class($pointer);
throw new BadMethodCallException("não existe método na classe ".$class." para acessar ".$className."->".$property[$i]);
}
}
// Checking if final value is an object
if(is_object($pointer)){
if(method_exists($pointer, "__toString")){
$pointer = $pointer->__toString();
} else {
$pointer = "Object";
}
}
// Replace
$s = str_replace("{".$var.$properties."}", $pointer, $s);
}
}
}
return $s;
}
/**
* Clear all child blocks of a given block.
*
* @param string $block a block with chield blocks.
*/
private function clearBlocks($block) {
if (isset($this->parents[$block])){
$chields = $this->parents[$block];
foreach($chields as $chield){
$this->clear("B_".$chield);
}
}
}
/**
* Mostra um bloco.
*
* Esse método deve ser chamado quando um bloco deve ser mostrado.
* Sem isso, o bloco não irá aparecer no conteúdo final.
*
* Se o parâmetro $append for true, o conteúdo do bloco será
* adicionado ao conteúdo que já existia antes. Ou seja, use true
* quando quiser que o bloco seja duplicado.
*
* @param string $block nome do bloco que deve ser mostrado
* @param boolean $append true se o conteúdo anterior deve ser mantido (ou seja, para duplicar o bloco)
*/
public function block($block, $append = true) {
if(!in_array($block, $this->blocks)) throw new InvalidArgumentException("bloco $block não existe");
if ($append) $this->setValue("B_".$block, $this->getVar("B_".$block) . $this->subst($block));
else $this->setValue("B_".$block, $this->subst($block));
$this->clearBlocks($block);
}
/**
* Retorna o conteúdo final, sem mostrá-lo na tela.
* Se você quer mostrá-lo na tela, use o método Template::show().
*
* @return string
*/
public function parse() {
// After subst, remove empty vars
return preg_replace("/{(".self::$REG_NAME.")((\-\>(".self::$REG_NAME."))*)?}/", "", $this->subst("."));
}
/**
* Mostra na tela o conteúdo final.
*/
public function show() {
echo $this->parse();
}
}