AbstractXml.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Configuration XML-files merger
  8. */
  9. namespace Magento\Framework\Config;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. abstract class AbstractXml
  15. {
  16. /**
  17. * Data extracted from the merged configuration files
  18. *
  19. * @var array
  20. */
  21. protected $_data;
  22. /**
  23. * Dom configuration model
  24. * @var \Magento\Framework\Config\Dom
  25. */
  26. protected $_domConfig = null;
  27. /**
  28. * @var \Magento\Framework\Config\DomFactory
  29. */
  30. protected $domFactory;
  31. /**
  32. * Instantiate with the list of files to merge
  33. *
  34. * @param array $configFiles
  35. * @param \Magento\Framework\Config\DomFactory $domFactory
  36. * @throws \InvalidArgumentException
  37. */
  38. public function __construct(
  39. $configFiles,
  40. \Magento\Framework\Config\DomFactory $domFactory
  41. ) {
  42. $this->domFactory = $domFactory;
  43. if (empty($configFiles)) {
  44. throw new \InvalidArgumentException('There must be at least one configuration file specified.');
  45. }
  46. $this->_data = $this->_extractData($this->_merge($configFiles));
  47. }
  48. /**
  49. * Get absolute path to the XML-schema file
  50. *
  51. * @return string
  52. */
  53. abstract public function getSchemaFile();
  54. /**
  55. * Get absolute path to per-file XML-schema file
  56. *
  57. * @return string
  58. */
  59. public function getPerFileSchemaFile()
  60. {
  61. return null;
  62. }
  63. /**
  64. * Extract configuration data from the DOM structure
  65. *
  66. * @param \DOMDocument $dom
  67. * @return array
  68. */
  69. abstract protected function _extractData(\DOMDocument $dom);
  70. /**
  71. * Merge the config XML-files
  72. *
  73. * @param array $configFiles
  74. * @return \DOMDocument
  75. * @throws \Magento\Framework\Exception\LocalizedException If a non-existing or invalid XML-file passed
  76. */
  77. protected function _merge($configFiles)
  78. {
  79. foreach ($configFiles as $key => $content) {
  80. try {
  81. $this->_getDomConfigModel()->merge($content);
  82. } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
  83. throw new \Magento\Framework\Exception\LocalizedException(
  84. new \Magento\Framework\Phrase(
  85. 'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
  86. [$key, $e->getMessage()]
  87. )
  88. );
  89. }
  90. }
  91. $this->_performValidate();
  92. return $this->_getDomConfigModel()->getDom();
  93. }
  94. /**
  95. * Perform xml validation
  96. *
  97. * @param string $file
  98. * @return $this
  99. * @throws \Magento\Framework\Exception\LocalizedException If invalid XML-file passed
  100. */
  101. protected function _performValidate($file = null)
  102. {
  103. $errors = [];
  104. $this->_getDomConfigModel()->validate($this->getSchemaFile(), $errors);
  105. if (!empty($errors)) {
  106. $phrase = (null === $file)
  107. ? new \Magento\Framework\Phrase('Invalid Document %1%2', [PHP_EOL, implode("\n", $errors)])
  108. : new \Magento\Framework\Phrase('Invalid XML-file: %1%2%3', [$file, PHP_EOL, implode("\n", $errors)]);
  109. throw new \Magento\Framework\Exception\LocalizedException($phrase);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Get Dom configuration model
  115. *
  116. * @return \Magento\Framework\Config\Dom
  117. * @throws \Magento\Framework\Config\Dom\ValidationException
  118. */
  119. protected function _getDomConfigModel()
  120. {
  121. if (null === $this->_domConfig) {
  122. $this->_domConfig = $this->domFactory->createDom(
  123. [
  124. 'xml' => $this->_getInitialXml(),
  125. 'idAttributes' => $this->_getIdAttributes(),
  126. 'schemaFile' => $this->getPerFileSchemaFile()
  127. ]
  128. );
  129. }
  130. return $this->_domConfig;
  131. }
  132. /**
  133. * Get XML-contents, initial for merging
  134. *
  135. * @return string
  136. */
  137. abstract protected function _getInitialXml();
  138. /**
  139. * Get list of paths to identifiable nodes
  140. *
  141. * @return array
  142. */
  143. abstract protected function _getIdAttributes();
  144. }