1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Twig. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier |
7
|
|
|
* (c) Armin Ronacher |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace nystudio107\templatecomments\web\twig\nodes; |
14
|
|
|
|
15
|
|
|
use Twig\Compiler; |
16
|
|
|
use Twig\Node\BlockNode; |
17
|
|
|
use Twig\Node\Node; |
18
|
|
|
use function in_array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a block node. |
22
|
|
|
* |
23
|
|
|
* @author Fabien Potencier <[email protected]> |
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class CommentBlockNode extends BlockNode |
26
|
|
|
{ |
27
|
|
|
private string $blockName; |
|
|
|
|
28
|
|
|
|
29
|
|
|
private array $excludeBlocks = ['attr']; |
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct(string $name, Node $body, int $lineno, string $tag = null) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
parent::__construct($name, $body, $lineno, $tag); |
34
|
|
|
$this->blockName = $name; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function compile(Compiler $compiler): void |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$compiler |
40
|
|
|
->addDebugInfo($this) |
41
|
|
|
->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n") |
42
|
|
|
->indent() |
43
|
|
|
->write("\$macros = \$this->macros;\n"); |
44
|
|
|
if (!in_array($this->blockName, $this->excludeBlocks, false)) { |
45
|
|
|
$compiler |
46
|
|
|
->write('$_blockTimer = microtime(true)') |
47
|
|
|
->raw(";\n") |
48
|
|
|
->write('$_templateName = ') |
49
|
|
|
->write("'" . $this->getTemplateName() . "'") |
50
|
|
|
->raw(";\n") |
51
|
|
|
->write('$_blockName = ') |
52
|
|
|
->write("'" . $this->blockName . "'") |
53
|
|
|
->raw(";\n") |
54
|
|
|
->write('echo PHP_EOL."<!-- >>> BLOCK BEGIN >>> ".$_blockName." FROM ".$_templateName." -->".PHP_EOL') |
55
|
|
|
->raw(";\n"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$compiler |
59
|
|
|
->subcompile($this->getNode('body')) |
60
|
|
|
->outdent(); |
61
|
|
|
if (!in_array($this->blockName, $this->excludeBlocks, false)) { |
62
|
|
|
$compiler |
63
|
|
|
->write('echo PHP_EOL."<!-- <<< BLOCK END <<< ".$_blockName." FROM ".$_templateName." TIME ".number_format((microtime(true)-$_blockTimer)*1000,2)."ms -->".PHP_EOL') |
64
|
|
|
->raw(";\n") |
65
|
|
|
->write("unset(\$_blockTimer);\n") |
66
|
|
|
->write("unset(\$_blockName);\n") |
67
|
|
|
->write("unset(\$_templateName);\n"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$compiler |
71
|
|
|
->write("}\n\n"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|