-
Notifications
You must be signed in to change notification settings - Fork 15
/
BlogCommentNodeVisitor.php
117 lines (96 loc) · 2.87 KB
/
BlogCommentNodeVisitor.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
<?php
namespace Frosh\DevelopmentHelper\Component\Twig\NodeVisitor;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Node\BlockNode;
use Twig\Node\BodyNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\TextNode;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\Source;
class BlogCommentNodeVisitor extends AbstractNodeVisitor
{
private const BLACKLIST_KEYS = [
'head_meta_tags',
'layout_head_title',
'page_product_detail_buy_form_action',
'base_body_classes',
'page_checkout_additional',
'buy_widget_buy_form_action',
];
/**
* @var string
*/
private $kernelRootDir;
public function __construct(string $kernelRootDir)
{
$this->kernelRootDir = $kernelRootDir;
}
/**
* @inheritDoc
*/
protected function doEnterNode(Node $node, Environment $env): Node
{
return $node;
}
/**
* @inheritDoc
*/
protected function doLeaveNode(Node $node, Environment $env): Node
{
$twigSource = $node->getSourceContext();
if ($env->getLoader() instanceof ArrayLoader) {
return $node;
}
if ($twigSource === null) {
return $node;
}
if ($twigSource->getPath() === '') {
return $node;
}
$path = $node->getTemplateName();
if ($node->getSourceContext() instanceof Source) {
$path = ltrim(str_replace($this->kernelRootDir, '', $node->getSourceContext()->getPath()), '/');
}
if ($this->isBlacklisted($node)) {
return $node;
}
if ($node instanceof ModuleNode) {
$node->setNode('body', new BodyNode([
new TextNode('<!-- INCLUDE BEGIN ' . $node->getTemplateName() . ' (' . $path .') -->', 0),
$node->getNode('body'),
new TextNode('<!-- INCLUDE END ' . $node->getTemplateName() . ' -->', 0),
]));
} elseif ($node instanceof BlockNode) {
$name = $node->getAttribute('name');
$node->setNode('body', new BodyNode([
new TextNode('<!-- BLOCK BEGIN ' . $name . ' (' . $path .') -->', 0),
$node->getNode('body'),
new TextNode('<!-- BLOCK END ' . $name . ' -->', 0),
]));
}
return $node;
}
public function getPriority(): int
{
return 0;
}
private function isBlacklisted(Node $node): bool
{
$name = $node->getTemplateName();
if ($node instanceof BlockNode) {
$name = $node->getAttribute('name');
}
if (empty($name)) {
return true;
}
foreach (self::BLACKLIST_KEYS as $key) {
if (strpos($name, $key) !== false) {
return true;
}
}
return false;
}
}