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; } }