FileResolver.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Application config file resolver
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Config;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Module\Dir\Reader as DirReader;
  12. use Magento\Framework\View\Design\Fallback\RulePool;
  13. use Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface;
  14. use Magento\Framework\View\Design\ThemeInterface;
  15. use Magento\Framework\View\DesignInterface;
  16. /**
  17. * Class FileResolver
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class FileResolver implements \Magento\Framework\Config\FileResolverInterface, DesignResolverInterface
  21. {
  22. /**
  23. * Module configuration file reader
  24. *
  25. * @var DirReader
  26. */
  27. protected $moduleReader;
  28. /**
  29. * @var \Magento\Framework\Config\FileIteratorFactory
  30. */
  31. protected $iteratorFactory;
  32. /**
  33. * @var \Magento\Framework\View\DesignInterface
  34. */
  35. protected $currentTheme;
  36. /**
  37. * @var string
  38. */
  39. protected $area;
  40. /**
  41. * @var Filesystem\Directory\ReadInterface
  42. */
  43. protected $rootDirectory;
  44. /**
  45. * @var \Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface
  46. */
  47. protected $resolver;
  48. /**
  49. * @var DirectoryList
  50. * @deprecated 102.0.0 Unused class property
  51. */
  52. private $directoryList;
  53. /**
  54. * @param DirReader $moduleReader
  55. * @param FileIteratorFactory $iteratorFactory
  56. * @param DesignInterface $designInterface
  57. * @param DirectoryList $directoryList @deprecated
  58. * @param Filesystem $filesystem
  59. * @param ResolverInterface $resolver
  60. */
  61. public function __construct(
  62. DirReader $moduleReader,
  63. FileIteratorFactory $iteratorFactory,
  64. DesignInterface $designInterface,
  65. DirectoryList $directoryList,
  66. Filesystem $filesystem,
  67. ResolverInterface $resolver
  68. ) {
  69. $this->directoryList = $directoryList;
  70. $this->iteratorFactory = $iteratorFactory;
  71. $this->moduleReader = $moduleReader;
  72. $this->currentTheme = $designInterface->getDesignTheme();
  73. $this->area = $designInterface->getArea();
  74. $this->rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
  75. $this->resolver = $resolver;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function get($filename, $scope)
  81. {
  82. switch ($scope) {
  83. case 'global':
  84. $iterator = $this->moduleReader->getConfigurationFiles($filename)->toArray();
  85. $themeConfigFile = $this->currentTheme->getCustomization()->getCustomViewConfigPath();
  86. if ($themeConfigFile
  87. && $this->rootDirectory->isExist($this->rootDirectory->getRelativePath($themeConfigFile))
  88. ) {
  89. $iterator[$this->rootDirectory->getRelativePath($themeConfigFile)] =
  90. $this->rootDirectory->readFile(
  91. $this->rootDirectory->getRelativePath(
  92. $themeConfigFile
  93. )
  94. );
  95. } else {
  96. $designPath = $this->resolver->resolve(
  97. RulePool::TYPE_FILE,
  98. 'etc/view.xml',
  99. $this->area,
  100. $this->currentTheme
  101. );
  102. if (file_exists($designPath)) {
  103. try {
  104. $designDom = new \DOMDocument();
  105. $designDom->load($designPath);
  106. $iterator[$designPath] = $designDom->saveXML();
  107. } catch (\Exception $e) {
  108. throw new \Magento\Framework\Exception\LocalizedException(
  109. new \Magento\Framework\Phrase('Could not read config file')
  110. );
  111. }
  112. }
  113. }
  114. break;
  115. default:
  116. $iterator = $this->iteratorFactory->create([]);
  117. break;
  118. }
  119. return $iterator;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function getParents($filename, $scope)
  125. {
  126. switch ($scope) {
  127. case 'global':
  128. $iterator = $this->moduleReader->getConfigurationFiles($filename)->toArray();
  129. $designPath = $this->resolver->resolve(
  130. RulePool::TYPE_FILE,
  131. 'etc/view.xml',
  132. $this->area,
  133. $this->currentTheme
  134. );
  135. if (file_exists($designPath)) {
  136. try {
  137. $iterator = $this->getParentConfigs($this->currentTheme, []);
  138. } catch (\Exception $e) {
  139. throw new \Magento\Framework\Exception\LocalizedException(
  140. new \Magento\Framework\Phrase('Could not read config file')
  141. );
  142. }
  143. }
  144. break;
  145. default:
  146. $iterator = $this->iteratorFactory->create([]);
  147. break;
  148. }
  149. return $iterator;
  150. }
  151. /**
  152. * Recursively add parent theme configs
  153. *
  154. * @param ThemeInterface $theme
  155. * @param array $iterator
  156. * @param int $index
  157. * @return array
  158. */
  159. private function getParentConfigs(ThemeInterface $theme, array $iterator, $index = 0)
  160. {
  161. if ($theme->getParentTheme() && $theme->isPhysical()) {
  162. $parentDesignPath = $this->resolver->resolve(
  163. RulePool::TYPE_FILE,
  164. 'etc/view.xml',
  165. $this->area,
  166. $theme->getParentTheme()
  167. );
  168. $parentDom = new \DOMDocument();
  169. $parentDom->load($parentDesignPath);
  170. $iterator[$index] = $parentDom->saveXML();
  171. $iterator = $this->getParentConfigs($theme->getParentTheme(), $iterator, ++$index);
  172. }
  173. return $iterator;
  174. }
  175. }