5 * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
39 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
41 * @since File available since Release 1.0.0
45 * FilterIterator implementation that filters files based on prefix(es) and/or
46 * suffix(es). Hidden files and files from hidden directories are also filtered.
48 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
49 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
50 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
51 * @version Release: 1.2.6
52 * @link http://github.com/sebastianbergmann/php-file-iterator/tree
53 * @since Class available since Release 1.0.0
55 class File_Iterator extends FilterIterator
63 protected $suffixes = array();
68 protected $prefixes = array();
73 protected $exclude = array();
81 * @param Iterator $iterator
82 * @param array $suffixes
83 * @param array $prefixes
84 * @param array $exclude
85 * @param string $basepath
87 public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
89 $exclude = array_filter(array_map('realpath', $exclude));
91 if ($basepath !== NULL) {
92 $basepath = realpath($basepath);
95 if ($basepath === FALSE) {
98 foreach ($exclude as &$_exclude) {
99 $_exclude = str_replace($basepath, '', $_exclude);
103 $this->prefixes = $prefixes;
104 $this->suffixes = $suffixes;
105 $this->exclude = $exclude;
106 $this->basepath = $basepath;
108 parent::__construct($iterator);
114 public function accept()
116 $current = $this->getInnerIterator()->current();
117 $filename = $current->getFilename();
118 $realpath = $current->getRealPath();
120 if ($this->basepath !== NULL) {
121 $realpath = str_replace($this->basepath, '', $realpath);
124 // Filter files in hidden directories.
125 if (preg_match('=/\.[^/]*/=', $realpath)) {
129 return $this->acceptPath($realpath) &&
130 $this->acceptPrefix($filename) &&
131 $this->acceptSuffix($filename);
135 * @param string $path
137 * @since Method available since Release 1.1.0
139 protected function acceptPath($path)
141 foreach ($this->exclude as $exclude) {
142 if (strpos($path, $exclude) === 0) {
151 * @param string $filename
153 * @since Method available since Release 1.1.0
155 protected function acceptPrefix($filename)
157 return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
161 * @param string $filename
163 * @since Method available since Release 1.1.0
165 protected function acceptSuffix($filename)
167 return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
171 * @param string $filename
172 * @param array $subString
173 * @param integer $type
175 * @since Method available since Release 1.1.0
177 protected function acceptSubString($filename, array $subStrings, $type)
179 if (empty($subStrings)) {
185 foreach ($subStrings as $string) {
186 if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
187 ($type == self::SUFFIX &&
188 substr($filename, -1 * strlen($string)) == $string)) {