FlyweightFactory.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 factory
  9. */
  10. class FlyweightFactory
  11. {
  12. /**
  13. * Theme provider
  14. *
  15. * @var ThemeProviderInterface
  16. */
  17. protected $themeProvider;
  18. /**
  19. * Themes
  20. *
  21. * @var \Magento\Framework\View\Design\ThemeInterface[]
  22. */
  23. protected $themes = [];
  24. /**
  25. * Themes by path
  26. *
  27. * @var \Magento\Framework\View\Design\ThemeInterface[]
  28. */
  29. protected $themesByPath = [];
  30. /**
  31. * Constructor
  32. *
  33. * @param ThemeProviderInterface $themeProvider
  34. */
  35. public function __construct(ThemeProviderInterface $themeProvider)
  36. {
  37. $this->themeProvider = $themeProvider;
  38. }
  39. /**
  40. * Creates or returns a shared model of theme
  41. *
  42. * Search for theme in File System by specific path or load theme from DB
  43. * by specific path (e.g. adminhtml/Magento/backend) or by identifier (theme primary key) and return it
  44. * Can be used to deploy static or in other setup commands, even if Magento is not installed yet.
  45. *
  46. * @param string $themeKey Should looks like Magento/backend or should be theme primary key
  47. * @param string $area Can be adminhtml, frontend, etc...
  48. * @return \Magento\Framework\View\Design\ThemeInterface
  49. * @throws \InvalidArgumentException when incorrect $themeKey was specified
  50. * @throws \LogicException when theme with appropriate $themeKey was not found
  51. */
  52. public function create($themeKey, $area = \Magento\Framework\View\DesignInterface::DEFAULT_AREA)
  53. {
  54. if (!is_numeric($themeKey) && !is_string($themeKey)) {
  55. throw new \InvalidArgumentException('Incorrect theme identification key');
  56. }
  57. $themeKey = $this->extractThemeId($themeKey);
  58. if (is_numeric($themeKey)) {
  59. $themeModel = $this->_loadById($themeKey);
  60. } else {
  61. $themeModel = $this->_loadByPath($themeKey, $area);
  62. }
  63. if (!$themeModel->getCode()) {
  64. throw new \LogicException("Unable to load theme by specified key: '{$themeKey}'");
  65. }
  66. $this->_addTheme($themeModel);
  67. return $themeModel;
  68. }
  69. /**
  70. * Attempt to determine a numeric theme ID from the specified path
  71. *
  72. * @param string $path
  73. * @return string
  74. */
  75. private function extractThemeId($path)
  76. {
  77. $dir = \Magento\Framework\View\DesignInterface::PUBLIC_THEME_DIR;
  78. if (preg_match('/^' . preg_quote($dir, '/') . '(\d+)$/', $path, $matches)) {
  79. return $matches[1];
  80. }
  81. return $path;
  82. }
  83. /**
  84. * Load theme by id
  85. *
  86. * @param int $themeId
  87. * @return \Magento\Framework\View\Design\ThemeInterface
  88. */
  89. protected function _loadById($themeId)
  90. {
  91. if (isset($this->themes[$themeId])) {
  92. return $this->themes[$themeId];
  93. }
  94. return $this->themeProvider->getThemeById($themeId);
  95. }
  96. /**
  97. * Load theme by theme path
  98. *
  99. * @param string $themePath
  100. * @param string $area
  101. * @return \Magento\Framework\View\Design\ThemeInterface
  102. */
  103. protected function _loadByPath($themePath, $area)
  104. {
  105. $fullPath = $area . \Magento\Framework\View\Design\ThemeInterface::PATH_SEPARATOR . $themePath;
  106. if (isset($this->themesByPath[$fullPath])) {
  107. return $this->themesByPath[$fullPath];
  108. }
  109. return $this->themeProvider->getThemeByFullPath($fullPath);
  110. }
  111. /**
  112. * Add theme to shared collection
  113. *
  114. * @param \Magento\Framework\View\Design\ThemeInterface $themeModel
  115. * @return $this
  116. */
  117. protected function _addTheme(\Magento\Framework\View\Design\ThemeInterface $themeModel)
  118. {
  119. if ($themeModel->getId()) {
  120. $this->themes[$themeModel->getId()] = $themeModel;
  121. $themePath = $themeModel->getFullPath();
  122. if ($themePath) {
  123. $this->themesByPath[$themePath] = $themeModel;
  124. }
  125. }
  126. return $this;
  127. }
  128. }