|
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) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($name, $body, $lineno); |
|
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
|
|
|
->write("return; yield '';\n") // needed when body doesn't yield anything |
|
61
|
|
|
->outdent(); |
|
62
|
|
|
if (!in_array($this->blockName, $this->excludeBlocks, false)) { |
|
63
|
|
|
$compiler |
|
64
|
|
|
->write('echo PHP_EOL."<!-- <<< BLOCK END <<< ".$_blockName." FROM ".$_templateName." TIME ".number_format((microtime(true)-$_blockTimer)*1000,2)."ms -->".PHP_EOL') |
|
65
|
|
|
->raw(";\n") |
|
66
|
|
|
->write("unset(\$_blockTimer);\n") |
|
67
|
|
|
->write("unset(\$_blockName);\n") |
|
68
|
|
|
->write("unset(\$_templateName);\n"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$compiler |
|
72
|
|
|
->write("}\n\n"); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|