Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
14 
21 {
25  protected $_filesystem;
26 
30  protected $_authorization;
31 
35  protected $_cacheTypeList;
36 
45  public function __construct(
46  \Magento\Framework\App\Helper\Context $context,
48  \Magento\Framework\AuthorizationInterface $authorization,
49  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
50  ) {
51  parent::__construct($context);
52  $this->_authorization = $authorization;
53  $this->_filesystem = $filesystem;
54  $this->_cacheTypeList = $cacheTypeList;
55  }
56 
62  public function getBackupTypes()
63  {
64  return [
66  \Magento\Framework\Backup\Factory::TYPE_MEDIA => __('Database and Media'),
69  ];
70  }
71 
77  public function getBackupTypesList()
78  {
79  return [
84  ];
85  }
86 
92  public function getDefaultBackupType()
93  {
94  return \Magento\Framework\Backup\Factory::TYPE_DB;
95  }
96 
102  public function getBackupsDir()
103  {
104  return $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->getAbsolutePath('backups');
105  }
106 
113  public function getExtensionByType($type)
114  {
115  $extensions = $this->getExtensions();
116  return $extensions[$type] ?? '';
117  }
118 
124  public function getExtensions()
125  {
126  return [
131  ];
132  }
133 
140  public function generateBackupDownloadName(\Magento\Backup\Model\Backup $backup)
141  {
142  $additionalExtension = $backup->getType() == \Magento\Framework\Backup\Factory::TYPE_DB ? '.sql' : '';
143  return $backup->getTime() .
144  '_' .
145  $backup->getType() .
146  '_' .
147  $backup->getName() .
148  $additionalExtension .
149  '.' .
150  $this->getExtensionByType(
151  $backup->getType()
152  );
153  }
154 
160  public function isRollbackAllowed()
161  {
162  return $this->_authorization->isAllowed('Magento_Backup::rollback');
163  }
164 
170  public function getBackupIgnorePaths()
171  {
172  return [
173  '.git',
174  '.svn',
175  $this->_filesystem->getDirectoryRead(MaintenanceMode::FLAG_DIR)
176  ->getAbsolutePath(MaintenanceMode::FLAG_FILENAME),
177  $this->_filesystem->getDirectoryRead(DirectoryList::SESSION)->getAbsolutePath(),
178  $this->_filesystem->getDirectoryRead(DirectoryList::CACHE)->getAbsolutePath(),
179  $this->_filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath(),
180  $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('full_page_cache'),
181  $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('locks'),
182  $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('report'),
183  ];
184  }
185 
191  public function getRollbackIgnorePaths()
192  {
193  return [
194  '.svn',
195  '.git',
196  $this->_filesystem->getDirectoryRead(MaintenanceMode::FLAG_DIR)
197  ->getAbsolutePath(MaintenanceMode::FLAG_FILENAME),
198  $this->_filesystem->getDirectoryRead(DirectoryList::SESSION)->getAbsolutePath(),
199  $this->_filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath(),
200  $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('locks'),
201  $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('report'),
202  $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath('errors'),
203  $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath('index.php'),
204  ];
205  }
206 
214  {
215  $messagesMap = [
216  \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT => __('You created the system backup.'),
218  'You created the system backup (excluding media).'
219  ),
220  \Magento\Framework\Backup\Factory::TYPE_MEDIA => __('You created the database and media backup.'),
221  \Magento\Framework\Backup\Factory::TYPE_DB => __('You created the database backup.'),
222  ];
223 
224  if (!isset($messagesMap[$type])) {
225  return;
226  }
227 
228  return $messagesMap[$type];
229  }
230 
236  public function invalidateCache()
237  {
238  if ($cacheTypes = $this->_cacheConfig->getTypes()) {
239  $cacheTypesList = array_keys($cacheTypes);
240  $this->_cacheTypeList->invalidate($cacheTypesList);
241  }
242  return $this;
243  }
244 
251  public function nameToDisplayName($name)
252  {
253  return str_replace('_', ' ', $name);
254  }
255 
262  public function extractDataFromFilename($filename)
263  {
264  $extensions = $this->getExtensions();
265 
266  $filenameWithoutExtension = $filename;
267 
268  foreach ($extensions as $extension) {
269  $filenameWithoutExtension = preg_replace(
270  '/' . preg_quote($extension, '/') . '$/',
271  '',
272  $filenameWithoutExtension
273  );
274  }
275 
276  $filenameWithoutExtension = substr($filenameWithoutExtension, 0, strrpos($filenameWithoutExtension, "."));
277 
278  list($time, $type) = explode("_", $filenameWithoutExtension);
279 
280  $name = str_replace($time . '_' . $type, '', $filenameWithoutExtension);
281 
282  if (!empty($name)) {
283  $name = substr($name, 1);
284  }
285 
286  $result = new \Magento\Framework\DataObject();
287  $result->addData(['name' => $name, 'type' => $type, 'time' => $time]);
288 
289  return $result;
290  }
291 
297  public function isEnabled(): bool
298  {
299  return $this->scopeConfig->isSetFlag('system/backup/functionality_enabled');
300  }
301 }
extractDataFromFilename($filename)
Definition: Data.php:262
getCreateSuccessMessageByType($type)
Definition: Data.php:213
__()
Definition: __.php:13
$type
Definition: item.phtml:13
generateBackupDownloadName(\Magento\Backup\Model\Backup $backup)
Definition: Data.php:140
__construct(\Magento\Framework\App\Helper\Context $context, Filesystem $filesystem, \Magento\Framework\AuthorizationInterface $authorization, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList)
Definition: Data.php:45
$filesystem
if(!isset($_GET['name'])) $name
Definition: log.php:14