Archiver.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @package Grav\Common\Filesystem
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Filesystem;
  9. use FilesystemIterator;
  10. use Grav\Common\Utils;
  11. use RecursiveDirectoryIterator;
  12. use RecursiveIteratorIterator;
  13. use function function_exists;
  14. /**
  15. * Class Archiver
  16. * @package Grav\Common\Filesystem
  17. */
  18. abstract class Archiver
  19. {
  20. /** @var array */
  21. protected $options = [
  22. 'exclude_files' => ['.DS_Store'],
  23. 'exclude_paths' => []
  24. ];
  25. /** @var string */
  26. protected $archive_file;
  27. /**
  28. * @param string $compression
  29. * @return ZipArchiver
  30. */
  31. public static function create($compression)
  32. {
  33. if ($compression === 'zip') {
  34. return new ZipArchiver();
  35. }
  36. return new ZipArchiver();
  37. }
  38. /**
  39. * @param string $archive_file
  40. * @return $this
  41. */
  42. public function setArchive($archive_file)
  43. {
  44. $this->archive_file = $archive_file;
  45. return $this;
  46. }
  47. /**
  48. * @param array $options
  49. * @return $this
  50. */
  51. public function setOptions($options)
  52. {
  53. // Set infinite PHP execution time if possible.
  54. if (Utils::functionExists('set_time_limit')) {
  55. @set_time_limit(0);
  56. }
  57. $this->options = $options + $this->options;
  58. return $this;
  59. }
  60. /**
  61. * @param string $folder
  62. * @param callable|null $status
  63. * @return $this
  64. */
  65. abstract public function compress($folder, callable $status = null);
  66. /**
  67. * @param string $destination
  68. * @param callable|null $status
  69. * @return $this
  70. */
  71. abstract public function extract($destination, callable $status = null);
  72. /**
  73. * @param array $folders
  74. * @param callable|null $status
  75. * @return $this
  76. */
  77. abstract public function addEmptyFolders($folders, callable $status = null);
  78. /**
  79. * @param string $rootPath
  80. * @return RecursiveIteratorIterator
  81. */
  82. protected function getArchiveFiles($rootPath)
  83. {
  84. $exclude_paths = $this->options['exclude_paths'];
  85. $exclude_files = $this->options['exclude_files'];
  86. $dirItr = new RecursiveDirectoryIterator($rootPath, RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::UNIX_PATHS);
  87. $filterItr = new RecursiveDirectoryFilterIterator($dirItr, $rootPath, $exclude_paths, $exclude_files);
  88. $files = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
  89. return $files;
  90. }
  91. }