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 
14 class File
15 {
21  protected $_eventPrefix = 'media_storage_file_storage_file';
22 
28  protected $_mediaBaseDirectory = null;
29 
35  protected $_storageHelper = null;
36 
40  protected $_mediaHelper = null;
41 
47  protected $_data = null;
48 
54  protected $_errors = [];
55 
59  protected $_logger;
60 
67  public function __construct(
68  \Psr\Log\LoggerInterface $logger,
69  \Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
70  \Magento\MediaStorage\Helper\File\Media $mediaHelper,
71  \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility
72  ) {
73  $this->_fileUtility = $fileUtility;
74  $this->_storageHelper = $storageHelper;
75  $this->_logger = $logger;
76  $this->_mediaHelper = $mediaHelper;
77  }
78 
84  public function init()
85  {
86  return $this;
87  }
88 
94  public function getStorageName()
95  {
96  return __('File system');
97  }
98 
104  public function getStorageData()
105  {
106  return $this->_fileUtility->getStorageData();
107  }
108 
114  public function hasErrors()
115  {
116  return !empty($this->_errors);
117  }
118 
124  public function clear()
125  {
126  $this->_fileUtility->clear();
127  return $this;
128  }
129 
139  public function collectData($offset = 0, $count = 100, $type = 'files')
140  {
141  if (!in_array($type, ['files', 'directories'])) {
142  return false;
143  }
144 
145  $offset = (int)$offset >= 0 ? (int)$offset : 0;
146  $count = (int)$count >= 1 ? (int)$count : 1;
147 
148  if (empty($this->_data)) {
149  $this->_data = $this->getStorageData();
150  }
151 
152  if (!array_key_exists($type, $this->_data)) {
153  return false;
154  }
155  $slice = array_slice($this->_data[$type], $offset, $count);
156  return $slice ?: false;
157  }
158 
164  public function getConfigConnectionName()
165  {
166  return null;
167  }
168 
174  public function getConnectionName()
175  {
176  return null;
177  }
178 
186  public function exportDirectories($offset = 0, $count = 100)
187  {
188  return $this->collectData($offset, $count, 'directories');
189  }
190 
198  public function exportFiles($offset = 0, $count = 1)
199  {
200  $slice = $this->collectData($offset, $count, 'files');
201 
202  if (!$slice) {
203  return false;
204  }
205 
206  $result = [];
207  foreach ($slice as $fileName) {
208  try {
209  $fileInfo = $this->_mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $fileName);
210  } catch (\Exception $e) {
211  $this->_logger->critical($e);
212  continue;
213  }
214 
215  $result[] = $fileInfo;
216  }
217 
218  return $result;
219  }
220 
228  public function import($data, $callback)
229  {
230  if (!is_array($data) || !method_exists($this, $callback)) {
231  return $this;
232  }
233 
234  foreach ($data as $part) {
235  try {
236  $this->{$callback}($part);
237  } catch (\Exception $e) {
238  $this->_errors[] = $e->getMessage();
239  $this->_logger->critical($e);
240  }
241  }
242 
243  return $this;
244  }
245 
252  public function importDirectories($dirs)
253  {
254  return $this->import($dirs, 'saveDir');
255  }
256 
263  public function importFiles($files)
264  {
265  return $this->import($files, 'saveFile');
266  }
267 
274  public function saveDir($dir)
275  {
276  return $this->_fileUtility->saveDir($dir);
277  }
278 
287  public function saveFile($file, $overwrite = true)
288  {
289  if (isset($file['filename'])
290  && !empty($file['filename'])
291  && isset($file['content'])
292  && !empty($file['content'])
293  ) {
294  try {
295  $filename = isset(
296  $file['directory']
297  ) && !empty($file['directory']) ? $file['directory'] . '/' . $file['filename'] : $file['filename'];
298 
299  return $this->_fileUtility->saveFile($filename, $file['content'], $overwrite);
300  } catch (\Exception $e) {
301  $this->_logger->critical($e);
302  throw new \Magento\Framework\Exception\LocalizedException(
303  __('Unable to save file "%1" at "%2"', $file['filename'], $file['directory'])
304  );
305  }
306  } else {
307  throw new \Magento\Framework\Exception\LocalizedException(__('Wrong file info format'));
308  }
309  }
310 
316  public function getMediaBaseDirectory()
317  {
318  if ($this->_mediaBaseDirectory === null) {
319  $this->_mediaBaseDirectory = $this->_storageHelper->getMediaBaseDir();
320  }
322  }
323 }
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
$logger
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15
__construct(\Psr\Log\LoggerInterface $logger, \Magento\MediaStorage\Helper\File\Storage\Database $storageHelper, \Magento\MediaStorage\Helper\File\Media $mediaHelper, \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility)
Definition: File.php:67
exportDirectories($offset=0, $count=100)
Definition: File.php:186
collectData($offset=0, $count=100, $type='files')
Definition: File.php:139
saveFile($file, $overwrite=true)
Definition: File.php:287
foreach($appDirs as $dir) $files