Rules   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A rules() 0 41 2
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