Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Reader.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Ui\Config;
8 
15 
19 class Reader implements ReaderInterface
20 {
26  private $idAttributes = ['/' => 'name'];
27 
31  private $definitionReader;
32 
36  private $readerFactory;
37 
41  private $fileResolver;
42 
46  private $converter;
47 
51  private $readerDomFactory;
52 
58  private $fileName;
59 
71  public function __construct(
72  $fileName,
73  FileResolverInterface $fileResolver,
74  ConfigConverter $converter,
75  Reader\Definition $definitionReader,
76  ReaderFactory $readerFactory,
77  Reader\DomFactory $readerDomFactory,
78  array $idAttributes = []
79  ) {
80  $this->fileName = $fileName;
81  $this->fileResolver = $fileResolver;
82  $this->converter = $converter;
83  $this->definitionReader = $definitionReader;
84  $this->readerFactory = $readerFactory;
85  $this->readerDomFactory = $readerDomFactory;
86  $this->idAttributes = array_replace($this->idAttributes, $idAttributes);
87  }
88 
95  public function read($scope = null)
96  {
97  $scope = $scope ?: 'global';
98  $fileList = $this->fileResolver->get($this->fileName, $scope);
99  if (!count($fileList)) {
100  return [];
101  }
102  $output = $this->readFiles($fileList);
103 
104  return $output;
105  }
106 
114  private function readFiles($fileList)
115  {
117  $configMerger = null;
118  $output = [];
119  foreach ($fileList as $key => $content) {
120  try {
121  $configMerger = $this->readerDomFactory->create(
122  [
123  'xml' => $content,
124  'idAttributes' => $this->idAttributes,
125  ]
126  );
127  $output = array_replace_recursive($output, $this->converter->convert($configMerger->getDom()));
128  } catch (ValidationException $e) {
129  throw new LocalizedException(
130  new Phrase(
131  'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
132  [$key, $e->getMessage()]
133  )
134  );
135  }
136  }
137 
138  $definitionData = $this->definitionReader->read();
139 
140  if (isset($output['attributes']['extends'])) {
141  $extendsReader = $this->readerFactory->create(
142  [
143  'fileName' => sprintf(
145  $output['attributes']['extends']
146  )
147  ]
148  );
149  $extendsData = $extendsReader->read();
150  $output = array_replace_recursive($extendsData, $output);
151  }
152 
153  $output = $this->mergeDefinition($output, $definitionData);
154 
155  return $output;
156  }
157 
165  private function mergeDefinition(array $component, array $definitions)
166  {
167  foreach ($component['children'] as $name => $child) {
168  $component['children'][$name] = $this->mergeDefinition($child, $definitions);
169  }
170  if (isset($component['uiComponentType'])) {
171  $definition = isset($definitions[$component['uiComponentType']])
172  ? $definitions[$component['uiComponentType']]
173  : [];
174  $component = array_replace_recursive($definition, $component);
175  unset($component['uiComponentType']);
176  }
177 
178  return $component;
179  }
180 }
$fileList
Definition: export.php:13
__construct( $fileName, FileResolverInterface $fileResolver, ConfigConverter $converter, Reader\Definition $definitionReader, ReaderFactory $readerFactory, Reader\DomFactory $readerDomFactory, array $idAttributes=[])
Definition: Reader.php:71
read($scope=null)
Definition: Reader.php:95
const SEARCH_PATTERN
Definition: Data.php:26
if(!isset($_GET['name'])) $name
Definition: log.php:14