AbstractFile.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Theme\Customization;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Theme file service abstract class
  10. */
  11. abstract class AbstractFile implements
  12. \Magento\Framework\View\Design\Theme\Customization\FileInterface,
  13. \Magento\Framework\View\Design\Theme\Customization\FileAssetInterface
  14. {
  15. /**
  16. * Customization path
  17. *
  18. * @var \Magento\Framework\View\Design\Theme\Customization\Path
  19. */
  20. protected $_customizationPath;
  21. /**
  22. * File factory
  23. *
  24. * @var \Magento\Framework\View\Design\Theme\FileFactory
  25. */
  26. protected $_fileFactory;
  27. /**
  28. * File system
  29. *
  30. * @var \Magento\Framework\Filesystem
  31. */
  32. protected $_filesystem;
  33. /**
  34. * Constructor
  35. *
  36. * @param \Magento\Framework\View\Design\Theme\Customization\Path $customizationPath
  37. * @param \Magento\Framework\View\Design\Theme\FileFactory $fileFactory
  38. * @param \Magento\Framework\Filesystem $filesystem
  39. */
  40. public function __construct(
  41. \Magento\Framework\View\Design\Theme\Customization\Path $customizationPath,
  42. \Magento\Framework\View\Design\Theme\FileFactory $fileFactory,
  43. \Magento\Framework\Filesystem $filesystem
  44. ) {
  45. $this->_customizationPath = $customizationPath;
  46. $this->_fileFactory = $fileFactory;
  47. $this->_filesystem = $filesystem;
  48. }
  49. /**
  50. * Create class instance with specified parameters
  51. *
  52. * @return \Magento\Framework\View\Design\Theme\FileInterface
  53. */
  54. public function create()
  55. {
  56. $file = $this->_fileFactory->create();
  57. $file->setCustomizationService($this);
  58. return $file;
  59. }
  60. /**
  61. * Returns customization full path
  62. *
  63. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  64. * @return string
  65. */
  66. public function getFullPath(\Magento\Framework\View\Design\Theme\FileInterface $file)
  67. {
  68. $customizationPath = $this->_customizationPath->getCustomizationPath($file->getTheme());
  69. return $customizationPath . '/' . $file->getData('file_path');
  70. }
  71. /**
  72. * Prepare the file
  73. *
  74. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  75. * @return $this
  76. */
  77. public function prepareFile(\Magento\Framework\View\Design\Theme\FileInterface $file)
  78. {
  79. $file->setData('file_type', $this->getType());
  80. if (!$file->getId()) {
  81. $this->_prepareFileName($file);
  82. $this->_prepareFilePath($file);
  83. $this->_prepareSortOrder($file);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Creates or updates file of customization in filesystem
  89. *
  90. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  91. * @return $this
  92. */
  93. public function save(\Magento\Framework\View\Design\Theme\FileInterface $file)
  94. {
  95. $this->_saveFileContent($this->getFullPath($file), $file->getContent());
  96. return $this;
  97. }
  98. /**
  99. * Deletes file of customization in filesystem
  100. *
  101. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  102. * @return $this
  103. */
  104. public function delete(\Magento\Framework\View\Design\Theme\FileInterface $file)
  105. {
  106. $this->_deleteFileContent($this->getFullPath($file));
  107. return $this;
  108. }
  109. /**
  110. * Prepares filename of file
  111. *
  112. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  113. * @return void
  114. */
  115. protected function _prepareFileName(\Magento\Framework\View\Design\Theme\FileInterface $file)
  116. {
  117. $customFiles = $file->getTheme()->getCustomization()->getFilesByType($this->getType());
  118. $fileName = $file->getFileName();
  119. $fileInfo = pathinfo($fileName);
  120. $fileIndex = 0;
  121. /** @var $customFile \Magento\Framework\View\Design\Theme\FileInterface */
  122. foreach ($customFiles as $customFile) {
  123. if ($fileName === $customFile->getFileName()) {
  124. $fileName = sprintf('%s_%d.%s', $fileInfo['filename'], ++$fileIndex, $fileInfo['extension']);
  125. }
  126. }
  127. $file->setFileName($fileName);
  128. }
  129. /**
  130. * Prepares relative path of file
  131. *
  132. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  133. * @return void
  134. */
  135. protected function _prepareFilePath(\Magento\Framework\View\Design\Theme\FileInterface $file)
  136. {
  137. $file->setData('file_path', $this->getContentType() . '/' . $file->getFileName());
  138. }
  139. /**
  140. * Prepares sort order of custom file
  141. *
  142. * @param \Magento\Framework\View\Design\Theme\FileInterface $file
  143. * @return void
  144. */
  145. protected function _prepareSortOrder(\Magento\Framework\View\Design\Theme\FileInterface $file)
  146. {
  147. $customFiles = $file->getTheme()->getCustomization()->getFilesByType($this->getType());
  148. $sortOrderIndex = (int)$file->getData('sort_order');
  149. foreach ($customFiles as $customFile) {
  150. $prevSortOrderIndex = $customFile->getData('sort_order');
  151. if ($prevSortOrderIndex > $sortOrderIndex) {
  152. $sortOrderIndex = $prevSortOrderIndex;
  153. }
  154. }
  155. $file->setData('sort_order', ++$sortOrderIndex);
  156. }
  157. /**
  158. * Creates or updates file of customization in filesystem
  159. *
  160. * @param string $filePath
  161. * @param string $content
  162. * @return void
  163. */
  164. protected function _saveFileContent($filePath, $content)
  165. {
  166. $this->getDirectoryWrite()->delete($filePath);
  167. if (!empty($content)) {
  168. $this->getDirectoryWrite()->writeFile($this->getDirectoryWrite()->getRelativePath($filePath), $content);
  169. }
  170. }
  171. /**
  172. * Deletes file of customization in filesystem
  173. *
  174. * @param string $filePath
  175. * @return void
  176. */
  177. protected function _deleteFileContent($filePath)
  178. {
  179. $filePath = $this->getDirectoryWrite()->getRelativePath($filePath);
  180. if ($this->getDirectoryWrite()->touch($filePath)) {
  181. $this->getDirectoryWrite()->delete($filePath);
  182. }
  183. }
  184. /**
  185. * Returns filesystem directory instance for write operations
  186. *
  187. * @return \Magento\Framework\Filesystem\Directory\WriteInterface
  188. */
  189. protected function getDirectoryWrite()
  190. {
  191. return $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
  192. }
  193. }