ThemeProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Theme;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. use Magento\Framework\View\Design\Theme\ListInterface;
  10. use Magento\Framework\App\DeploymentConfig;
  11. /**
  12. * Provide data for theme grid and for theme edit page
  13. */
  14. class ThemeProvider implements \Magento\Framework\View\Design\Theme\ThemeProviderInterface
  15. {
  16. /**
  17. * @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory
  18. */
  19. protected $collectionFactory;
  20. /**
  21. * @var \Magento\Theme\Model\ThemeFactory
  22. */
  23. protected $themeFactory;
  24. /**
  25. * @var \Magento\Framework\App\CacheInterface
  26. */
  27. protected $cache;
  28. /**
  29. * @var \Magento\Framework\View\Design\ThemeInterface[]
  30. */
  31. private $themes;
  32. /**
  33. * @var ListInterface
  34. */
  35. private $themeList;
  36. /**
  37. * @var DeploymentConfig
  38. */
  39. private $deploymentConfig;
  40. /**
  41. * @var Json
  42. */
  43. private $serializer;
  44. /**
  45. * ThemeProvider constructor.
  46. *
  47. * @param \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory
  48. * @param \Magento\Theme\Model\ThemeFactory $themeFactory
  49. * @param \Magento\Framework\App\CacheInterface $cache
  50. * @param Json $serializer
  51. */
  52. public function __construct(
  53. \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory,
  54. \Magento\Theme\Model\ThemeFactory $themeFactory,
  55. \Magento\Framework\App\CacheInterface $cache,
  56. Json $serializer = null
  57. ) {
  58. $this->collectionFactory = $collectionFactory;
  59. $this->themeFactory = $themeFactory;
  60. $this->cache = $cache;
  61. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function getThemeByFullPath($fullPath)
  67. {
  68. if (isset($this->themes[$fullPath])) {
  69. return $this->themes[$fullPath];
  70. }
  71. if (! $this->getDeploymentConfig()->isDbAvailable()) {
  72. return $this->getThemeList()->getThemeByFullPath($fullPath);
  73. }
  74. $theme = $this->loadThemeFromCache('theme' . $fullPath);
  75. if ($theme) {
  76. $this->themes[$fullPath] = $theme;
  77. return $theme;
  78. }
  79. $themeCollection = $this->collectionFactory->create();
  80. $theme = $themeCollection->getThemeByFullPath($fullPath);
  81. if ($theme->getId()) {
  82. $this->saveThemeToCache($theme, 'theme' . $fullPath);
  83. $this->saveThemeToCache($theme, 'theme-by-id-' . $theme->getId());
  84. $this->themes[$fullPath] = $theme;
  85. }
  86. return $theme;
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. public function getThemeCustomizations(
  92. $area = \Magento\Framework\App\Area::AREA_FRONTEND,
  93. $type = \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL
  94. ) {
  95. /** @var $themeCollection \Magento\Theme\Model\ResourceModel\Theme\Collection */
  96. $themeCollection = $this->collectionFactory->create();
  97. $themeCollection->addAreaFilter($area)->addTypeFilter($type);
  98. return $themeCollection;
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function getThemeById($themeId)
  104. {
  105. if (isset($this->themes[$themeId])) {
  106. return $this->themes[$themeId];
  107. }
  108. $theme = $this->loadThemeFromCache('theme-by-id-' . $themeId);
  109. if ($theme) {
  110. $this->themes[$themeId] = $theme;
  111. return $theme;
  112. }
  113. $theme = $this->themeFactory->create();
  114. $theme->load($themeId);
  115. if ($theme->getId()) {
  116. // We only cache by ID, as virtual themes may share the same path
  117. $this->saveThemeToCache($theme, 'theme-by-id-' . $themeId);
  118. $this->themes[$themeId] = $theme;
  119. }
  120. return $theme;
  121. }
  122. /**
  123. * Load Theme model from cache
  124. *
  125. * @param string $cacheId
  126. * @return \Magento\Theme\Model\Theme|null
  127. */
  128. private function loadThemeFromCache($cacheId)
  129. {
  130. $themeData = $this->cache->load($cacheId);
  131. if ($themeData) {
  132. $themeData = $this->serializer->unserialize($themeData);
  133. $theme = $this->themeFactory->create()->populateFromArray($themeData);
  134. return $theme;
  135. }
  136. return null;
  137. }
  138. /**
  139. * Save Theme model to the cache
  140. *
  141. * @param \Magento\Theme\Model\Theme $theme
  142. * @param string $cacheId
  143. * @return void
  144. */
  145. private function saveThemeToCache(\Magento\Theme\Model\Theme $theme, $cacheId)
  146. {
  147. $themeData = $this->serializer->serialize($theme->toArray());
  148. $this->cache->save($themeData, $cacheId);
  149. }
  150. /**
  151. * @deprecated 100.1.3
  152. * @return ListInterface
  153. */
  154. private function getThemeList()
  155. {
  156. if ($this->themeList === null) {
  157. $this->themeList = ObjectManager::getInstance()->get(ListInterface::class);
  158. }
  159. return $this->themeList;
  160. }
  161. /**
  162. * @deprecated 100.1.3
  163. * @return DeploymentConfig
  164. */
  165. private function getDeploymentConfig()
  166. {
  167. if ($this->deploymentConfig === null) {
  168. $this->deploymentConfig = ObjectManager::getInstance()->get(DeploymentConfig::class);
  169. }
  170. return $this->deploymentConfig;
  171. }
  172. }