Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FlyweightFactory.php
Go to the documentation of this file.
1 <?php
7 
12 {
18  protected $themeProvider;
19 
25  protected $themes = [];
26 
32  protected $themesByPath = [];
33 
40  {
41  $this->themeProvider = $themeProvider;
42  }
43 
57  public function create($themeKey, $area = \Magento\Framework\View\DesignInterface::DEFAULT_AREA)
58  {
59  if (!is_numeric($themeKey) && !is_string($themeKey)) {
60  throw new \InvalidArgumentException('Incorrect theme identification key');
61  }
62  $themeKey = $this->extractThemeId($themeKey);
63  if (is_numeric($themeKey)) {
64  $themeModel = $this->_loadById($themeKey);
65  } else {
66  $themeModel = $this->_loadByPath($themeKey, $area);
67  }
68  if (!$themeModel->getCode()) {
69  throw new \LogicException("Unable to load theme by specified key: '{$themeKey}'");
70  }
71  $this->_addTheme($themeModel);
72  return $themeModel;
73  }
74 
81  private function extractThemeId($path)
82  {
84  if (preg_match('/^' . preg_quote($dir, '/') . '(\d+)$/', $path, $matches)) {
85  return $matches[1];
86  }
87  return $path;
88  }
89 
96  protected function _loadById($themeId)
97  {
98  if (isset($this->themes[$themeId])) {
99  return $this->themes[$themeId];
100  }
101 
102  return $this->themeProvider->getThemeById($themeId);
103  }
104 
112  protected function _loadByPath($themePath, $area)
113  {
114  $fullPath = $area . \Magento\Framework\View\Design\ThemeInterface::PATH_SEPARATOR . $themePath;
115  if (isset($this->themesByPath[$fullPath])) {
116  return $this->themesByPath[$fullPath];
117  }
118 
119  return $this->themeProvider->getThemeByFullPath($fullPath);
120  }
121 
128  protected function _addTheme(\Magento\Framework\View\Design\ThemeInterface $themeModel)
129  {
130  if ($themeModel->getId()) {
131  $this->themes[$themeModel->getId()] = $themeModel;
132  $themePath = $themeModel->getFullPath();
133  if ($themePath) {
134  $this->themesByPath[$themePath] = $themeModel;
135  }
136  }
137  return $this;
138  }
139 }
create($themeKey, $area=\Magento\Framework\View\DesignInterface::DEFAULT_AREA)
_addTheme(\Magento\Framework\View\Design\ThemeInterface $themeModel)
__construct(ThemeProviderInterface $themeProvider)