Registration.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Theme\Plugin;
  7. use Magento\Backend\App\AbstractAction;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Theme\Model\Theme\Registration as ThemeRegistration;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Psr\Log\LoggerInterface;
  12. use Magento\Framework\App\State as AppState;
  13. use Magento\Theme\Model\Theme\Collection as ThemeCollection;
  14. use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeLoader;
  15. use Magento\Framework\Config\Theme;
  16. /**
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class Registration
  20. {
  21. /**
  22. * @var \Magento\Theme\Model\Theme\Registration
  23. */
  24. protected $themeRegistration;
  25. /**
  26. * @var \Magento\Theme\Model\Theme\Collection
  27. */
  28. protected $themeCollection;
  29. /**
  30. * @var \Magento\Theme\Model\ResourceModel\Theme\Collection
  31. */
  32. protected $themeLoader;
  33. /**
  34. * @var \Psr\Log\LoggerInterface
  35. */
  36. protected $logger;
  37. /**
  38. * @var \Magento\Framework\App\State
  39. */
  40. protected $appState;
  41. /**
  42. * @param ThemeRegistration $themeRegistration
  43. * @param ThemeCollection $themeCollection
  44. * @param ThemeLoader $themeLoader
  45. * @param LoggerInterface $logger
  46. * @param AppState $appState
  47. */
  48. public function __construct(
  49. ThemeRegistration $themeRegistration,
  50. ThemeCollection $themeCollection,
  51. ThemeLoader $themeLoader,
  52. LoggerInterface $logger,
  53. AppState $appState
  54. ) {
  55. $this->themeRegistration = $themeRegistration;
  56. $this->themeCollection = $themeCollection;
  57. $this->themeLoader = $themeLoader;
  58. $this->logger = $logger;
  59. $this->appState = $appState;
  60. }
  61. /**
  62. * Add new theme from filesystem and update existing
  63. *
  64. * @param AbstractAction $subject
  65. * @param RequestInterface $request
  66. *
  67. * @return void
  68. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  69. */
  70. public function beforeDispatch(
  71. AbstractAction $subject,
  72. RequestInterface $request
  73. ) {
  74. try {
  75. if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
  76. $this->themeRegistration->register();
  77. $this->updateThemeData();
  78. }
  79. } catch (LocalizedException $e) {
  80. $this->logger->critical($e);
  81. }
  82. }
  83. /**
  84. * Update theme data
  85. *
  86. * @return void
  87. */
  88. protected function updateThemeData()
  89. {
  90. $themesFromConfig = $this->themeCollection->loadData();
  91. /** @var \Magento\Theme\Model\Theme $themeFromConfig */
  92. foreach ($themesFromConfig as $themeFromConfig) {
  93. /** @var \Magento\Theme\Model\Theme $themeFromDb */
  94. $themeFromDb = $this->themeLoader->getThemeByFullPath(
  95. $themeFromConfig->getArea()
  96. . Theme::THEME_PATH_SEPARATOR
  97. . $themeFromConfig->getThemePath()
  98. );
  99. if ($themeFromConfig->getParentTheme()) {
  100. $parentThemeFromDb = $this->themeLoader->getThemeByFullPath(
  101. $themeFromConfig->getParentTheme()->getFullPath()
  102. );
  103. $themeFromDb->setParentId($parentThemeFromDb->getId());
  104. }
  105. $themeFromDb->setThemeTitle($themeFromConfig->getThemeTitle());
  106. $themeFromDb->save();
  107. }
  108. }
  109. }