Archiver.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @package Grav\Common\Filesystem
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Filesystem;
  9. use Grav\Common\Utils;
  10. abstract class Archiver
  11. {
  12. protected $options = [
  13. 'exclude_files' => ['.DS_Store'],
  14. 'exclude_paths' => []
  15. ];
  16. protected $archive_file;
  17. public static function create($compression)
  18. {
  19. if ($compression === 'zip') {
  20. return new ZipArchiver();
  21. }
  22. return new ZipArchiver();
  23. }
  24. public function setArchive($archive_file)
  25. {
  26. $this->archive_file = $archive_file;
  27. return $this;
  28. }
  29. public function setOptions($options)
  30. {
  31. // Set infinite PHP execution time if possible.
  32. if (function_exists('set_time_limit') && !Utils::isFunctionDisabled('set_time_limit')) {
  33. set_time_limit(0);
  34. }
  35. $this->options = $options + $this->options;
  36. return $this;
  37. }
  38. public abstract function compress($folder, callable $status = null);
  39. public abstract function extract($destination, callable $status = null);
  40. public abstract function addEmptyFolders($folders, callable $status = null);
  41. protected function getArchiveFiles($rootPath)
  42. {
  43. $exclude_paths = $this->options['exclude_paths'];
  44. $exclude_files = $this->options['exclude_files'];
  45. $dirItr = new \RecursiveDirectoryIterator($rootPath, \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::UNIX_PATHS);
  46. $filterItr = new RecursiveDirectoryFilterIterator($dirItr, $rootPath, $exclude_paths, $exclude_files);
  47. $files = new \RecursiveIteratorIterator($filterItr, \RecursiveIteratorIterator::SELF_FIRST);
  48. return $files;
  49. }
  50. }