Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Database.php
Go to the documentation of this file.
1 <?php
7 
14 class Database extends \Magento\MediaStorage\Model\File\Storage\Database\AbstractDatabase
15 {
21  protected $_eventPrefix = 'media_storage_file_storage_database';
22 
28  protected $_directoryModel = null;
29 
35  protected $_errors = [];
36 
40  protected $_directoryFactory;
41 
45  protected $_mediaHelper;
46 
53  protected $mediaBaseDirectory = null;
54 
69  public function __construct(
70  \Magento\Framework\Model\Context $context,
71  \Magento\Framework\Registry $registry,
72  \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
73  \Magento\Framework\Stdlib\DateTime\DateTime $dateModel,
74  \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
75  \Magento\MediaStorage\Helper\File\Media $mediaHelper,
76  \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource,
77  \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory $directoryFactory,
78  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
79  $connectionName = null,
80  array $data = []
81  ) {
82  $this->_directoryFactory = $directoryFactory;
83  $this->_mediaHelper = $mediaHelper;
84  parent::__construct(
85  $context,
86  $registry,
87  $coreFileStorageDb,
88  $dateModel,
90  $resource,
91  $resourceCollection,
92  $connectionName,
93  $data
94  );
95  $this->_init(get_class($this->_resource));
96  }
97 
103  public function getDirectoryModel()
104  {
105  if ($this->_directoryModel === null) {
106  $this->_directoryModel = $this->_directoryFactory->create(
107  ['connectionName' => $this->getConnectionName()]
108  );
109  }
110 
111  return $this->_directoryModel;
112  }
113 
119  public function init()
120  {
121  $this->getDirectoryModel()->prepareStorage();
122  $this->prepareStorage();
123 
124  return $this;
125  }
126 
132  public function getStorageName()
133  {
134  return __('database "%1"', $this->getConnectionName());
135  }
136 
143  public function loadByFilename($filePath)
144  {
145  $filename = basename($filePath);
146  $path = dirname($filePath);
147  $this->_getResource()->loadByFilename($this, $filename, $path);
148  return $this;
149  }
150 
156  public function hasErrors()
157  {
158  return !empty($this->_errors) || $this->getDirectoryModel()->hasErrors();
159  }
160 
166  public function clear()
167  {
168  $this->getDirectoryModel()->clearDirectories();
169  $this->_getResource()->clearFiles();
170  return $this;
171  }
172 
180  public function exportDirectories($offset = 0, $count = 100)
181  {
182  return $this->getDirectoryModel()->exportDirectories($offset, $count);
183  }
184 
191  public function importDirectories($dirs)
192  {
193  return $this->getDirectoryModel()->importDirectories($dirs);
194  }
195 
203  public function exportFiles($offset = 0, $count = 100)
204  {
205  $offset = (int)$offset >= 0 ? (int)$offset : 0;
206  $count = (int)$count >= 1 ? (int)$count : 1;
207 
208  $result = $this->_getResource()->getFiles($offset, $count);
209  if (empty($result)) {
210  return false;
211  }
212 
213  return $result;
214  }
215 
222  public function importFiles($files)
223  {
224  if (!is_array($files)) {
225  return $this;
226  }
227 
228  $dateSingleton = $this->_date;
229  foreach ($files as $file) {
230  if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
231  continue;
232  }
233 
234  try {
235  $file['update_time'] = $dateSingleton->date();
236  $file['directory_id'] = isset(
237  $file['directory']
238  ) && strlen(
239  $file['directory']
240  ) ? $this->_directoryFactory->create(
241  ['connectionName' => $this->getConnectionName()]
242  )->loadByPath(
243  $file['directory']
244  )->getId() : null;
245 
246  $this->_getResource()->saveFile($file);
247  } catch (\Exception $e) {
248  $this->_errors[] = $e->getMessage();
249  $this->_logger->critical($e);
250  }
251  }
252 
253  return $this;
254  }
255 
262  public function saveFile($filename)
263  {
264  $fileInfo = $this->_mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $filename);
265  $filePath = $fileInfo['directory'];
266 
267  $directory = $this->_directoryFactory->create()->loadByPath($filePath);
268 
269  if (!$directory->getId()) {
270  $directory = $this->getDirectoryModel()->createRecursive($filePath);
271  }
272 
273  $fileInfo['directory_id'] = $directory->getId();
274  $this->_getResource()->saveFile($fileInfo);
275 
276  return $this;
277  }
278 
285  public function fileExists($filePath)
286  {
287  return $this->_getResource()->fileExists(basename($filePath), dirname($filePath));
288  }
289 
297  public function copyFile($oldFilePath, $newFilePath)
298  {
299  $this->_getResource()->copyFile(
300  basename($oldFilePath),
301  dirname($oldFilePath),
302  basename($newFilePath),
303  dirname($newFilePath)
304  );
305 
306  return $this;
307  }
308 
316  public function renameFile($oldFilePath, $newFilePath)
317  {
318  $this->_getResource()->renameFile(
319  basename($oldFilePath),
320  dirname($oldFilePath),
321  basename($newFilePath),
322  dirname($newFilePath)
323  );
324 
325  $newPath = dirname($newFilePath);
326  $directory = $this->_directoryFactory->create()->loadByPath($newPath);
327 
328  if (!$directory->getId()) {
329  $directory = $this->getDirectoryModel()->createRecursive($newPath);
330  }
331 
332  $this->loadByFilename($newFilePath);
333  if ($this->getId()) {
334  $this->setDirectoryId($directory->getId())->save();
335  }
336 
337  return $this;
338  }
339 
346  public function getDirectoryFiles($directory)
347  {
348  $directory = $this->_coreFileStorageDb->getMediaRelativePath($directory);
349  return $this->_getResource()->getDirectoryFiles($directory);
350  }
351 
358  public function deleteFile($path)
359  {
360  $filename = basename($path);
361  $directory = dirname($path);
362  $this->_getResource()->deleteFile($filename, $directory);
363 
364  return $this;
365  }
366 
373  public function getMediaBaseDirectory()
374  {
375  if ($this->mediaBaseDirectory === null) {
376  $this->mediaBaseDirectory = $this->_coreFileStorageDb->getMediaBaseDir();
377  }
379  }
380 }
$configuration
Definition: index.php:33
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb, \Magento\Framework\Stdlib\DateTime\DateTime $dateModel, \Magento\Framework\App\Config\ScopeConfigInterface $configuration, \Magento\MediaStorage\Helper\File\Media $mediaHelper, \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource, \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory $directoryFactory, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, $connectionName=null, array $data=[])
Definition: Database.php:69
renameFile($oldFilePath, $newFilePath)
Definition: Database.php:316
foreach($appDirs as $dir) $files