3 * This file is part of the File_Iterator package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * FilterIterator implementation that filters files based on prefix(es) and/or
13 * suffix(es). Hidden files and files from hidden directories are also filtered.
15 * @since Class available since Release 1.0.0
17 class File_Iterator extends FilterIterator
25 protected $suffixes = array();
30 protected $prefixes = array();
35 protected $exclude = array();
43 * @param Iterator $iterator
44 * @param array $suffixes
45 * @param array $prefixes
46 * @param array $exclude
47 * @param string $basepath
49 public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
51 $exclude = array_filter(array_map('realpath', $exclude));
53 if ($basepath !== NULL) {
54 $basepath = realpath($basepath);
57 if ($basepath === FALSE) {
60 foreach ($exclude as &$_exclude) {
61 $_exclude = str_replace($basepath, '', $_exclude);
65 $this->prefixes = $prefixes;
66 $this->suffixes = $suffixes;
67 $this->exclude = $exclude;
68 $this->basepath = $basepath;
70 parent::__construct($iterator);
76 public function accept()
78 $current = $this->getInnerIterator()->current();
79 $filename = $current->getFilename();
80 $realpath = $current->getRealPath();
82 if ($this->basepath !== NULL) {
83 $realpath = str_replace($this->basepath, '', $realpath);
86 // Filter files in hidden directories.
87 if (preg_match('=/\.[^/]*/=', $realpath)) {
91 return $this->acceptPath($realpath) &&
92 $this->acceptPrefix($filename) &&
93 $this->acceptSuffix($filename);
99 * @since Method available since Release 1.1.0
101 protected function acceptPath($path)
103 foreach ($this->exclude as $exclude) {
104 if (strpos($path, $exclude) === 0) {
113 * @param string $filename
115 * @since Method available since Release 1.1.0
117 protected function acceptPrefix($filename)
119 return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
123 * @param string $filename
125 * @since Method available since Release 1.1.0
127 protected function acceptSuffix($filename)
129 return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
133 * @param string $filename
134 * @param array $subString
137 * @since Method available since Release 1.1.0
139 protected function acceptSubString($filename, array $subStrings, $type)
141 if (empty($subStrings)) {
147 foreach ($subStrings as $string) {
148 if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
149 ($type == self::SUFFIX &&
150 substr($filename, -1 * strlen($string)) == $string)) {