IncludeElement.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Compiler;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Module\Dir;
  10. use Magento\Framework\Module\Dir\Reader;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
  13. use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Element\ElementInterface;
  14. /**
  15. * Class IncludeElement
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class IncludeElement implements ElementInterface
  20. {
  21. const INCLUDE_PATH = 'path';
  22. /**
  23. * @var Reader
  24. */
  25. protected $moduleReader;
  26. /**
  27. * @var Filesystem\Directory\ReadFactory
  28. */
  29. protected $readFactory;
  30. /**
  31. * Constructor
  32. *
  33. * @param Reader $moduleReader
  34. * @param Filesystem\Directory\ReadFactory $readFactory
  35. */
  36. public function __construct(Reader $moduleReader, Filesystem\Directory\ReadFactory $readFactory)
  37. {
  38. $this->readFactory = $readFactory;
  39. $this->moduleReader = $moduleReader;
  40. }
  41. /**
  42. * Compiles the Element node
  43. *
  44. * @param CompilerInterface $compiler
  45. * @param \DOMElement $node
  46. * @param DataObject $processedObject
  47. * @param DataObject $context
  48. * @return void
  49. */
  50. public function compile(
  51. CompilerInterface $compiler,
  52. \DOMElement $node,
  53. DataObject $processedObject,
  54. DataObject $context
  55. ) {
  56. $ownerDocument = $node->ownerDocument;
  57. $parentNode = $node->parentNode;
  58. $document = new \DOMDocument();
  59. $document->loadXML($this->getContent($node->getAttribute(static::INCLUDE_PATH)));
  60. foreach ($this->getChildNodes($document->documentElement) as $child) {
  61. $compiler->compile($child, $processedObject, $context);
  62. }
  63. $newFragment = $ownerDocument->createDocumentFragment();
  64. foreach ($document->documentElement->childNodes as $child) {
  65. $newFragment->appendXML($document->saveXML($child));
  66. }
  67. $parentNode->replaceChild($newFragment, $node);
  68. }
  69. /**
  70. * Get child nodes
  71. *
  72. * @param \DOMElement $node
  73. * @return \DOMElement[]
  74. */
  75. protected function getChildNodes(\DOMElement $node)
  76. {
  77. $childNodes = [];
  78. foreach ($node->childNodes as $child) {
  79. $childNodes[] = $child;
  80. }
  81. return $childNodes;
  82. }
  83. /**
  84. * Get content of include file (in adminhtml area)
  85. *
  86. * @param string $includePath
  87. * @return string
  88. * @throws LocalizedException
  89. */
  90. protected function getContent($includePath)
  91. {
  92. // <include path="Magento_Payment::my_payment.xml" />
  93. list($moduleName, $filename) = explode('::', $includePath);
  94. $path = 'adminhtml/' . $filename;
  95. $directoryRead = $this->readFactory->create(
  96. $this->moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, $moduleName)
  97. );
  98. if ($directoryRead->isExist($path) && $directoryRead->isFile($path)) {
  99. return $directoryRead->readFile($path);
  100. }
  101. throw new LocalizedException(__('The "%1" file doesn\'t exist.', $path));
  102. }
  103. }