Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CopyService.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Theme\Model;
11 
14 
16 {
20  protected $_directory;
21 
25  protected $_fileFactory;
26 
30  protected $_link;
31 
35  protected $_updateFactory;
36 
40  protected $_eventManager;
41 
46 
55  public function __construct(
56  \Magento\Framework\Filesystem $filesystem,
57  \Magento\Framework\View\Design\Theme\FileFactory $fileFactory,
58  \Magento\Widget\Model\Layout\Link $link,
59  \Magento\Widget\Model\Layout\UpdateFactory $updateFactory,
60  \Magento\Framework\Event\ManagerInterface $eventManager,
61  \Magento\Framework\View\Design\Theme\Customization\Path $customization
62  ) {
63  $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
64  $this->_fileFactory = $fileFactory;
65  $this->_link = $link;
66  $this->_updateFactory = $updateFactory;
67  $this->_eventManager = $eventManager;
68  $this->_customizationPath = $customization;
69  }
70 
79  {
80  $this->_copyDatabaseCustomization($source, $target);
81  $this->_copyLayoutCustomization($source, $target);
82  $this->_copyFilesystemCustomization($source, $target);
83  }
84 
92  protected function _copyDatabaseCustomization(ThemeInterface $source, ThemeInterface $target)
93  {
95  foreach ($target->getCustomization()->getFiles() as $themeFile) {
96  $themeFile->delete();
97  }
99  foreach ($source->getCustomization()->getFiles() as $themeFile) {
101  $newThemeFile = $this->_fileFactory->create();
102  $newThemeFile->setData(
103  [
104  'theme_id' => $target->getId(),
105  'file_path' => $themeFile->getFilePath(),
106  'file_type' => $themeFile->getFileType(),
107  'content' => $themeFile->getContent(),
108  'sort_order' => $themeFile->getData('sort_order'),
109  ]
110  );
111  $newThemeFile->save();
112  }
113  }
114 
122  protected function _copyLayoutCustomization(ThemeInterface $source, ThemeInterface $target)
123  {
124  $update = $this->_updateFactory->create();
126  $targetUpdates = $update->getCollection();
127  $targetUpdates->addThemeFilter($target->getId());
128  $targetUpdates->delete();
129 
131  $sourceCollection = $this->_link->getCollection();
132  $sourceCollection->addThemeFilter($source->getId());
134  foreach ($sourceCollection as $layoutLink) {
136  $update = $this->_updateFactory->create();
137  $update->load($layoutLink->getLayoutUpdateId());
138  if ($update->getId()) {
139  $update->setId(null);
140  $update->save();
141  $layoutLink->setThemeId($target->getId());
142  $layoutLink->setLayoutUpdateId($update->getId());
143  $layoutLink->setId(null);
144  $layoutLink->save();
145  }
146  }
147  }
148 
157  {
158  $sourcePath = $this->_customizationPath->getCustomizationPath($source);
159  $targetPath = $this->_customizationPath->getCustomizationPath($target);
160 
161  if (!$sourcePath || !$targetPath) {
162  return;
163  }
164 
165  $this->_deleteFilesRecursively($targetPath);
166 
167  if ($this->_directory->isDirectory($sourcePath)) {
168  $this->_copyFilesRecursively($sourcePath, $sourcePath, $targetPath);
169  }
170  }
171 
180  protected function _copyFilesRecursively($baseDir, $sourceDir, $targetDir)
181  {
182  foreach ($this->_directory->read($sourceDir) as $path) {
183  if ($this->_directory->isDirectory($path)) {
184  $this->_copyFilesRecursively($baseDir, $path, $targetDir);
185  } else {
186  $filePath = substr($path, strlen($baseDir) + 1);
187  $this->_directory->copyFile($path, $targetDir . '/' . $filePath);
188  }
189  }
190  }
191 
198  protected function _deleteFilesRecursively($targetDir)
199  {
200  if ($this->_directory->isExist($targetDir)) {
201  foreach ($this->_directory->read($targetDir) as $path) {
202  $this->_directory->delete($path);
203  }
204  }
205  }
206 }
_copyFilesystemCustomization(ThemeInterface $source, ThemeInterface $target)
$baseDir
Definition: autoload.php:9
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
copy(ThemeInterface $source, ThemeInterface $target)
Definition: CopyService.php:78
_copyFilesRecursively($baseDir, $sourceDir, $targetDir)
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\Framework\View\Design\Theme\FileFactory $fileFactory, \Magento\Widget\Model\Layout\Link $link, \Magento\Widget\Model\Layout\UpdateFactory $updateFactory, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\View\Design\Theme\Customization\Path $customization)
Definition: CopyService.php:55
$filesystem