Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Importer.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory;
13 use Magento\Theme\Model\Theme\Collection as ThemeFilesystemCollection;
16 
28 class Importer implements ImporterInterface
29 {
35  private $themeFilesystemCollection;
36 
42  private $themeCollectionFactory;
43 
49  private $themeRegistration;
50 
56  private $themeResourceModel;
57 
64  public function __construct(
65  ThemeFilesystemCollection $themeFilesystemCollection,
66  CollectionFactory $collectionFactory,
67  Registration $registration,
68  ThemeResourceModel $themeResourceModel
69  ) {
70  $this->themeFilesystemCollection = $themeFilesystemCollection;
71  $this->themeCollectionFactory = $collectionFactory;
72  $this->themeRegistration = $registration;
73  $this->themeResourceModel = $themeResourceModel;
74  }
75 
79  public function import(array $data)
80  {
81  $messages = ['<info>Theme import was started.</info>'];
82 
83  try {
84  // Registers themes from filesystem
85  $this->themeRegistration->register();
86 
88  $collection = $this->themeCollectionFactory->create();
89 
90  // List of themes full paths which are located in filesystem
91  $themesInFs = $this->themeFilesystemCollection->getAllIds();
92 
97  foreach ($collection->getItems() as $theme) {
98  $themeFullPath = $theme->getFullPath();
99  if (!key_exists($themeFullPath, $data) && !in_array($themeFullPath, $themesInFs)) {
100  $this->themeResourceModel->delete($theme);
101  }
102  }
103  } catch (\Exception $exception) {
104  throw new InvalidTransitionException(__('%1', $exception->getMessage()), $exception);
105  }
106 
107  $messages[] = '<info>Theme import finished.</info>';
108 
109  return $messages;
110  }
111 
119  public function getWarningMessages(array $data)
120  {
121  $themesInFile = array_keys($data);
122  $themesInDb = [];
123 
125  $collection = $this->themeCollectionFactory->create();
126 
128  foreach ($collection->getItems() as $theme) {
129  $themesInDb[] = $theme->getFullPath();
130  }
131 
132  $toBeRegistered = $this->themeFilesystemCollection->getAllIds();
133  $toBeRemoved = array_diff($themesInDb, $toBeRegistered, $themesInFile);
134  $newThemes = array_diff($toBeRegistered, $themesInDb);
135 
136  $messages = [];
137 
138  if ($newThemes) {
139  $messages[] = '<info>The following themes will be registered:</info> ' . implode(', ', $newThemes);
140  }
141 
142  if ($toBeRemoved) {
143  $messages[] = '<info>The following themes will be removed:</info> ' . implode(', ', $toBeRemoved);
144  }
145 
146  return $messages;
147  }
148 }
__construct(ThemeFilesystemCollection $themeFilesystemCollection, CollectionFactory $collectionFactory, Registration $registration, ThemeResourceModel $themeResourceModel)
Definition: Importer.php:64
__()
Definition: __.php:13
$theme