Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeProvider.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  protected $collectionFactory;
22 
26  protected $themeFactory;
27 
31  protected $cache;
32 
36  private $themes;
37 
41  private $themeList;
42 
46  private $deploymentConfig;
47 
51  private $serializer;
52 
61  public function __construct(
62  \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory,
63  \Magento\Theme\Model\ThemeFactory $themeFactory,
64  \Magento\Framework\App\CacheInterface $cache,
65  Json $serializer = null
66  ) {
67  $this->collectionFactory = $collectionFactory;
68  $this->themeFactory = $themeFactory;
69  $this->cache = $cache;
70  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
71  }
72 
76  public function getThemeByFullPath($fullPath)
77  {
78  if (isset($this->themes[$fullPath])) {
79  return $this->themes[$fullPath];
80  }
81 
82  if (! $this->getDeploymentConfig()->isDbAvailable()) {
83  return $this->getThemeList()->getThemeByFullPath($fullPath);
84  }
85 
86  $theme = $this->loadThemeFromCache('theme' . $fullPath);
87  if ($theme) {
88  $this->themes[$fullPath] = $theme;
89  return $theme;
90  }
91  $themeCollection = $this->collectionFactory->create();
92  $theme = $themeCollection->getThemeByFullPath($fullPath);
93  if ($theme->getId()) {
94  $this->saveThemeToCache($theme, 'theme' . $fullPath);
95  $this->saveThemeToCache($theme, 'theme-by-id-' . $theme->getId());
96  $this->themes[$fullPath] = $theme;
97  }
98 
99  return $theme;
100  }
101 
105  public function getThemeCustomizations(
106  $area = \Magento\Framework\App\Area::AREA_FRONTEND,
108  ) {
110  $themeCollection = $this->collectionFactory->create();
111  $themeCollection->addAreaFilter($area)->addTypeFilter($type);
112  return $themeCollection;
113  }
114 
118  public function getThemeById($themeId)
119  {
120  if (isset($this->themes[$themeId])) {
121  return $this->themes[$themeId];
122  }
123  $theme = $this->loadThemeFromCache('theme-by-id-' . $themeId);
124  if ($theme) {
125  $this->themes[$themeId] = $theme;
126  return $theme;
127  }
128  $theme = $this->themeFactory->create();
129  $theme->load($themeId);
130  if ($theme->getId()) {
131  // We only cache by ID, as virtual themes may share the same path
132  $this->saveThemeToCache($theme, 'theme-by-id-' . $themeId);
133  $this->themes[$themeId] = $theme;
134  }
135  return $theme;
136  }
137 
144  private function loadThemeFromCache($cacheId)
145  {
146  $themeData = $this->cache->load($cacheId);
147  if ($themeData) {
148  $themeData = $this->serializer->unserialize($themeData);
149  $theme = $this->themeFactory->create()->populateFromArray($themeData);
150  return $theme;
151  }
152 
153  return null;
154  }
155 
163  private function saveThemeToCache(\Magento\Theme\Model\Theme $theme, $cacheId)
164  {
165  $themeData = $this->serializer->serialize($theme->toArray());
166  $this->cache->save($themeData, $cacheId);
167  }
168 
173  private function getThemeList()
174  {
175  if ($this->themeList === null) {
176  $this->themeList = ObjectManager::getInstance()->get(ListInterface::class);
177  }
178  return $this->themeList;
179  }
180 
185  private function getDeploymentConfig()
186  {
187  if ($this->deploymentConfig === null) {
188  $this->deploymentConfig = ObjectManager::getInstance()->get(DeploymentConfig::class);
189  }
190  return $this->deploymentConfig;
191  }
192 }
getThemeCustomizations($area, $type=\Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL)
$type
Definition: item.phtml:13
$theme
__construct(\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory, \Magento\Theme\Model\ThemeFactory $themeFactory, \Magento\Framework\App\CacheInterface $cache, Json $serializer=null)