Customization.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Theme;
  7. /**
  8. * Theme customizations manager
  9. */
  10. class Customization implements CustomizationInterface
  11. {
  12. /**
  13. * File provider
  14. *
  15. * @var \Magento\Framework\View\Design\Theme\FileProviderInterface
  16. */
  17. protected $fileProvider;
  18. /**
  19. * Theme customization path
  20. *
  21. * @var \Magento\Framework\View\Design\Theme\Customization\Path
  22. */
  23. protected $customizationPath;
  24. /**
  25. * Theme
  26. *
  27. * @var \Magento\Framework\View\Design\ThemeInterface
  28. */
  29. protected $theme;
  30. /**
  31. * Theme files
  32. *
  33. * @var \Magento\Framework\View\Design\Theme\FileInterface[]
  34. */
  35. protected $themeFiles;
  36. /**
  37. * Theme files by type
  38. *
  39. * @var \Magento\Framework\View\Design\Theme\FileInterface[]
  40. */
  41. protected $themeFilesByType = [];
  42. /**
  43. * Constructor
  44. *
  45. * @param \Magento\Framework\View\Design\Theme\FileProviderInterface $fileProvider
  46. * @param \Magento\Framework\View\Design\Theme\Customization\Path $customizationPath
  47. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  48. */
  49. public function __construct(
  50. \Magento\Framework\View\Design\Theme\FileProviderInterface $fileProvider,
  51. \Magento\Framework\View\Design\Theme\Customization\Path $customizationPath,
  52. \Magento\Framework\View\Design\ThemeInterface $theme = null
  53. ) {
  54. $this->fileProvider = $fileProvider;
  55. $this->customizationPath = $customizationPath;
  56. $this->theme = $theme;
  57. }
  58. /**
  59. * Retrieve list of files which belong to a theme
  60. *
  61. * @return \Magento\Framework\View\Design\Theme\FileInterface[]
  62. */
  63. public function getFiles()
  64. {
  65. if (!$this->themeFiles) {
  66. $this->themeFiles = $this->fileProvider->getItems($this->theme);
  67. }
  68. return $this->themeFiles;
  69. }
  70. /**
  71. * Retrieve list of files which belong to a theme only by type
  72. *
  73. * @param string $type
  74. * @return \Magento\Framework\View\Design\Theme\FileInterface[]
  75. */
  76. public function getFilesByType($type)
  77. {
  78. if (!isset($this->themeFilesByType[$type])) {
  79. $this->themeFilesByType[$type] = $this->fileProvider->getItems($this->theme, ['file_type' => $type]);
  80. }
  81. return $this->themeFilesByType[$type];
  82. }
  83. /**
  84. * Get short file information
  85. *
  86. * @param \Magento\Framework\View\Design\Theme\FileInterface[] $files
  87. * @return array
  88. */
  89. public function generateFileInfo(array $files)
  90. {
  91. $filesInfo = [];
  92. /** @var $file \Magento\Framework\View\Design\Theme\FileInterface */
  93. foreach ($files as $file) {
  94. if ($file instanceof \Magento\Framework\View\Design\Theme\FileInterface) {
  95. $filesInfo[] = $file->getFileInfo();
  96. }
  97. }
  98. return $filesInfo;
  99. }
  100. /**
  101. * Returns customization absolute path
  102. *
  103. * @return null|string
  104. */
  105. public function getCustomizationPath()
  106. {
  107. return $this->customizationPath->getCustomizationPath($this->theme);
  108. }
  109. /**
  110. * Get directory where themes files are stored
  111. *
  112. * @return null|string
  113. */
  114. public function getThemeFilesPath()
  115. {
  116. return $this->theme->isPhysical() ? $this->customizationPath->getThemeFilesPath(
  117. $this->theme
  118. ) : $this->customizationPath->getCustomizationPath(
  119. $this->theme
  120. );
  121. }
  122. /**
  123. * Get path to custom view configuration file
  124. *
  125. * @return null|string
  126. */
  127. public function getCustomViewConfigPath()
  128. {
  129. return $this->customizationPath->getCustomViewConfigPath($this->theme);
  130. }
  131. /**
  132. * Reorder
  133. *
  134. * @param string $type
  135. * @param array $sequence
  136. * @return $this|CustomizationInterface
  137. */
  138. public function reorder($type, array $sequence)
  139. {
  140. $sortOrderSequence = array_flip(array_values($sequence));
  141. /** @var $file \Magento\Framework\View\Design\Theme\FileInterface */
  142. foreach ($this->getFilesByType($type) as $file) {
  143. if (isset($sortOrderSequence[$file->getId()])) {
  144. $prevSortOrder = $file->getData('sort_order');
  145. $currentSortOrder = $sortOrderSequence[$file->getId()];
  146. if ($prevSortOrder !== $currentSortOrder) {
  147. $file->setData('sort_order', $currentSortOrder);
  148. $file->save();
  149. }
  150. }
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Remove custom files by ids
  156. *
  157. * @param array $fileIds
  158. * @return $this
  159. */
  160. public function delete(array $fileIds)
  161. {
  162. /** @var $file \Magento\Framework\View\Design\Theme\FileInterface */
  163. foreach ($this->getFiles() as $file) {
  164. if (in_array($file->getId(), $fileIds)) {
  165. $file->delete();
  166. }
  167. }
  168. return $this;
  169. }
  170. }