Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IncludeElement.php
Go to the documentation of this file.
1 <?php
8 
16 
23 {
24  const INCLUDE_PATH = 'path';
25 
29  protected $moduleReader;
30 
34  protected $readFactory;
35 
42  public function __construct(Reader $moduleReader, Filesystem\Directory\ReadFactory $readFactory)
43  {
44  $this->readFactory = $readFactory;
45  $this->moduleReader = $moduleReader;
46  }
47 
57  public function compile(
58  CompilerInterface $compiler,
59  \DOMElement $node,
60  DataObject $processedObject,
61  DataObject $context
62  ) {
63  $ownerDocument = $node->ownerDocument;
64  $parentNode = $node->parentNode;
65 
66  $document = new \DOMDocument();
67  $document->loadXML($this->getContent($node->getAttribute(static::INCLUDE_PATH)));
68 
69  foreach ($this->getChildNodes($document->documentElement) as $child) {
70  $compiler->compile($child, $processedObject, $context);
71  }
72 
73  $newFragment = $ownerDocument->createDocumentFragment();
74  foreach ($document->documentElement->childNodes as $child) {
75  $newFragment->appendXML($document->saveXML($child));
76  }
77 
78  $parentNode->replaceChild($newFragment, $node);
79  }
80 
87  protected function getChildNodes(\DOMElement $node)
88  {
89  $childNodes = [];
90  foreach ($node->childNodes as $child) {
91  $childNodes[] = $child;
92  }
93 
94  return $childNodes;
95  }
96 
104  protected function getContent($includePath)
105  {
106  // <include path="Magento_Payment::my_payment.xml" />
107  list($moduleName, $filename) = explode('::', $includePath);
108 
109  $path = 'adminhtml/' . $filename;
110  $directoryRead = $this->readFactory->create(
111  $this->moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, $moduleName)
112  );
113 
114  if ($directoryRead->isExist($path) && $directoryRead->isFile($path)) {
115  return $directoryRead->readFile($path);
116  }
117 
118  throw new LocalizedException(__('The "%1" file doesn\'t exist.', $path));
119  }
120 }
compile(CompilerInterface $compiler, \DOMElement $node, DataObject $processedObject, DataObject $context)
compile(\DOMNode $node, DataObject $dataObject, DataObject $context)
__()
Definition: __.php:13
__construct(Reader $moduleReader, Filesystem\Directory\ReadFactory $readFactory)