AbstractBackup.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * Class to work with archives
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractBackup implements BackupInterface, SourceFileInterface
  16. {
  17. /**
  18. * Backup name
  19. *
  20. * @var string
  21. */
  22. protected $_name;
  23. /**
  24. * Backup creation date
  25. *
  26. * @var int
  27. */
  28. protected $_time;
  29. /**
  30. * Backup file extension
  31. *
  32. * @var string
  33. */
  34. protected $_backupExtension;
  35. /**
  36. * Resource model
  37. *
  38. * @var object
  39. */
  40. protected $_resourceModel;
  41. /**
  42. * Magento's root directory
  43. *
  44. * @var string
  45. */
  46. protected $_rootDir;
  47. /**
  48. * Path to directory where backups stored
  49. *
  50. * @var string
  51. */
  52. protected $_backupsDir;
  53. /**
  54. * Is last operation completed successfully
  55. *
  56. * @var bool
  57. */
  58. protected $_lastOperationSucceed = false;
  59. /**
  60. * Last failed operation error message
  61. *
  62. * @var string
  63. */
  64. protected $_lastErrorMessage;
  65. /**
  66. * Keep Source files in Backup
  67. *
  68. * @var boolean
  69. */
  70. private $keepSourceFile;
  71. /**
  72. * Set Backup Extension
  73. *
  74. * @param string $backupExtension
  75. * @return $this
  76. */
  77. public function setBackupExtension($backupExtension)
  78. {
  79. $this->_backupExtension = $backupExtension;
  80. return $this;
  81. }
  82. /**
  83. * Get Backup Extension
  84. *
  85. * @return string
  86. */
  87. public function getBackupExtension()
  88. {
  89. return $this->_backupExtension;
  90. }
  91. /**
  92. * Set Resource Model
  93. *
  94. * @param object $resourceModel
  95. * @return $this
  96. */
  97. public function setResourceModel($resourceModel)
  98. {
  99. $this->_resourceModel = $resourceModel;
  100. return $this;
  101. }
  102. /**
  103. * Get Resource Model
  104. *
  105. * @return object
  106. */
  107. public function getResourceModel()
  108. {
  109. return $this->_resourceModel;
  110. }
  111. /**
  112. * Set Time
  113. *
  114. * @param int $time
  115. * @return $this
  116. */
  117. public function setTime($time)
  118. {
  119. $this->_time = $time;
  120. return $this;
  121. }
  122. /**
  123. * Get Time
  124. *
  125. * @return int
  126. */
  127. public function getTime()
  128. {
  129. return $this->_time;
  130. }
  131. /**
  132. * Set root directory of Magento installation
  133. *
  134. * @param string $rootDir
  135. * @throws LocalizedException
  136. * @return $this
  137. */
  138. public function setRootDir($rootDir)
  139. {
  140. if (!is_dir($rootDir)) {
  141. throw new LocalizedException(
  142. new Phrase('Bad root directory')
  143. );
  144. }
  145. $this->_rootDir = $rootDir;
  146. return $this;
  147. }
  148. /**
  149. * Get Magento's root directory
  150. * @return string
  151. */
  152. public function getRootDir()
  153. {
  154. return $this->_rootDir;
  155. }
  156. /**
  157. * Set path to directory where backups stored
  158. *
  159. * @param string $backupsDir
  160. * @return $this
  161. */
  162. public function setBackupsDir($backupsDir)
  163. {
  164. $this->_backupsDir = $backupsDir;
  165. return $this;
  166. }
  167. /**
  168. * Get path to directory where backups stored
  169. *
  170. * @return string
  171. */
  172. public function getBackupsDir()
  173. {
  174. return $this->_backupsDir;
  175. }
  176. /**
  177. * Get path to backup
  178. *
  179. * @return string
  180. */
  181. public function getBackupPath()
  182. {
  183. return $this->getBackupsDir() . '/' . $this->getBackupFilename();
  184. }
  185. /**
  186. * Get backup file name
  187. *
  188. * @return string
  189. */
  190. public function getBackupFilename()
  191. {
  192. $filename = $this->getTime() . '_' . $this->getType();
  193. $name = $this->getName();
  194. if (!empty($name)) {
  195. $filename .= '_' . $name;
  196. }
  197. $filename .= '.' . $this->getBackupExtension();
  198. return $filename;
  199. }
  200. /**
  201. * Check whether last operation completed successfully
  202. *
  203. * @return bool
  204. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  205. */
  206. public function getIsSuccess()
  207. {
  208. return $this->_lastOperationSucceed;
  209. }
  210. /**
  211. * Get last error message
  212. *
  213. * @return string
  214. */
  215. public function getErrorMessage()
  216. {
  217. return $this->_lastErrorMessage;
  218. }
  219. /**
  220. * Set error message
  221. *
  222. * @param string $errorMessage
  223. * @return void
  224. */
  225. public function setErrorMessage($errorMessage)
  226. {
  227. $this->_lastErrorMessage = $errorMessage;
  228. }
  229. /**
  230. * Set backup name
  231. *
  232. * @param string $name
  233. * @param bool $applyFilter
  234. * @return $this
  235. */
  236. public function setName($name, $applyFilter = true)
  237. {
  238. if ($applyFilter) {
  239. $name = $this->_filterName($name);
  240. }
  241. $this->_name = $name;
  242. return $this;
  243. }
  244. /**
  245. * Get backup name
  246. *
  247. * @return string
  248. */
  249. public function getName()
  250. {
  251. return $this->_name;
  252. }
  253. /**
  254. * Get backup display name
  255. *
  256. * @return string
  257. */
  258. public function getDisplayName()
  259. {
  260. return str_replace('_', ' ', $this->_name);
  261. }
  262. /**
  263. * Removes disallowed characters and replaces spaces with underscores
  264. *
  265. * @param string $name
  266. * @return string
  267. */
  268. protected function _filterName($name)
  269. {
  270. $name = trim(preg_replace('/[^\da-zA-Z ]/', '', $name));
  271. $name = preg_replace('/\s{2,}/', ' ', $name);
  272. $name = str_replace(' ', '_', $name);
  273. return $name;
  274. }
  275. /**
  276. * Check if keep files of backup
  277. *
  278. * @return bool
  279. * @since 102.0.0
  280. */
  281. public function keepSourceFile()
  282. {
  283. return $this->keepSourceFile;
  284. }
  285. /**
  286. * Set if keep files of backup
  287. *
  288. * @param bool $keepSourceFile
  289. * @return $this
  290. * @since 102.0.0
  291. */
  292. public function setKeepSourceFile(bool $keepSourceFile)
  293. {
  294. $this->keepSourceFile = $keepSourceFile;
  295. return $this;
  296. }
  297. }