Reader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent\Config;
  7. use Magento\Framework\Filesystem;
  8. use Magento\Framework\Config\ConverterInterface;
  9. use Magento\Framework\Config\FileIteratorFactory;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\Filesystem\Directory\ReadInterface;
  12. /**
  13. * Class Reader
  14. */
  15. class Reader implements UiReaderInterface
  16. {
  17. /**
  18. * DOM document merger
  19. *
  20. * @var DomMergerInterface
  21. */
  22. protected $domMerger;
  23. /**
  24. * XML converter
  25. *
  26. * @var ConverterInterface
  27. */
  28. protected $converter;
  29. /**
  30. * Constructor
  31. *
  32. * @param FileCollectorInterface $fileCollector
  33. * @param ConverterInterface $converter
  34. * @param DomMergerInterface $domMerger
  35. */
  36. public function __construct(
  37. FileCollectorInterface $fileCollector,
  38. ConverterInterface $converter,
  39. DomMergerInterface $domMerger
  40. ) {
  41. $this->converter = $converter;
  42. $this->domMerger = $domMerger;
  43. $this->readFiles($fileCollector->collectFiles());
  44. }
  45. /**
  46. * Read configuration files
  47. *
  48. * @param array $fileList
  49. * @return array
  50. * @throws \Magento\Framework\Exception\LocalizedException
  51. */
  52. protected function readFiles(array $fileList)
  53. {
  54. foreach ($fileList as $fileContent) {
  55. $this->domMerger->merge($fileContent);
  56. }
  57. }
  58. /**
  59. * Add xml content in the merged file
  60. *
  61. * @param string $xmlContent
  62. * @return void
  63. */
  64. public function addXMLContent($xmlContent)
  65. {
  66. $this->domMerger->merge($xmlContent);
  67. }
  68. /**
  69. * Add DOM node into DOM document
  70. *
  71. * @param \DOMNode $node
  72. * @return void
  73. */
  74. public function addNode(\DOMNode $node)
  75. {
  76. $this->domMerger->mergeNode($node);
  77. }
  78. /**
  79. * Load configuration scope
  80. *
  81. * @param string|null $scope
  82. * @return array
  83. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  84. */
  85. public function read($scope = null)
  86. {
  87. return $this->converter->convert($this->domMerger->getDom());
  88. }
  89. /**
  90. * Get content from the merged files
  91. *
  92. * @return string
  93. */
  94. public function getContent()
  95. {
  96. return $this->domMerger->getDom()->saveXML();
  97. }
  98. /**
  99. * Get DOM document
  100. *
  101. * @return \DOMDocument
  102. */
  103. public function getDOMDocument()
  104. {
  105. return $this->domMerger->getDom();
  106. }
  107. }