1 | <?php |
||
9 | class FileList implements \IteratorAggregate, \Countable |
||
10 | { |
||
11 | /** @var string */ |
||
12 | protected $cacheKey; |
||
13 | |||
14 | /** @var callable */ |
||
15 | protected $source; |
||
16 | |||
17 | /** @var array */ |
||
18 | protected static $cache; |
||
19 | |||
20 | /** @var \Iterator */ |
||
21 | protected $sourceResult; |
||
22 | |||
23 | /** |
||
24 | * @param string $cacheKey Unique key to identify this collection of files |
||
25 | * @param callable $source Callable that return an iterator or array of SplFileInfo |
||
26 | */ |
||
27 | 33 | public function __construct($cacheKey, callable $source) |
|
32 | |||
33 | /** |
||
34 | * Search file names (excluding path) by pattern. |
||
35 | * |
||
36 | * @param string $pattern Pattern to look for in file name (regexp, glob, or string) |
||
37 | * |
||
38 | * @return static |
||
39 | */ |
||
40 | 5 | public function name($pattern) |
|
53 | |||
54 | /** |
||
55 | * Search file names (excluding path) by pattern. |
||
56 | * |
||
57 | * @param string $pattern Pattern to exclude files (regexp, glob, or string) |
||
58 | * |
||
59 | * @return static |
||
60 | */ |
||
61 | 1 | public function notName($pattern) |
|
62 | { |
||
63 | 1 | return new self( |
|
64 | 1 | $this->cacheKey . '->' . __FUNCTION__ . '(' . $pattern . ')', |
|
65 | function () use ($pattern) { |
||
66 | 1 | return new Iterator\FilenameFilterIterator( |
|
67 | 1 | $this->getSourceIterator(), |
|
68 | 1 | [], |
|
69 | 1 | [$pattern] |
|
70 | ); |
||
71 | 1 | } |
|
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Search path names by pattern. |
||
77 | * |
||
78 | * @param string $pattern Pattern to look for in path (regexp, glob, or string) |
||
79 | * |
||
80 | * @return static |
||
81 | */ |
||
82 | 1 | public function path($pattern) |
|
95 | |||
96 | /** |
||
97 | * Search path names by pattern. |
||
98 | * |
||
99 | * @param string $pattern Pattern to exclude paths (regexp, glob, or string) |
||
100 | * |
||
101 | * @return static |
||
102 | */ |
||
103 | 1 | public function notPath($pattern) |
|
116 | |||
117 | /** |
||
118 | * Filters out anything that is not a file. |
||
119 | * |
||
120 | * @return static |
||
121 | */ |
||
122 | 3 | public function files() |
|
134 | |||
135 | /** |
||
136 | * Filters out anything that is not a directory. |
||
137 | * |
||
138 | * @return static |
||
139 | */ |
||
140 | 2 | public function directories() |
|
152 | |||
153 | /** |
||
154 | * Filters out items that do not match the specified level. |
||
155 | * |
||
156 | * @param string $level The depth expression (for example '< 1') |
||
157 | * |
||
158 | * @return static |
||
159 | */ |
||
160 | 3 | public function depth($level) |
|
197 | |||
198 | /** |
||
199 | * Filters out items whose last modified do not match expression. |
||
200 | * |
||
201 | * @param string $date A date range string that can be parsed by `strtotime()`` |
||
202 | * |
||
203 | * @return static |
||
204 | */ |
||
205 | public function date($date) |
||
217 | |||
218 | /** |
||
219 | * Filters out files not matching a string or regexp. |
||
220 | * |
||
221 | * @param string $pattern A pattern (string or regexp) |
||
222 | * |
||
223 | * @return static |
||
224 | */ |
||
225 | 2 | public function contains($pattern) |
|
238 | |||
239 | /** |
||
240 | * Filters out files matching a string or regexp. |
||
241 | * |
||
242 | * @param string $pattern A pattern (string or regexp) |
||
243 | * |
||
244 | * @return static |
||
245 | */ |
||
246 | 1 | public function notContains($pattern) |
|
259 | |||
260 | /** |
||
261 | * Filters using an anonymous function. Function receives a \SplFileInfo and must return false to filter it out. |
||
262 | * |
||
263 | * @param \Closure $closure An anonymous function |
||
264 | * |
||
265 | * @return static |
||
266 | */ |
||
267 | 4 | public function filter(\Closure $closure) |
|
279 | |||
280 | /** |
||
281 | * Returns array of file paths. |
||
282 | * |
||
283 | * @return string[] |
||
284 | */ |
||
285 | 32 | public function toArray() |
|
298 | |||
299 | /** |
||
300 | * @return \Iterator |
||
301 | */ |
||
302 | public function getIterator() |
||
306 | |||
307 | /** |
||
308 | * @return \Iterator |
||
309 | */ |
||
310 | 32 | public function getSourceIterator() |
|
311 | { |
||
312 | 32 | if (!$this->sourceResult) { |
|
313 | 32 | $source = $this->source; |
|
314 | 32 | $result = $source(); |
|
315 | |||
316 | 32 | if ($result instanceof \IteratorAggregate) { |
|
317 | $this->sourceResult = $result->getIterator(); |
||
318 | } elseif ($result instanceof \Iterator) { |
||
319 | 14 | $this->sourceResult = $result; |
|
320 | 18 | } elseif ($result instanceof \Traversable || is_array($result)) { |
|
321 | 18 | $iterator = new \ArrayIterator(); |
|
322 | 18 | foreach ($result as $file) { |
|
323 | 13 | $iterator->append( |
|
324 | $file instanceof \SplFileInfo |
||
325 | 2 | ? $file |
|
326 | 13 | : new SfyFileInfo($file, getcwd(), $file) |
|
327 | ); |
||
328 | } |
||
329 | 18 | $this->sourceResult = $iterator; |
|
330 | } else { |
||
331 | throw new \RuntimeException( |
||
332 | sprintf( |
||
333 | 'Iterator or array was expected instead of %s.', |
||
334 | is_object($result) ? get_class($result) : gettype($result) |
||
335 | ) |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | 32 | return $this->sourceResult; |
|
341 | } |
||
342 | |||
343 | /** |
||
344 | * @return int |
||
345 | */ |
||
346 | 3 | public function count() |
|
350 | } |
||
351 |