Virtual.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Virtual theme domain model
  8. */
  9. namespace Magento\Theme\Model\Theme\Domain;
  10. class Virtual implements \Magento\Framework\View\Design\Theme\Domain\VirtualInterface
  11. {
  12. /**
  13. * Virtual theme model instance
  14. *
  15. * @var \Magento\Framework\View\Design\ThemeInterface
  16. */
  17. protected $_theme;
  18. /**
  19. * @var \Magento\Theme\Model\ThemeFactory $themeFactory
  20. */
  21. protected $_themeFactory;
  22. /**
  23. * Staging theme model instance
  24. *
  25. * @var \Magento\Framework\View\Design\ThemeInterface
  26. */
  27. protected $_stagingTheme;
  28. /**
  29. * @var \Magento\Theme\Model\CopyService
  30. */
  31. protected $_themeCopyService;
  32. /**
  33. * Theme customization config
  34. *
  35. * @var \Magento\Theme\Model\Config\Customization
  36. */
  37. protected $_customizationConfig;
  38. /**
  39. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  40. * @param \Magento\Theme\Model\ThemeFactory $themeFactory
  41. * @param \Magento\Theme\Model\CopyService $themeCopyService
  42. * @param \Magento\Theme\Model\Config\Customization $customizationConfig
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Design\ThemeInterface $theme,
  46. \Magento\Theme\Model\ThemeFactory $themeFactory,
  47. \Magento\Theme\Model\CopyService $themeCopyService,
  48. \Magento\Theme\Model\Config\Customization $customizationConfig
  49. ) {
  50. $this->_theme = $theme;
  51. $this->_themeFactory = $themeFactory;
  52. $this->_themeCopyService = $themeCopyService;
  53. $this->_customizationConfig = $customizationConfig;
  54. }
  55. /**
  56. * Get 'staging' theme
  57. *
  58. * @return \Magento\Framework\View\Design\ThemeInterface
  59. */
  60. public function getStagingTheme()
  61. {
  62. if (!$this->_stagingTheme) {
  63. $this->_stagingTheme = $this->_theme->getStagingVersion();
  64. if (!$this->_stagingTheme) {
  65. $this->_stagingTheme = $this->_createStagingTheme();
  66. $this->_themeCopyService->copy($this->_theme, $this->_stagingTheme);
  67. }
  68. }
  69. return $this->_stagingTheme;
  70. }
  71. /**
  72. * Get 'physical' theme
  73. *
  74. * @return \Magento\Framework\View\Design\ThemeInterface
  75. */
  76. public function getPhysicalTheme()
  77. {
  78. /** @var $parentTheme \Magento\Framework\View\Design\ThemeInterface */
  79. $parentTheme = $this->_theme->getParentTheme();
  80. while ($parentTheme && !$parentTheme->isPhysical()) {
  81. $parentTheme = $parentTheme->getParentTheme();
  82. }
  83. if (!$parentTheme || !$parentTheme->getId()) {
  84. return null;
  85. }
  86. return $parentTheme;
  87. }
  88. /**
  89. * Check if theme is assigned to ANY store
  90. *
  91. * @return bool
  92. */
  93. public function isAssigned()
  94. {
  95. return $this->_customizationConfig->isThemeAssignedToStore($this->_theme);
  96. }
  97. /**
  98. * Create 'staging' theme associated with current 'virtual' theme
  99. *
  100. * @return \Magento\Framework\View\Design\ThemeInterface
  101. */
  102. protected function _createStagingTheme()
  103. {
  104. $stagingTheme = $this->_themeFactory->create();
  105. $stagingTheme->setData(
  106. [
  107. 'parent_id' => $this->_theme->getId(),
  108. 'theme_path' => null,
  109. 'theme_title' => sprintf('%s - Staging', $this->_theme->getThemeTitle()),
  110. 'preview_image' => $this->_theme->getPreviewImage(),
  111. 'is_featured' => $this->_theme->getIsFeatured(),
  112. 'type' => \Magento\Framework\View\Design\ThemeInterface::TYPE_STAGING,
  113. ]
  114. );
  115. $stagingTheme->save();
  116. return $stagingTheme;
  117. }
  118. }