123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Backup;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Phrase;
- /**
- * Class to work with archives
- *
- * @api
- * @since 100.0.2
- */
- abstract class AbstractBackup implements BackupInterface, SourceFileInterface
- {
- /**
- * Backup name
- *
- * @var string
- */
- protected $_name;
- /**
- * Backup creation date
- *
- * @var int
- */
- protected $_time;
- /**
- * Backup file extension
- *
- * @var string
- */
- protected $_backupExtension;
- /**
- * Resource model
- *
- * @var object
- */
- protected $_resourceModel;
- /**
- * Magento's root directory
- *
- * @var string
- */
- protected $_rootDir;
- /**
- * Path to directory where backups stored
- *
- * @var string
- */
- protected $_backupsDir;
- /**
- * Is last operation completed successfully
- *
- * @var bool
- */
- protected $_lastOperationSucceed = false;
- /**
- * Last failed operation error message
- *
- * @var string
- */
- protected $_lastErrorMessage;
- /**
- * Keep Source files in Backup
- *
- * @var boolean
- */
- private $keepSourceFile;
- /**
- * Set Backup Extension
- *
- * @param string $backupExtension
- * @return $this
- */
- public function setBackupExtension($backupExtension)
- {
- $this->_backupExtension = $backupExtension;
- return $this;
- }
- /**
- * Get Backup Extension
- *
- * @return string
- */
- public function getBackupExtension()
- {
- return $this->_backupExtension;
- }
- /**
- * Set Resource Model
- *
- * @param object $resourceModel
- * @return $this
- */
- public function setResourceModel($resourceModel)
- {
- $this->_resourceModel = $resourceModel;
- return $this;
- }
- /**
- * Get Resource Model
- *
- * @return object
- */
- public function getResourceModel()
- {
- return $this->_resourceModel;
- }
- /**
- * Set Time
- *
- * @param int $time
- * @return $this
- */
- public function setTime($time)
- {
- $this->_time = $time;
- return $this;
- }
- /**
- * Get Time
- *
- * @return int
- */
- public function getTime()
- {
- return $this->_time;
- }
- /**
- * Set root directory of Magento installation
- *
- * @param string $rootDir
- * @throws LocalizedException
- * @return $this
- */
- public function setRootDir($rootDir)
- {
- if (!is_dir($rootDir)) {
- throw new LocalizedException(
- new Phrase('Bad root directory')
- );
- }
- $this->_rootDir = $rootDir;
- return $this;
- }
- /**
- * Get Magento's root directory
- * @return string
- */
- public function getRootDir()
- {
- return $this->_rootDir;
- }
- /**
- * Set path to directory where backups stored
- *
- * @param string $backupsDir
- * @return $this
- */
- public function setBackupsDir($backupsDir)
- {
- $this->_backupsDir = $backupsDir;
- return $this;
- }
- /**
- * Get path to directory where backups stored
- *
- * @return string
- */
- public function getBackupsDir()
- {
- return $this->_backupsDir;
- }
- /**
- * Get path to backup
- *
- * @return string
- */
- public function getBackupPath()
- {
- return $this->getBackupsDir() . '/' . $this->getBackupFilename();
- }
- /**
- * Get backup file name
- *
- * @return string
- */
- public function getBackupFilename()
- {
- $filename = $this->getTime() . '_' . $this->getType();
- $name = $this->getName();
- if (!empty($name)) {
- $filename .= '_' . $name;
- }
- $filename .= '.' . $this->getBackupExtension();
- return $filename;
- }
- /**
- * Check whether last operation completed successfully
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getIsSuccess()
- {
- return $this->_lastOperationSucceed;
- }
- /**
- * Get last error message
- *
- * @return string
- */
- public function getErrorMessage()
- {
- return $this->_lastErrorMessage;
- }
- /**
- * Set error message
- *
- * @param string $errorMessage
- * @return void
- */
- public function setErrorMessage($errorMessage)
- {
- $this->_lastErrorMessage = $errorMessage;
- }
- /**
- * Set backup name
- *
- * @param string $name
- * @param bool $applyFilter
- * @return $this
- */
- public function setName($name, $applyFilter = true)
- {
- if ($applyFilter) {
- $name = $this->_filterName($name);
- }
- $this->_name = $name;
- return $this;
- }
- /**
- * Get backup name
- *
- * @return string
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- * Get backup display name
- *
- * @return string
- */
- public function getDisplayName()
- {
- return str_replace('_', ' ', $this->_name);
- }
- /**
- * Removes disallowed characters and replaces spaces with underscores
- *
- * @param string $name
- * @return string
- */
- protected function _filterName($name)
- {
- $name = trim(preg_replace('/[^\da-zA-Z ]/', '', $name));
- $name = preg_replace('/\s{2,}/', ' ', $name);
- $name = str_replace(' ', '_', $name);
- return $name;
- }
- /**
- * Check if keep files of backup
- *
- * @return bool
- * @since 102.0.0
- */
- public function keepSourceFile()
- {
- return $this->keepSourceFile;
- }
- /**
- * Set if keep files of backup
- *
- * @param bool $keepSourceFile
- * @return $this
- * @since 102.0.0
- */
- public function setKeepSourceFile(bool $keepSourceFile)
- {
- $this->keepSourceFile = $keepSourceFile;
- return $this;
- }
- }
|