File.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Theme;
  7. use Magento\Framework\Model\AbstractModel;
  8. use Magento\Framework\View\Design\Theme\Customization\FileInterface as CustomizationFileInterface;
  9. use Magento\Framework\View\Design\Theme\FileInterface;
  10. /**
  11. * Theme files model class
  12. */
  13. class File extends AbstractModel implements FileInterface
  14. {
  15. /**
  16. * {@inheritdoc}
  17. *
  18. * @var string
  19. */
  20. protected $_eventPrefix = 'theme_file';
  21. /**
  22. * {@inheritdoc}
  23. *
  24. * @var string
  25. */
  26. protected $_eventObject = 'file';
  27. /**
  28. * @var \Magento\Framework\View\Design\ThemeInterface
  29. */
  30. protected $_theme;
  31. /**
  32. * @var \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory
  33. */
  34. protected $_fileServiceFactory;
  35. /**
  36. * @var CustomizationFileInterface
  37. */
  38. protected $_fileService;
  39. /**
  40. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory
  41. */
  42. protected $_themeFactory;
  43. /**
  44. * @param \Magento\Framework\Model\Context $context
  45. * @param \Magento\Framework\Registry $registry
  46. * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
  47. * @param \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory $fileServiceFactory
  48. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  49. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  50. * @param array $data
  51. */
  52. public function __construct(
  53. \Magento\Framework\Model\Context $context,
  54. \Magento\Framework\Registry $registry,
  55. \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
  56. \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory $fileServiceFactory,
  57. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  58. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  59. array $data = []
  60. ) {
  61. $this->_themeFactory = $themeFactory;
  62. $this->_fileServiceFactory = $fileServiceFactory;
  63. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  64. }
  65. /**
  66. * Theme files model initialization
  67. *
  68. * @return void
  69. */
  70. protected function _construct()
  71. {
  72. $this->_init(\Magento\Theme\Model\ResourceModel\Theme\File::class);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. *
  77. * @return $this
  78. */
  79. public function setCustomizationService(CustomizationFileInterface $fileService)
  80. {
  81. $this->_fileService = $fileService;
  82. return $this;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. *
  87. * @return CustomizationFileInterface
  88. * @throws \UnexpectedValueException
  89. */
  90. public function getCustomizationService()
  91. {
  92. if (!$this->_fileService && $this->hasData('file_type')) {
  93. $this->_fileService = $this->_fileServiceFactory->create($this->getData('file_type'));
  94. } elseif (!$this->_fileService) {
  95. throw new \UnexpectedValueException('Type of file is empty');
  96. }
  97. return $this->_fileService;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setTheme(\Magento\Framework\View\Design\ThemeInterface $theme)
  103. {
  104. $this->_theme = $theme;
  105. $this->setData('theme_id', $theme->getId());
  106. $this->setData('theme_path', $theme->getThemePath());
  107. return $this;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. *
  112. * @throws \Magento\Framework\Exception\LocalizedException
  113. */
  114. public function getTheme()
  115. {
  116. $theme = $this->_themeFactory->create($this->getData('theme_id'));
  117. if (!$theme) {
  118. throw new \Magento\Framework\Exception\LocalizedException(__('Theme id should be set'));
  119. }
  120. return $theme;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function setFileName($fileName)
  126. {
  127. $this->setData('file_name', $fileName);
  128. return $this;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getFileName()
  134. {
  135. return $this->getData('file_name') ?: basename($this->getData('file_path'));
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getFullPath()
  141. {
  142. return $this->getCustomizationService()->getFullPath($this);
  143. }
  144. /**
  145. * @return string
  146. */
  147. public function getContent()
  148. {
  149. return $this->getData('content');
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getFileInfo()
  155. {
  156. return [
  157. 'id' => $this->getId(),
  158. 'name' => $this->getFileName(),
  159. 'temporary' => $this->getData('is_temporary') ? $this->getId() : 0
  160. ];
  161. }
  162. /**
  163. * Prepare file before it will be saved
  164. *
  165. * @return $this
  166. */
  167. public function beforeSave()
  168. {
  169. $fileService = $this->getCustomizationService();
  170. $fileService->prepareFile($this);
  171. $fileService->save($this);
  172. return parent::beforeSave();
  173. }
  174. /**
  175. * Prepare file before it will be deleted
  176. *
  177. * @return $this
  178. */
  179. public function beforeDelete()
  180. {
  181. $fileService = $this->getCustomizationService();
  182. $fileService->delete($this);
  183. return parent::beforeDelete();
  184. }
  185. }