Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Storage.php
Go to the documentation of this file.
1 <?php
8 
12 
20 class Storage extends AbstractModel
21 {
26 
28 
32  const XML_PATH_STORAGE_MEDIA = 'system/media_storage_configuration/media_storage';
33 
34  const XML_PATH_STORAGE_MEDIA_DATABASE = 'system/media_storage_configuration/media_database';
35 
36  const XML_PATH_MEDIA_RESOURCE_WHITELIST = 'system/media_storage_configuration/allowed_resources';
37 
38  const XML_PATH_MEDIA_UPDATE_TIME = 'system/media_storage_configuration/configuration_update_time';
39 
45  protected $_eventPrefix = 'media_storage_file_storage';
46 
52  protected $_coreFileStorage = null;
53 
59  protected $_scopeConfig;
60 
64  protected $_coreConfig;
65 
71  protected $_fileFlag;
72 
78  protected $_fileFactory;
79 
83  protected $_databaseFactory;
84 
90  protected $filesystem;
91 
107  public function __construct(
108  \Magento\Framework\Model\Context $context,
109  \Magento\Framework\Registry $registry,
110  \Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
111  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
112  \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
113  \Magento\MediaStorage\Model\File\Storage\Flag $fileFlag,
114  \Magento\MediaStorage\Model\File\Storage\FileFactory $fileFactory,
115  \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $databaseFactory,
117  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
118  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
119  array $data = []
120  ) {
121  $this->_coreFileStorage = $coreFileStorage;
122  $this->_scopeConfig = $scopeConfig;
123  $this->_coreConfig = $coreConfig;
124  $this->_fileFlag = $fileFlag;
125  $this->_fileFactory = $fileFactory;
126  $this->_databaseFactory = $databaseFactory;
127  $this->filesystem = $filesystem;
128  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
129  }
130 
138  protected function _synchronizeHasErrors($sourceModel, $destinationModel)
139  {
140  if (!$sourceModel || !$destinationModel) {
141  return true;
142  }
143 
144  return $sourceModel->hasErrors() || $destinationModel->hasErrors();
145  }
146 
152  public function getSyncFlag()
153  {
154  return $this->_fileFlag->loadSelf();
155  }
156 
170  public function getStorageModel($storage = null, $params = [])
171  {
172  if ($storage === null) {
173  $storage = $this->_coreFileStorage->getCurrentStorageCode();
174  }
175 
176  switch ($storage) {
178  $model = $this->_fileFactory->create();
179  break;
181  $connection = isset($params['connection']) ? $params['connection'] : null;
182  $model = $this->_databaseFactory->create(['connectionName' => $connection]);
183  break;
184  default:
185  return false;
186  }
187 
188  if (isset($params['init']) && $params['init']) {
189  $model->init();
190  }
191 
192  return $model;
193  }
194 
207  public function synchronize($storage)
208  {
209  if (is_array($storage) && isset($storage['type'])) {
210  $storageDest = (int)$storage['type'];
211  $connection = isset($storage['connection']) ? $storage['connection'] : null;
213 
214  // if unable to sync to internal storage from itself
215  if ($storageDest == $helper->getCurrentStorageCode() && $helper->isInternalStorage()) {
216  return $this;
217  }
218 
219  $sourceModel = $this->getStorageModel();
220  $destinationModel = $this->getStorageModel(
221  $storageDest,
222  ['connection' => $connection, 'init' => true]
223  );
224 
225  if (!$sourceModel || !$destinationModel) {
226  return $this;
227  }
228 
229  $hasErrors = false;
230  $flag = $this->getSyncFlag();
231  $flagData = [
232  'source' => $sourceModel->getStorageName(),
233  'destination' => $destinationModel->getStorageName(),
234  'destination_storage_type' => $storageDest,
235  'destination_connection_name' => (string)$destinationModel->getConnectionName(),
236  'has_errors' => false,
237  'timeout_reached' => false,
238  ];
239  $flag->setFlagData($flagData);
240 
241  $destinationModel->clear();
242 
243  $offset = 0;
244  while (($dirs = $sourceModel->exportDirectories($offset)) !== false) {
245  $flagData['timeout_reached'] = false;
246  if (!$hasErrors) {
247  $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
248  if ($hasErrors) {
249  $flagData['has_errors'] = true;
250  }
251  }
252 
253  $flag->setFlagData($flagData)->save();
254 
255  $destinationModel->importDirectories($dirs);
256  $offset += count($dirs);
257  }
258  unset($dirs);
259 
260  $offset = 0;
261  while (($files = $sourceModel->exportFiles($offset, 1)) !== false) {
262  $flagData['timeout_reached'] = false;
263  if (!$hasErrors) {
264  $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
265  if ($hasErrors) {
266  $flagData['has_errors'] = true;
267  }
268  }
269 
270  $flag->setFlagData($flagData)->save();
271 
272  $destinationModel->importFiles($files);
273  $offset += count($files);
274  }
275  unset($files);
276  }
277 
278  return $this;
279  }
280 
286  public function getScriptConfig()
287  {
288  $config = [];
289  $config['media_directory'] = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
290 
291  $allowedResources = $this->_coreConfig->getValue(self::XML_PATH_MEDIA_RESOURCE_WHITELIST, 'default');
292  foreach ($allowedResources as $allowedResource) {
293  $config['allowed_resources'][] = $allowedResource;
294  }
295 
296  $config['update_time'] = $this->_scopeConfig->getValue(
297  self::XML_PATH_MEDIA_UPDATE_TIME,
298  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
299  );
300 
301  return $config;
302  }
303 }
$helper
Definition: iframe.phtml:13
$config
Definition: fraud_order.php:17
_synchronizeHasErrors($sourceModel, $destinationModel)
Definition: Storage.php:138
$resource
Definition: bulk.php:12
getStorageModel($storage=null, $params=[])
Definition: Storage.php:170
$allowedResources
Definition: get.php:17
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\MediaStorage\Helper\File\Storage $coreFileStorage, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\MediaStorage\Model\File\Storage\Flag $fileFlag, \Magento\MediaStorage\Model\File\Storage\FileFactory $fileFactory, \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $databaseFactory, Filesystem $filesystem, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Storage.php:107
$connection
Definition: bulk.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
foreach($appDirs as $dir) $files