Tar.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Extended version of \Magento\Framework\Archive\Tar that supports filtering
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Backup\Archive;
  12. use Magento\Framework\Backup\Filesystem\Iterator\Filter;
  13. use RecursiveDirectoryIterator;
  14. use RecursiveIteratorIterator;
  15. class Tar extends \Magento\Framework\Archive\Tar
  16. {
  17. /**
  18. * Filenames or filename parts that are used for filtering files
  19. *
  20. * @var array
  21. */
  22. protected $_skipFiles = [];
  23. /**
  24. * Overridden \Magento\Framework\Archive\Tar::_createTar method that does the same actions as it's parent but
  25. * filters files using \Magento\Framework\Backup\Filesystem\Iterator\Filter
  26. *
  27. * @param bool $skipRoot
  28. * @param bool $finalize
  29. * @return void
  30. *
  31. * @see \Magento\Framework\Archive\Tar::_createTar()
  32. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  33. */
  34. protected function _createTar($skipRoot = false, $finalize = false)
  35. {
  36. $path = $this->_getCurrentFile();
  37. $filesystemIterator = new RecursiveIteratorIterator(
  38. new RecursiveDirectoryIterator($path),
  39. RecursiveIteratorIterator::SELF_FIRST
  40. );
  41. $iterator = new Filter(
  42. $filesystemIterator,
  43. $this->_skipFiles
  44. );
  45. foreach ($iterator as $item) {
  46. $this->_setCurrentFile($item->getPathname());
  47. $this->_packAndWriteCurrentFile();
  48. }
  49. if ($finalize) {
  50. $this->_getWriter()->write(str_repeat("\0", self::TAR_BLOCK_SIZE * 12));
  51. }
  52. }
  53. /**
  54. * Set files that shouldn't be added to tarball
  55. *
  56. * @param array $skipFiles
  57. * @return $this
  58. */
  59. public function setSkipFiles(array $skipFiles)
  60. {
  61. $this->_skipFiles = $skipFiles;
  62. return $this;
  63. }
  64. }