SingleFile.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Service model to upload single file in customizations
  8. */
  9. namespace Magento\Theme\Model\Theme;
  10. class SingleFile
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Design\Theme\Customization\FileInterface
  14. */
  15. protected $_fileService;
  16. /**
  17. * @param \Magento\Framework\View\Design\Theme\Customization\FileInterface $fileService
  18. */
  19. public function __construct(\Magento\Framework\View\Design\Theme\Customization\FileInterface $fileService)
  20. {
  21. $this->_fileService = $fileService;
  22. }
  23. /**
  24. * Creates or updates custom single file which belong to a selected theme
  25. *
  26. * @param \Magento\Framework\View\Design\ThemeInterface $themeModel
  27. * @param string $fileContent
  28. * @return \Magento\Framework\View\Design\Theme\FileInterface
  29. */
  30. public function update(\Magento\Framework\View\Design\ThemeInterface $themeModel, $fileContent)
  31. {
  32. $customFiles = $themeModel->getCustomization()->getFilesByType($this->_fileService->getType());
  33. $customCss = reset($customFiles);
  34. if (empty($fileContent) && $customCss) {
  35. $customCss->delete();
  36. return $customCss;
  37. }
  38. if (!$customCss) {
  39. $customCss = $this->_fileService->create();
  40. }
  41. $customCss->setData('content', $fileContent);
  42. $customCss->setTheme($themeModel);
  43. $customCss->save();
  44. return $customCss;
  45. }
  46. }