1 : <?php
2 : /**
3 : * php-file-iterator
4 : *
5 : * Copyright (c) 2009-2010, Sebastian Bergmann <sb@sebastian-bergmann.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 <sb@sebastian-bergmann.de>
39 : * @copyright 2009-2010 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
42 : */
43 :
44 : /**
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.
47 : *
48 : * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
49 : * @copyright 2009-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
50 : * @license http://www.opensource.org/licenses/bsd-license.php BSD License
51 : * @version Release: 1.2.3
52 : * @link http://github.com/sebastianbergmann/php-file-iterator/tree
53 : * @since Class available since Release 1.0.0
54 : */
55 : class File_Iterator extends FilterIterator
56 : {
57 : const PREFIX = 0;
58 : const SUFFIX = 1;
59 :
60 : /**
61 : * @var array
62 : */
63 : protected $suffixes = array();
64 :
65 : /**
66 : * @var array
67 : */
68 : protected $prefixes = array();
69 :
70 : /**
71 : * @var array
72 : */
73 : protected $exclude = array();
74 :
75 : /**
76 : * @var string
77 : */
78 : protected $basepath;
79 :
80 : /**
81 : * @param Iterator $iterator
82 : * @param array $suffixes
83 : * @param array $prefixes
84 : * @param array $exclude
85 : * @param string $basepath
86 : */
87 : public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
88 : {
89 0 : $exclude = array_map('realpath', $exclude);
90 :
91 0 : if ($basepath !== NULL) {
92 0 : $basepath = realpath($basepath);
93 : }
94 :
95 0 : if ($basepath === FALSE) {
96 0 : $basepath = NULL;
97 0 : } else {
98 0 : foreach ($exclude as &$_exclude) {
99 0 : $_exclude = str_replace($basepath, '', $_exclude);
100 0 : }
101 : }
102 :
103 0 : $this->prefixes = $prefixes;
104 0 : $this->suffixes = $suffixes;
105 0 : $this->exclude = $exclude;
106 0 : $this->basepath = $basepath;
107 :
108 0 : parent::__construct($iterator);
109 0 : }
110 :
111 : /**
112 : * @return boolean
113 : */
114 : public function accept()
115 : {
116 0 : $current = $this->getInnerIterator()->current();
117 0 : $filename = $current->getFilename();
118 0 : $realpath = $current->getRealPath();
119 :
120 0 : if ($this->basepath !== NULL) {
121 0 : $realpath = str_replace($this->basepath, '', $realpath);
122 : }
123 :
124 : // Filter files in hidden directories.
125 0 : if (preg_match('=/\.[^/]*/=', $realpath)) {
126 0 : return FALSE;
127 : }
128 :
129 0 : return $this->acceptPath($realpath) &&
130 0 : $this->acceptPrefix($filename) &&
131 0 : $this->acceptSuffix($filename);
132 : }
133 :
134 : /**
135 : * @param string $path
136 : * @return boolean
137 : * @since Method available since Release 1.1.0
138 : */
139 : protected function acceptPath($path)
140 : {
141 0 : foreach ($this->exclude as $exclude) {
142 0 : if (strpos($path, $exclude) === 0) {
143 0 : return FALSE;
144 : }
145 0 : }
146 :
147 0 : return TRUE;
148 : }
149 :
150 : /**
151 : * @param string $filename
152 : * @return boolean
153 : * @since Method available since Release 1.1.0
154 : */
155 : protected function acceptPrefix($filename)
156 : {
157 0 : return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
158 : }
159 :
160 : /**
161 : * @param string $filename
162 : * @return boolean
163 : * @since Method available since Release 1.1.0
164 : */
165 : protected function acceptSuffix($filename)
166 : {
167 0 : return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
168 : }
169 :
170 : /**
171 : * @param string $filename
172 : * @param array $subString
173 : * @param integer $type
174 : * @return boolean
175 : * @since Method available since Release 1.1.0
176 : */
177 : protected function acceptSubString($filename, array $subStrings, $type)
178 : {
179 0 : if (empty($subStrings)) {
180 0 : return TRUE;
181 : }
182 :
183 0 : $matched = FALSE;
184 :
185 0 : foreach ($subStrings as $string) {
186 0 : if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
187 0 : ($type == self::SUFFIX &&
188 0 : substr($filename, -1 * strlen($string)) == $string)) {
189 0 : $matched = TRUE;
190 : break;
191 : }
192 0 : }
193 :
194 0 : return $matched;
195 : }
196 : }
197 : ?>
|