Iterator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the File_Iterator package.
  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. /**
  11. * FilterIterator implementation that filters files based on prefix(es) and/or
  12. * suffix(es). Hidden files and files from hidden directories are also filtered.
  13. *
  14. * @since Class available since Release 1.0.0
  15. */
  16. class File_Iterator extends FilterIterator
  17. {
  18. const PREFIX = 0;
  19. const SUFFIX = 1;
  20. /**
  21. * @var array
  22. */
  23. protected $suffixes = array();
  24. /**
  25. * @var array
  26. */
  27. protected $prefixes = array();
  28. /**
  29. * @var array
  30. */
  31. protected $exclude = array();
  32. /**
  33. * @var string
  34. */
  35. protected $basepath;
  36. /**
  37. * @param Iterator $iterator
  38. * @param array $suffixes
  39. * @param array $prefixes
  40. * @param array $exclude
  41. * @param string $basepath
  42. */
  43. public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
  44. {
  45. $exclude = array_filter(array_map('realpath', $exclude));
  46. if ($basepath !== NULL) {
  47. $basepath = realpath($basepath);
  48. }
  49. if ($basepath === FALSE) {
  50. $basepath = NULL;
  51. } else {
  52. foreach ($exclude as &$_exclude) {
  53. $_exclude = str_replace($basepath, '', $_exclude);
  54. }
  55. }
  56. $this->prefixes = $prefixes;
  57. $this->suffixes = $suffixes;
  58. $this->exclude = $exclude;
  59. $this->basepath = $basepath;
  60. parent::__construct($iterator);
  61. }
  62. /**
  63. * @return bool
  64. */
  65. public function accept()
  66. {
  67. $current = $this->getInnerIterator()->current();
  68. $filename = $current->getFilename();
  69. $realpath = $current->getRealPath();
  70. if ($this->basepath !== NULL) {
  71. $realpath = str_replace($this->basepath, '', $realpath);
  72. }
  73. // Filter files in hidden directories.
  74. if (preg_match('=/\.[^/]*/=', $realpath)) {
  75. return FALSE;
  76. }
  77. return $this->acceptPath($realpath) &&
  78. $this->acceptPrefix($filename) &&
  79. $this->acceptSuffix($filename);
  80. }
  81. /**
  82. * @param string $path
  83. * @return bool
  84. * @since Method available since Release 1.1.0
  85. */
  86. protected function acceptPath($path)
  87. {
  88. foreach ($this->exclude as $exclude) {
  89. if (strpos($path, $exclude) === 0) {
  90. return FALSE;
  91. }
  92. }
  93. return TRUE;
  94. }
  95. /**
  96. * @param string $filename
  97. * @return bool
  98. * @since Method available since Release 1.1.0
  99. */
  100. protected function acceptPrefix($filename)
  101. {
  102. return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
  103. }
  104. /**
  105. * @param string $filename
  106. * @return bool
  107. * @since Method available since Release 1.1.0
  108. */
  109. protected function acceptSuffix($filename)
  110. {
  111. return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
  112. }
  113. /**
  114. * @param string $filename
  115. * @param array $subString
  116. * @param int $type
  117. * @return bool
  118. * @since Method available since Release 1.1.0
  119. */
  120. protected function acceptSubString($filename, array $subStrings, $type)
  121. {
  122. if (empty($subStrings)) {
  123. return TRUE;
  124. }
  125. $matched = FALSE;
  126. foreach ($subStrings as $string) {
  127. if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
  128. ($type == self::SUFFIX &&
  129. substr($filename, -1 * strlen($string)) == $string)) {
  130. $matched = TRUE;
  131. break;
  132. }
  133. }
  134. return $matched;
  135. }
  136. }