Iterator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of php-file-iterator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\FileIterator;
  11. class Iterator extends \FilterIterator
  12. {
  13. const PREFIX = 0;
  14. const SUFFIX = 1;
  15. /**
  16. * @var string
  17. */
  18. private $basePath;
  19. /**
  20. * @var array
  21. */
  22. private $suffixes = [];
  23. /**
  24. * @var array
  25. */
  26. private $prefixes = [];
  27. /**
  28. * @var array
  29. */
  30. private $exclude = [];
  31. /**
  32. * @param string $basePath
  33. * @param \Iterator $iterator
  34. * @param array $suffixes
  35. * @param array $prefixes
  36. * @param array $exclude
  37. */
  38. public function __construct(string $basePath, \Iterator $iterator, array $suffixes = [], array $prefixes = [], array $exclude = [])
  39. {
  40. $this->basePath = \realpath($basePath);
  41. $this->prefixes = $prefixes;
  42. $this->suffixes = $suffixes;
  43. $this->exclude = \array_filter(\array_map('realpath', $exclude));
  44. parent::__construct($iterator);
  45. }
  46. public function accept()
  47. {
  48. $current = $this->getInnerIterator()->current();
  49. $filename = $current->getFilename();
  50. $realPath = $current->getRealPath();
  51. return $this->acceptPath($realPath) &&
  52. $this->acceptPrefix($filename) &&
  53. $this->acceptSuffix($filename);
  54. }
  55. private function acceptPath(string $path): bool
  56. {
  57. // Filter files in hidden directories by checking path that is relative to the base path.
  58. if (\preg_match('=/\.[^/]*/=', \str_replace($this->basePath, '', $path))) {
  59. return false;
  60. }
  61. foreach ($this->exclude as $exclude) {
  62. if (\strpos($path, $exclude) === 0) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. private function acceptPrefix(string $filename): bool
  69. {
  70. return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
  71. }
  72. private function acceptSuffix(string $filename): bool
  73. {
  74. return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
  75. }
  76. private function acceptSubString(string $filename, array $subStrings, int $type): bool
  77. {
  78. if (empty($subStrings)) {
  79. return true;
  80. }
  81. $matched = false;
  82. foreach ($subStrings as $string) {
  83. if (($type === self::PREFIX && \strpos($filename, $string) === 0) ||
  84. ($type === self::SUFFIX &&
  85. \substr($filename, -1 * \strlen($string)) === $string)) {
  86. $matched = true;
  87. break;
  88. }
  89. }
  90. return $matched;
  91. }
  92. }