1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tleckie\Template\Compiler\Parser; |
4
|
|
|
|
5
|
|
|
use function sprintf; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Rules |
9
|
|
|
* |
10
|
|
|
* @package Tleckie\Template\Compiler\Parser |
11
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Rules extends AbstractRules |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public function rules(): array |
19
|
|
|
{ |
20
|
|
|
$compiler = $this->compiler; |
21
|
|
|
|
22
|
|
|
return [ |
23
|
|
|
'/\{\#(.*)\#\}/Ui' => static function ($matches) { |
24
|
|
|
return '<?php /*' . $matches[1] . '*/ ?>'; |
25
|
|
|
}, |
26
|
|
|
'/\{set\s+(.*)\s*;?\s*\}/Ui' => static function ($matches) { |
27
|
|
|
return '<?php ' . $matches[1] . '; ?>'; |
28
|
|
|
}, |
29
|
|
|
'/\{(elseif)\s+(.*)\}/Ui' => static function ($matches) { |
30
|
|
|
return '<?php }' . $matches[1] . '(' . $matches[2] . '){ ?>'; |
31
|
|
|
}, |
32
|
|
|
'/\{else\/?\}/Ui' => static function () { |
33
|
|
|
return '<?php }else{ ?>'; |
34
|
|
|
}, |
35
|
|
|
'/\{dump\s+(.*)\s*;?\s*\}/Ui' => static function ($matches) { |
36
|
|
|
return '<?php var_dump(' . $matches[1] . '); ?>'; |
37
|
|
|
}, |
38
|
|
|
'/\{(foreach|if|for)\s+(.*)\}/Ui' => static function ($matches) { |
39
|
|
|
return '<?php ' . $matches[1] . '(' . $matches[2] . '){ ?>'; |
40
|
|
|
}, |
41
|
|
|
'/\{(endforeach|endif|endfor)\}/Ui' => static function ($matches) { |
42
|
|
|
return '<?php } ?>'; |
43
|
|
|
}, |
44
|
|
|
'/\{\{\s?(.*)\s*;?\s?\}\}/Ui' => static function ($matches) { |
45
|
|
|
return '<?php echo ' . $matches[1] . '; ?>'; |
46
|
|
|
}, |
47
|
|
|
'/\{\s?extends\s*([\w\-_\.\/]*)\}/Ui' => static function ($matches) use ($compiler) { |
48
|
|
|
$templatePath = $compiler->getTemplatePath()->withFile($matches[1]); |
49
|
|
|
$compilerPath = $compiler->getCompiledPath()->withFile(sprintf('%s.compiled', $matches[1])); |
50
|
|
|
|
51
|
|
|
$compiler->setTemplatePath($templatePath); |
52
|
|
|
$compiler->setCompiledPath($compilerPath); |
53
|
|
|
|
54
|
|
|
if ($compiler->needsCompile()) { |
55
|
|
|
$compiler->compile(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return sprintf('%s', $compilerPath->read()); |
59
|
|
|
} |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|