Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
7 
14 class View extends \Magento\Framework\Config\Reader\Filesystem
15 {
19  protected $xpath;
20 
26  protected $data;
27 
39  public function __construct(
40  FileResolverInterface $fileResolver,
41  ConverterInterface $converter,
42  SchemaLocatorInterface $schemaLocator,
44  $fileName,
45  $idAttributes = [],
46  $domDocumentClass = \Magento\Framework\Config\Dom::class,
47  $defaultScope = 'global',
48  $xpath = []
49  ) {
50  $this->xpath = $xpath;
51  $idAttributes = $this->getIdAttributes();
52  parent::__construct(
53  $fileResolver,
54  $converter,
55  $schemaLocator,
57  $fileName,
58  $idAttributes,
59  $domDocumentClass,
60  $defaultScope
61  );
62  }
63 
72  public function getVars($module)
73  {
74  $this->initData();
75  return $this->data['vars'][$module] ?? [];
76  }
77 
85  public function getVarValue($module, $var)
86  {
87  $this->initData();
88  if (!isset($this->data['vars'][$module])) {
89  return false;
90  }
91 
92  $value = $this->data['vars'][$module];
93  foreach (explode('/', $var) as $node) {
94  if (is_array($value) && isset($value[$node])) {
95  $value = $value[$node];
96  } else {
97  return false;
98  }
99  }
100 
101  return $value;
102  }
103 
111  public function getMediaEntities($module, $mediaType)
112  {
113  $this->initData();
114  return $this->data['media'][$module][$mediaType] ?? [];
115  }
116 
125  public function getMediaAttributes($module, $mediaType, $mediaId)
126  {
127  $this->initData();
128  return $this->data['media'][$module][$mediaType][$mediaId] ?? [];
129  }
130 
136  protected function getIdAttributes()
137  {
138  $idAttributes = [
139  '/view/vars' => 'module',
140  '/view/vars/(var/)*var' => 'name',
141  '/view/exclude/item' => ['type', 'item'],
142  ];
143  foreach ($this->xpath as $attribute) {
144  if (is_array($attribute)) {
145  foreach ($attribute as $key => $id) {
146  if (count($id) > 1) {
147  $idAttributes[$key] = array_values($id);
148  } else {
149  $idAttributes[$key] = array_shift($id);
150  }
151  }
152  }
153  }
154  return $idAttributes;
155  }
156 
162  public function getExcludedFiles()
163  {
164  $items = $this->getItems();
165  return $items['file'] ?? [];
166  }
167 
173  public function getExcludedDir()
174  {
175  $items = $this->getItems();
176  return $items['directory'] ?? [];
177  }
178 
184  protected function getItems()
185  {
186  $this->initData();
187  return $this->data['exclude'] ?? [];
188  }
189 
195  protected function initData()
196  {
197  if ($this->data === null) {
198  $this->data = $this->read();
199  }
200  }
201 
206  public function read($scope = null)
207  {
208  $scope = $scope ?: $this->_defaultScope;
209  $result = [];
210 
211  $parents = (array)$this->_fileResolver->getParents($this->_fileName, $scope);
212  // Sort parents desc
213  krsort($parents);
214 
215  foreach ($parents as $parent) {
216  $result = array_replace_recursive($result, $this->_readFiles([$parent]));
217  }
218 
219  return array_replace_recursive($result, parent::read($scope));
220  }
221 }
$id
Definition: fieldset.phtml:14
getMediaAttributes($module, $mediaType, $mediaId)
Definition: View.php:125
__construct(FileResolverInterface $fileResolver, ConverterInterface $converter, SchemaLocatorInterface $schemaLocator, ValidationStateInterface $validationState, $fileName, $idAttributes=[], $domDocumentClass=\Magento\Framework\Config\Dom::class, $defaultScope='global', $xpath=[])
Definition: View.php:39
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
getMediaEntities($module, $mediaType)
Definition: View.php:111
getVarValue($module, $var)
Definition: View.php:85
$items