Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
7 
11 
15 class File extends AbstractModel implements FileInterface
16 {
22  protected $_eventPrefix = 'theme_file';
23 
29  protected $_eventObject = 'file';
30 
34  protected $_theme;
35 
40 
44  protected $_fileService;
45 
49  protected $_themeFactory;
50 
60  public function __construct(
61  \Magento\Framework\Model\Context $context,
62  \Magento\Framework\Registry $registry,
63  \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
64  \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory $fileServiceFactory,
65  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
66  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
67  array $data = []
68  ) {
69  $this->_themeFactory = $themeFactory;
70  $this->_fileServiceFactory = $fileServiceFactory;
71  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
72  }
73 
79  protected function _construct()
80  {
81  $this->_init(\Magento\Theme\Model\ResourceModel\Theme\File::class);
82  }
83 
89  public function setCustomizationService(CustomizationFileInterface $fileService)
90  {
91  $this->_fileService = $fileService;
92  return $this;
93  }
94 
101  public function getCustomizationService()
102  {
103  if (!$this->_fileService && $this->hasData('file_type')) {
104  $this->_fileService = $this->_fileServiceFactory->create($this->getData('file_type'));
105  } elseif (!$this->_fileService) {
106  throw new \UnexpectedValueException('Type of file is empty');
107  }
108  return $this->_fileService;
109  }
110 
114  public function setTheme(\Magento\Framework\View\Design\ThemeInterface $theme)
115  {
116  $this->_theme = $theme;
117  $this->setData('theme_id', $theme->getId());
118  $this->setData('theme_path', $theme->getThemePath());
119  return $this;
120  }
121 
127  public function getTheme()
128  {
129  $theme = $this->_themeFactory->create($this->getData('theme_id'));
130  if (!$theme) {
131  throw new \Magento\Framework\Exception\LocalizedException(__('Theme id should be set'));
132  }
133  return $theme;
134  }
135 
139  public function setFileName($fileName)
140  {
141  $this->setData('file_name', $fileName);
142  return $this;
143  }
144 
148  public function getFileName()
149  {
150  return $this->getData('file_name') ?: basename($this->getData('file_path'));
151  }
152 
156  public function getFullPath()
157  {
158  return $this->getCustomizationService()->getFullPath($this);
159  }
160 
164  public function getContent()
165  {
166  return $this->getData('content');
167  }
168 
172  public function getFileInfo()
173  {
174  return [
175  'id' => $this->getId(),
176  'name' => $this->getFileName(),
177  'temporary' => $this->getData('is_temporary') ? $this->getId() : 0
178  ];
179  }
180 
186  public function beforeSave()
187  {
188  $fileService = $this->getCustomizationService();
189  $fileService->prepareFile($this);
190  $fileService->save($this);
191  return parent::beforeSave();
192  }
193 
199  public function beforeDelete()
200  {
201  $fileService = $this->getCustomizationService();
202  $fileService->delete($this);
203  return parent::beforeDelete();
204  }
205 }
setTheme(\Magento\Framework\View\Design\ThemeInterface $theme)
Definition: File.php:114
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$fileName
Definition: translate.phtml:15
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory, \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory $fileServiceFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: File.php:60
$theme
setCustomizationService(CustomizationFileInterface $fileService)
Definition: File.php:89