Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractXml.php
Go to the documentation of this file.
1 <?php
11 
16 abstract class AbstractXml
17 {
23  protected $_data;
24 
29  protected $_domConfig = null;
30 
34  protected $domFactory;
35 
43  public function __construct(
44  $configFiles,
45  \Magento\Framework\Config\DomFactory $domFactory
46  ) {
47  $this->domFactory = $domFactory;
48  if (empty($configFiles)) {
49  throw new \InvalidArgumentException('There must be at least one configuration file specified.');
50  }
51  $this->_data = $this->_extractData($this->_merge($configFiles));
52  }
53 
59  abstract public function getSchemaFile();
60 
66  public function getPerFileSchemaFile()
67  {
68  return null;
69  }
70 
77  abstract protected function _extractData(\DOMDocument $dom);
78 
86  protected function _merge($configFiles)
87  {
88  foreach ($configFiles as $key => $content) {
89  try {
90  $this->_getDomConfigModel()->merge($content);
91  } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
92  throw new \Magento\Framework\Exception\LocalizedException(
93  new \Magento\Framework\Phrase(
94  'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
95  [$key, $e->getMessage()]
96  )
97  );
98  }
99  }
100  $this->_performValidate();
101  return $this->_getDomConfigModel()->getDom();
102  }
103 
111  protected function _performValidate($file = null)
112  {
113  $errors = [];
114  $this->_getDomConfigModel()->validate($this->getSchemaFile(), $errors);
115  if (!empty($errors)) {
116  $phrase = (null === $file)
117  ? new \Magento\Framework\Phrase('Invalid Document %1%2', [PHP_EOL, implode("\n", $errors)])
118  : new \Magento\Framework\Phrase('Invalid XML-file: %1%2%3', [$file, PHP_EOL, implode("\n", $errors)]);
119 
120  throw new \Magento\Framework\Exception\LocalizedException($phrase);
121  }
122  return $this;
123  }
124 
131  protected function _getDomConfigModel()
132  {
133  if (null === $this->_domConfig) {
134  $this->_domConfig = $this->domFactory->createDom(
135  [
136  'xml' => $this->_getInitialXml(),
137  'idAttributes' => $this->_getIdAttributes(),
138  'schemaFile' => $this->getPerFileSchemaFile()
139  ]
140  );
141  }
142  return $this->_domConfig;
143  }
144 
150  abstract protected function _getInitialXml();
151 
157  abstract protected function _getIdAttributes();
158 }
$errors
Definition: overview.phtml:9
__construct( $configFiles, \Magento\Framework\Config\DomFactory $domFactory)
Definition: AbstractXml.php:43