Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Filesystem.php
Go to the documentation of this file.
1 <?php
11 
18 {
24  protected $_fileResolver;
25 
31  protected $_converter;
32 
38  protected $_fileName;
39 
45  protected $_schema;
46 
52  protected $_perFileSchema;
53 
59  protected $_idAttributes = [];
60 
66  protected $_domDocumentClass;
67 
71  protected $validationState;
72 
77  protected $_defaultScope;
78 
83  protected $_schemaFile;
84 
97  public function __construct(
98  \Magento\Framework\Config\FileResolverInterface $fileResolver,
99  \Magento\Framework\Config\ConverterInterface $converter,
100  \Magento\Framework\Config\SchemaLocatorInterface $schemaLocator,
102  $fileName,
103  $idAttributes = [],
104  $domDocumentClass = \Magento\Framework\Config\Dom::class,
105  $defaultScope = 'global'
106  ) {
107  $this->_fileResolver = $fileResolver;
108  $this->_converter = $converter;
109  $this->_fileName = $fileName;
110  $this->_idAttributes = array_replace($this->_idAttributes, $idAttributes);
111  $this->validationState = $validationState;
112  $this->_schemaFile = $schemaLocator->getSchema();
113  $this->_perFileSchema = $schemaLocator->getPerFileSchema() && $validationState->isValidationRequired()
114  ? $schemaLocator->getPerFileSchema() : null;
115  $this->_domDocumentClass = $domDocumentClass;
116  $this->_defaultScope = $defaultScope;
117  }
118 
125  public function read($scope = null)
126  {
127  $scope = $scope ?: $this->_defaultScope;
128  $fileList = $this->_fileResolver->get($this->_fileName, $scope);
129  if (!count($fileList)) {
130  return [];
131  }
132  $output = $this->_readFiles($fileList);
133 
134  return $output;
135  }
136 
144  protected function _readFiles($fileList)
145  {
147  $configMerger = null;
148  foreach ($fileList as $key => $content) {
149  try {
150  if (!$configMerger) {
151  $configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
152  } else {
153  $configMerger->merge($content);
154  }
155  } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
156  throw new \Magento\Framework\Exception\LocalizedException(
157  new \Magento\Framework\Phrase(
158  'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
159  [$key, $e->getMessage()]
160  )
161  );
162  }
163  }
164  if ($this->validationState->isValidationRequired()) {
165  $errors = [];
166  if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
167  $message = "Invalid Document \n";
168  throw new \Magento\Framework\Exception\LocalizedException(
169  new \Magento\Framework\Phrase($message . implode("\n", $errors))
170  );
171  }
172  }
173 
174  $output = [];
175  if ($configMerger) {
176  $output = $this->_converter->convert($configMerger->getDom());
177  }
178  return $output;
179  }
180 
189  protected function _createConfigMerger($mergerClass, $initialContents)
190  {
191  $result = new $mergerClass(
192  $initialContents,
193  $this->validationState,
194  $this->_idAttributes,
195  null,
196  $this->_perFileSchema
197  );
198  if (!$result instanceof \Magento\Framework\Config\Dom) {
199  throw new \UnexpectedValueException(
200  "Instance of the DOM config merger is expected, got {$mergerClass} instead."
201  );
202  }
203  return $result;
204  }
205 }
$fileList
Definition: export.php:13
$message
$fileName
Definition: translate.phtml:15
_createConfigMerger($mergerClass, $initialContents)
Definition: Filesystem.php:189
$errors
Definition: overview.phtml:9
__construct(\Magento\Framework\Config\FileResolverInterface $fileResolver, \Magento\Framework\Config\ConverterInterface $converter, \Magento\Framework\Config\SchemaLocatorInterface $schemaLocator, \Magento\Framework\Config\ValidationStateInterface $validationState, $fileName, $idAttributes=[], $domDocumentClass=\Magento\Framework\Config\Dom::class, $defaultScope='global')
Definition: Filesystem.php:97