Importer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Config;
  7. use Magento\Framework\App\DeploymentConfig\ImporterInterface;
  8. use Magento\Framework\Exception\State\InvalidTransitionException;
  9. use Magento\Theme\Model\ResourceModel\Theme as ThemeResourceModel;
  10. use Magento\Theme\Model\ResourceModel\Theme\Data\Collection as ThemeDbCollection;
  11. use Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory;
  12. use Magento\Theme\Model\Theme\Collection as ThemeFilesystemCollection;
  13. use Magento\Theme\Model\Theme\Data;
  14. use Magento\Theme\Model\Theme\Registration;
  15. /**
  16. * Imports themes from configurations files.
  17. *
  18. * If a theme is not presented in the configuration files and in the filesystem, but is presented in the DB -
  19. * removes its registration.
  20. * If a theme is not presented in the configuration files and in the DB, but is presented in the filesystem -
  21. * register the theme in the DB.
  22. * If a theme is presented in the configuration files and in the filesystem, but is not presented in the DB -
  23. * register the theme in the DB.
  24. * In other cases - do nothing.
  25. */
  26. class Importer implements ImporterInterface
  27. {
  28. /**
  29. * Collection of themes from the filesystem.
  30. *
  31. * @var ThemeFilesystemCollection
  32. */
  33. private $themeFilesystemCollection;
  34. /**
  35. * Factory of themes collection from the DB.
  36. *
  37. * @var CollectionFactory
  38. */
  39. private $themeCollectionFactory;
  40. /**
  41. * Registrar of themes registers themes in the DB.
  42. *
  43. * @var Registration
  44. */
  45. private $themeRegistration;
  46. /**
  47. * Resource model of theme.
  48. *
  49. * @var ThemeResourceModel
  50. */
  51. private $themeResourceModel;
  52. /**
  53. * @param ThemeFilesystemCollection $themeFilesystemCollection The collection of themes from the filesystem
  54. * @param CollectionFactory $collectionFactory The factory of themes collection from the DB
  55. * @param Registration $registration The registrar of themes registers themes in the DB
  56. * @param ThemeResourceModel $themeResourceModel The resource model of theme
  57. */
  58. public function __construct(
  59. ThemeFilesystemCollection $themeFilesystemCollection,
  60. CollectionFactory $collectionFactory,
  61. Registration $registration,
  62. ThemeResourceModel $themeResourceModel
  63. ) {
  64. $this->themeFilesystemCollection = $themeFilesystemCollection;
  65. $this->themeCollectionFactory = $collectionFactory;
  66. $this->themeRegistration = $registration;
  67. $this->themeResourceModel = $themeResourceModel;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function import(array $data)
  73. {
  74. $messages = ['<info>Theme import was started.</info>'];
  75. try {
  76. // Registers themes from filesystem
  77. $this->themeRegistration->register();
  78. /** @var ThemeDbCollection $collection */
  79. $collection = $this->themeCollectionFactory->create();
  80. // List of themes full paths which are located in filesystem
  81. $themesInFs = $this->themeFilesystemCollection->getAllIds();
  82. /**
  83. * Removes themes if they are not present in configuration files and filesystem
  84. * @var Data $theme
  85. */
  86. foreach ($collection->getItems() as $theme) {
  87. $themeFullPath = $theme->getFullPath();
  88. if (!key_exists($themeFullPath, $data) && !in_array($themeFullPath, $themesInFs)) {
  89. $this->themeResourceModel->delete($theme);
  90. }
  91. }
  92. } catch (\Exception $exception) {
  93. throw new InvalidTransitionException(__('%1', $exception->getMessage()), $exception);
  94. }
  95. $messages[] = '<info>Theme import finished.</info>';
  96. return $messages;
  97. }
  98. /**
  99. * Returns array of warning messages which contain information about which changes (removing, registration)
  100. * will be applied to themes.
  101. *
  102. * @param array $data The data that should be imported, used for creating warning messages
  103. * @return array
  104. */
  105. public function getWarningMessages(array $data)
  106. {
  107. $themesInFile = array_keys($data);
  108. $themesInDb = [];
  109. /** @var ThemeDbCollection $collection */
  110. $collection = $this->themeCollectionFactory->create();
  111. /** @var Data $theme */
  112. foreach ($collection->getItems() as $theme) {
  113. $themesInDb[] = $theme->getFullPath();
  114. }
  115. $toBeRegistered = $this->themeFilesystemCollection->getAllIds();
  116. $toBeRemoved = array_diff($themesInDb, $toBeRegistered, $themesInFile);
  117. $newThemes = array_diff($toBeRegistered, $themesInDb);
  118. $messages = [];
  119. if ($newThemes) {
  120. $messages[] = '<info>The following themes will be registered:</info> ' . implode(', ', $newThemes);
  121. }
  122. if ($toBeRemoved) {
  123. $messages[] = '<info>The following themes will be removed:</info> ' . implode(', ', $toBeRemoved);
  124. }
  125. return $messages;
  126. }
  127. }