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