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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\TemplateEngine\Xhtml;
use Magento\Framework\DataObject;
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\AttributeInterface;
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\CdataInterface;
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\CommentInterface;
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Element\ElementInterface;
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\TextInterface;
/**
* Class Compiler
*/
class Compiler implements CompilerInterface
{
/**
* @var TextInterface
*/
protected $compilerText;
/**
* @var AttributeInterface
*/
protected $compilerAttribute;
/**
* @var CdataInterface
*/
protected $compilerCdata;
/**
* @var CommentInterface
*/
protected $compilerComment;
/**
* @var ElementInterface[]
*/
protected $elementCompilers;
/**
* Postprocessing data
*
* @var array
*/
protected $data;
/**
* Constructor
*
* @param TextInterface $compilerText
* @param AttributeInterface $compilerAttribute
* @param AttributeInterface|CdataInterface $compilerCdata
* @param CommentInterface $compilerComment
* @param ElementInterface[] $elementCompilers
*/
public function __construct(
TextInterface $compilerText,
AttributeInterface $compilerAttribute,
CdataInterface $compilerCdata,
CommentInterface $compilerComment,
array $elementCompilers
) {
$this->compilerText = $compilerText;
$this->compilerAttribute = $compilerAttribute;
$this->compilerCdata = $compilerCdata;
$this->compilerComment = $compilerComment;
$this->elementCompilers = $elementCompilers;
}
/**
* The compilation of the template and filling in the data
*
* @param \DOMNode $node
* @param DataObject $processedObject
* @param DataObject $context
* @return void
*/
public function compile(\DOMNode $node, DataObject $processedObject, DataObject $context)
{
switch ($node->nodeType) {
case XML_TEXT_NODE:
$this->compilerText->compile($node, $processedObject);
break;
case XML_CDATA_SECTION_NODE:
$this->compilerCdata->compile($node, $processedObject);
break;
case XML_COMMENT_NODE:
$this->compilerComment->compile($node, $processedObject);
break;
default:
/** @var \DomElement $node */
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$this->compilerAttribute->compile($attribute, $processedObject);
}
}
$compiler = $this->getElementCompiler($node->nodeName);
if (null !== $compiler) {
$compiler->compile($this, $node, $processedObject, $context);
} elseif ($node->hasChildNodes()) {
foreach ($this->getChildNodes($node) as $child) {
$this->compile($child, $processedObject, $context);
}
}
}
}
/**
* Run postprocessing contents
*
* @param string $content
* @return string
*/
public function postprocessing($content)
{
$patternTag = preg_quote(CompilerInterface::PATTERN_TAG);
return preg_replace_callback(
'#' . $patternTag . '(.+?)' . $patternTag . '#',
function ($match) {
return $this->data[$match[1]] ?? '';
},
$content
);
}
/**
* Set postprocessing data
*
* @param string $key
* @param string $content
* @return void
*/
public function setPostprocessingData($key, $content)
{
$this->data[$key] = $content;
}
/**
* Get child nodes
*
* @param \DOMElement $node
* @return \DOMElement[]
*/
protected function getChildNodes(\DOMElement $node)
{
$childNodes = [];
foreach ($node->childNodes as $child) {
$childNodes[] = $child;
}
return $childNodes;
}
/**
* Get element compiler by name
*
* @param string $name
* @return ElementInterface
*/
protected function getElementCompiler($name)
{
if (isset($this->elementCompilers[$name])) {
return $this->elementCompilers[$name];
}
return null;
}
}