No Description

Iterator.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * php-file-iterator
  4. *
  5. * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package File
  38. * @author Sebastian Bergmann <sebastian@phpunit.de>
  39. * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
  40. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  41. * @since File available since Release 1.0.0
  42. */
  43. /**
  44. * FilterIterator implementation that filters files based on prefix(es) and/or
  45. * suffix(es). Hidden files and files from hidden directories are also filtered.
  46. *
  47. * @author Sebastian Bergmann <sebastian@phpunit.de>
  48. * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
  49. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  50. * @version Release: @package_version@
  51. * @link http://github.com/sebastianbergmann/php-file-iterator/tree
  52. * @since Class available since Release 1.0.0
  53. */
  54. class File_Iterator extends FilterIterator
  55. {
  56. const PREFIX = 0;
  57. const SUFFIX = 1;
  58. /**
  59. * @var array
  60. */
  61. protected $suffixes = array();
  62. /**
  63. * @var array
  64. */
  65. protected $prefixes = array();
  66. /**
  67. * @var array
  68. */
  69. protected $exclude = array();
  70. /**
  71. * @var string
  72. */
  73. protected $basepath;
  74. /**
  75. * @param Iterator $iterator
  76. * @param array $suffixes
  77. * @param array $prefixes
  78. * @param array $exclude
  79. * @param string $basepath
  80. */
  81. public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
  82. {
  83. $exclude = array_filter(array_map('realpath', $exclude));
  84. if ($basepath !== NULL) {
  85. $basepath = realpath($basepath);
  86. }
  87. if ($basepath === FALSE) {
  88. $basepath = NULL;
  89. } else {
  90. foreach ($exclude as &$_exclude) {
  91. $_exclude = str_replace($basepath, '', $_exclude);
  92. }
  93. }
  94. $this->prefixes = $prefixes;
  95. $this->suffixes = $suffixes;
  96. $this->exclude = $exclude;
  97. $this->basepath = $basepath;
  98. parent::__construct($iterator);
  99. }
  100. /**
  101. * @return boolean
  102. */
  103. public function accept()
  104. {
  105. $current = $this->getInnerIterator()->current();
  106. $filename = $current->getFilename();
  107. $realpath = $current->getRealPath();
  108. if ($this->basepath !== NULL) {
  109. $realpath = str_replace($this->basepath, '', $realpath);
  110. }
  111. // Filter files in hidden directories.
  112. if (preg_match('=/\.[^/]*/=', $realpath)) {
  113. return FALSE;
  114. }
  115. return $this->acceptPath($realpath) &&
  116. $this->acceptPrefix($filename) &&
  117. $this->acceptSuffix($filename);
  118. }
  119. /**
  120. * @param string $path
  121. * @return boolean
  122. * @since Method available since Release 1.1.0
  123. */
  124. protected function acceptPath($path)
  125. {
  126. foreach ($this->exclude as $exclude) {
  127. if (strpos($path, $exclude) === 0) {
  128. return FALSE;
  129. }
  130. }
  131. return TRUE;
  132. }
  133. /**
  134. * @param string $filename
  135. * @return boolean
  136. * @since Method available since Release 1.1.0
  137. */
  138. protected function acceptPrefix($filename)
  139. {
  140. return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
  141. }
  142. /**
  143. * @param string $filename
  144. * @return boolean
  145. * @since Method available since Release 1.1.0
  146. */
  147. protected function acceptSuffix($filename)
  148. {
  149. return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
  150. }
  151. /**
  152. * @param string $filename
  153. * @param array $subString
  154. * @param integer $type
  155. * @return boolean
  156. * @since Method available since Release 1.1.0
  157. */
  158. protected function acceptSubString($filename, array $subStrings, $type)
  159. {
  160. if (empty($subStrings)) {
  161. return TRUE;
  162. }
  163. $matched = FALSE;
  164. foreach ($subStrings as $string) {
  165. if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
  166. ($type == self::SUFFIX &&
  167. substr($filename, -1 * strlen($string)) == $string)) {
  168. $matched = TRUE;
  169. break;
  170. }
  171. }
  172. return $matched;
  173. }
  174. }