Physical.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Physical theme model class
  8. */
  9. namespace Magento\Theme\Model\Theme\Domain;
  10. class Physical implements \Magento\Framework\View\Design\Theme\Domain\PhysicalInterface
  11. {
  12. /**
  13. * Physical theme model instance
  14. *
  15. * @var \Magento\Framework\View\Design\ThemeInterface
  16. */
  17. protected $_theme;
  18. /**
  19. * @var \Magento\Theme\Model\ThemeFactory
  20. */
  21. protected $_themeFactory;
  22. /**
  23. * @var \Magento\Theme\Model\CopyService
  24. */
  25. protected $_themeCopyService;
  26. /**
  27. * @var \Magento\Theme\Model\ResourceModel\Theme\Collection
  28. */
  29. protected $_themeCollection;
  30. /**
  31. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  32. * @param \Magento\Theme\Model\ThemeFactory $themeFactory
  33. * @param \Magento\Theme\Model\CopyService $themeCopyService
  34. * @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Design\ThemeInterface $theme,
  38. \Magento\Theme\Model\ThemeFactory $themeFactory,
  39. \Magento\Theme\Model\CopyService $themeCopyService,
  40. \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
  41. ) {
  42. $this->_theme = $theme;
  43. $this->_themeFactory = $themeFactory;
  44. $this->_themeCopyService = $themeCopyService;
  45. $this->_themeCollection = $themeCollection;
  46. }
  47. /**
  48. * Create theme customization
  49. *
  50. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  51. * @return \Magento\Framework\View\Design\ThemeInterface
  52. */
  53. public function createVirtualTheme($theme)
  54. {
  55. $themeData = $theme->getData();
  56. $themeData['parent_id'] = $theme->getId();
  57. $themeData['theme_id'] = null;
  58. $themeData['theme_path'] = null;
  59. $themeData['theme_title'] = $this->_getVirtualThemeTitle($theme);
  60. $themeData['type'] = \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL;
  61. /** @var $themeCustomization \Magento\Framework\View\Design\ThemeInterface */
  62. $themeCustomization = $this->_themeFactory->create()->setData($themeData);
  63. $themeCustomization->getThemeImage()->createPreviewImageCopy($theme);
  64. $themeCustomization->save();
  65. $this->_themeCopyService->copy($theme, $themeCustomization);
  66. return $themeCustomization;
  67. }
  68. /**
  69. * Get virtual theme title
  70. *
  71. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  72. * @return string
  73. */
  74. protected function _getVirtualThemeTitle($theme)
  75. {
  76. $themeCopyCount = $this->_themeCollection->addAreaFilter(
  77. \Magento\Framework\App\Area::AREA_FRONTEND
  78. )->addTypeFilter(
  79. \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL
  80. )->addFilter(
  81. 'parent_id',
  82. $theme->getId()
  83. )->count();
  84. $title = sprintf("%s - %s #%s", $theme->getThemeTitle(), __('Copy'), $themeCopyCount + 1);
  85. return $title;
  86. }
  87. }