Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileResolver.php
Go to the documentation of this file.
1 <?php
9 
17 
23 {
29  protected $moduleReader;
30 
34  protected $iteratorFactory;
35 
39  protected $currentTheme;
40 
44  protected $area;
45 
49  protected $rootDirectory;
50 
54  protected $resolver;
55 
60  private $directoryList;
61 
70  public function __construct(
71  DirReader $moduleReader,
73  DesignInterface $designInterface,
74  DirectoryList $directoryList,
77  ) {
78  $this->directoryList = $directoryList;
79  $this->iteratorFactory = $iteratorFactory;
80  $this->moduleReader = $moduleReader;
81  $this->currentTheme = $designInterface->getDesignTheme();
82  $this->area = $designInterface->getArea();
83  $this->rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
84  $this->resolver = $resolver;
85  }
86 
90  public function get($filename, $scope)
91  {
92  switch ($scope) {
93  case 'global':
94  $iterator = $this->moduleReader->getConfigurationFiles($filename)->toArray();
95  $themeConfigFile = $this->currentTheme->getCustomization()->getCustomViewConfigPath();
96  if ($themeConfigFile
97  && $this->rootDirectory->isExist($this->rootDirectory->getRelativePath($themeConfigFile))
98  ) {
99  $iterator[$this->rootDirectory->getRelativePath($themeConfigFile)] =
100  $this->rootDirectory->readFile(
101  $this->rootDirectory->getRelativePath(
102  $themeConfigFile
103  )
104  );
105  } else {
106  $designPath = $this->resolver->resolve(
108  'etc/view.xml',
109  $this->area,
110  $this->currentTheme
111  );
112  if (file_exists($designPath)) {
113  try {
114  $designDom = new \DOMDocument();
115  $designDom->load($designPath);
116  $iterator[$designPath] = $designDom->saveXML();
117  } catch (\Exception $e) {
118  throw new \Magento\Framework\Exception\LocalizedException(
119  new \Magento\Framework\Phrase('Could not read config file')
120  );
121  }
122  }
123  }
124  break;
125  default:
126  $iterator = $this->iteratorFactory->create([]);
127  break;
128  }
129  return $iterator;
130  }
131 
135  public function getParents($filename, $scope)
136  {
137  switch ($scope) {
138  case 'global':
139  $iterator = $this->moduleReader->getConfigurationFiles($filename)->toArray();
140  $designPath = $this->resolver->resolve(
142  'etc/view.xml',
143  $this->area,
144  $this->currentTheme
145  );
146 
147  if (file_exists($designPath)) {
148  try {
149  $iterator = $this->getParentConfigs($this->currentTheme, []);
150  } catch (\Exception $e) {
151  throw new \Magento\Framework\Exception\LocalizedException(
152  new \Magento\Framework\Phrase('Could not read config file')
153  );
154  }
155  }
156  break;
157  default:
158  $iterator = $this->iteratorFactory->create([]);
159  break;
160  }
161 
162  return $iterator;
163  }
164 
173  private function getParentConfigs(ThemeInterface $theme, array $iterator, $index = 0)
174  {
175  if ($theme->getParentTheme() && $theme->isPhysical()) {
176  $parentDesignPath = $this->resolver->resolve(
178  'etc/view.xml',
179  $this->area,
180  $theme->getParentTheme()
181  );
182 
183  $parentDom = new \DOMDocument();
184  $parentDom->load($parentDesignPath);
185 
186  $iterator[$index] = $parentDom->saveXML();
187 
188  $iterator = $this->getParentConfigs($theme->getParentTheme(), $iterator, ++$index);
189  }
190 
191  return $iterator;
192  }
193 }
__construct(DirReader $moduleReader, FileIteratorFactory $iteratorFactory, DesignInterface $designInterface, DirectoryList $directoryList, Filesystem $filesystem, ResolverInterface $resolver)
$theme
$filesystem
$index
Definition: list.phtml:44