| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Finder;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Finder\Comparator\DateComparator;
|
|
|
15 |
use Symfony\Component\Finder\Comparator\NumberComparator;
|
|
|
16 |
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
|
|
|
17 |
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
|
|
|
18 |
use Symfony\Component\Finder\Iterator\DateRangeFilterIterator;
|
|
|
19 |
use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
|
|
|
20 |
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
|
|
|
21 |
use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
|
|
|
22 |
use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
|
|
|
23 |
use Symfony\Component\Finder\Iterator\LazyIterator;
|
|
|
24 |
use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
|
|
|
25 |
use Symfony\Component\Finder\Iterator\SortableIterator;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Finder allows to build rules to find files and directories.
|
|
|
29 |
*
|
|
|
30 |
* It is a thin wrapper around several specialized iterator classes.
|
|
|
31 |
*
|
|
|
32 |
* All rules may be invoked several times.
|
|
|
33 |
*
|
|
|
34 |
* All methods return the current Finder object to allow chaining:
|
|
|
35 |
*
|
|
|
36 |
* $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
|
|
|
37 |
*
|
|
|
38 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
39 |
*
|
|
|
40 |
* @implements \IteratorAggregate<string, SplFileInfo>
|
|
|
41 |
*/
|
|
|
42 |
class Finder implements \IteratorAggregate, \Countable
|
|
|
43 |
{
|
|
|
44 |
public const IGNORE_VCS_FILES = 1;
|
|
|
45 |
public const IGNORE_DOT_FILES = 2;
|
|
|
46 |
public const IGNORE_VCS_IGNORED_FILES = 4;
|
|
|
47 |
|
|
|
48 |
private int $mode = 0;
|
|
|
49 |
private array $names = [];
|
|
|
50 |
private array $notNames = [];
|
|
|
51 |
private array $exclude = [];
|
|
|
52 |
private array $filters = [];
|
|
|
53 |
private array $depths = [];
|
|
|
54 |
private array $sizes = [];
|
|
|
55 |
private bool $followLinks = false;
|
|
|
56 |
private bool $reverseSorting = false;
|
|
|
57 |
private \Closure|int|false $sort = false;
|
|
|
58 |
private int $ignore = 0;
|
|
|
59 |
private array $dirs = [];
|
|
|
60 |
private array $dates = [];
|
|
|
61 |
private array $iterators = [];
|
|
|
62 |
private array $contains = [];
|
|
|
63 |
private array $notContains = [];
|
|
|
64 |
private array $paths = [];
|
|
|
65 |
private array $notPaths = [];
|
|
|
66 |
private bool $ignoreUnreadableDirs = false;
|
|
|
67 |
|
|
|
68 |
private static array $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
|
|
|
69 |
|
|
|
70 |
public function __construct()
|
|
|
71 |
{
|
|
|
72 |
$this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Creates a new Finder.
|
|
|
77 |
*/
|
|
|
78 |
public static function create(): static
|
|
|
79 |
{
|
|
|
80 |
return new static();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Restricts the matching to directories only.
|
|
|
85 |
*
|
|
|
86 |
* @return $this
|
|
|
87 |
*/
|
|
|
88 |
public function directories(): static
|
|
|
89 |
{
|
|
|
90 |
$this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
|
|
|
91 |
|
|
|
92 |
return $this;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Restricts the matching to files only.
|
|
|
97 |
*
|
|
|
98 |
* @return $this
|
|
|
99 |
*/
|
|
|
100 |
public function files(): static
|
|
|
101 |
{
|
|
|
102 |
$this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
|
|
|
103 |
|
|
|
104 |
return $this;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Adds tests for the directory depth.
|
|
|
109 |
*
|
|
|
110 |
* Usage:
|
|
|
111 |
*
|
|
|
112 |
* $finder->depth('> 1') // the Finder will start matching at level 1.
|
|
|
113 |
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
|
|
|
114 |
* $finder->depth(['>= 1', '< 3'])
|
|
|
115 |
*
|
|
|
116 |
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
|
|
|
117 |
*
|
|
|
118 |
* @return $this
|
|
|
119 |
*
|
|
|
120 |
* @see DepthRangeFilterIterator
|
|
|
121 |
* @see NumberComparator
|
|
|
122 |
*/
|
|
|
123 |
public function depth(string|int|array $levels): static
|
|
|
124 |
{
|
|
|
125 |
foreach ((array) $levels as $level) {
|
|
|
126 |
$this->depths[] = new Comparator\NumberComparator($level);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
return $this;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Adds tests for file dates (last modified).
|
|
|
134 |
*
|
|
|
135 |
* The date must be something that strtotime() is able to parse:
|
|
|
136 |
*
|
|
|
137 |
* $finder->date('since yesterday');
|
|
|
138 |
* $finder->date('until 2 days ago');
|
|
|
139 |
* $finder->date('> now - 2 hours');
|
|
|
140 |
* $finder->date('>= 2005-10-15');
|
|
|
141 |
* $finder->date(['>= 2005-10-15', '<= 2006-05-27']);
|
|
|
142 |
*
|
|
|
143 |
* @param string|string[] $dates A date range string or an array of date ranges
|
|
|
144 |
*
|
|
|
145 |
* @return $this
|
|
|
146 |
*
|
|
|
147 |
* @see strtotime
|
|
|
148 |
* @see DateRangeFilterIterator
|
|
|
149 |
* @see DateComparator
|
|
|
150 |
*/
|
|
|
151 |
public function date(string|array $dates): static
|
|
|
152 |
{
|
|
|
153 |
foreach ((array) $dates as $date) {
|
|
|
154 |
$this->dates[] = new Comparator\DateComparator($date);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
return $this;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Adds rules that files must match.
|
|
|
162 |
*
|
|
|
163 |
* You can use patterns (delimited with / sign), globs or simple strings.
|
|
|
164 |
*
|
|
|
165 |
* $finder->name('*.php')
|
|
|
166 |
* $finder->name('/\.php$/') // same as above
|
|
|
167 |
* $finder->name('test.php')
|
|
|
168 |
* $finder->name(['test.py', 'test.php'])
|
|
|
169 |
*
|
|
|
170 |
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
|
|
171 |
*
|
|
|
172 |
* @return $this
|
|
|
173 |
*
|
|
|
174 |
* @see FilenameFilterIterator
|
|
|
175 |
*/
|
|
|
176 |
public function name(string|array $patterns): static
|
|
|
177 |
{
|
|
|
178 |
$this->names = array_merge($this->names, (array) $patterns);
|
|
|
179 |
|
|
|
180 |
return $this;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Adds rules that files must not match.
|
|
|
185 |
*
|
|
|
186 |
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
|
|
187 |
*
|
|
|
188 |
* @return $this
|
|
|
189 |
*
|
|
|
190 |
* @see FilenameFilterIterator
|
|
|
191 |
*/
|
|
|
192 |
public function notName(string|array $patterns): static
|
|
|
193 |
{
|
|
|
194 |
$this->notNames = array_merge($this->notNames, (array) $patterns);
|
|
|
195 |
|
|
|
196 |
return $this;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Adds tests that file contents must match.
|
|
|
201 |
*
|
|
|
202 |
* Strings or PCRE patterns can be used:
|
|
|
203 |
*
|
|
|
204 |
* $finder->contains('Lorem ipsum')
|
|
|
205 |
* $finder->contains('/Lorem ipsum/i')
|
|
|
206 |
* $finder->contains(['dolor', '/ipsum/i'])
|
|
|
207 |
*
|
|
|
208 |
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
|
|
209 |
*
|
|
|
210 |
* @return $this
|
|
|
211 |
*
|
|
|
212 |
* @see FilecontentFilterIterator
|
|
|
213 |
*/
|
|
|
214 |
public function contains(string|array $patterns): static
|
|
|
215 |
{
|
|
|
216 |
$this->contains = array_merge($this->contains, (array) $patterns);
|
|
|
217 |
|
|
|
218 |
return $this;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Adds tests that file contents must not match.
|
|
|
223 |
*
|
|
|
224 |
* Strings or PCRE patterns can be used:
|
|
|
225 |
*
|
|
|
226 |
* $finder->notContains('Lorem ipsum')
|
|
|
227 |
* $finder->notContains('/Lorem ipsum/i')
|
|
|
228 |
* $finder->notContains(['lorem', '/dolor/i'])
|
|
|
229 |
*
|
|
|
230 |
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
|
|
231 |
*
|
|
|
232 |
* @return $this
|
|
|
233 |
*
|
|
|
234 |
* @see FilecontentFilterIterator
|
|
|
235 |
*/
|
|
|
236 |
public function notContains(string|array $patterns): static
|
|
|
237 |
{
|
|
|
238 |
$this->notContains = array_merge($this->notContains, (array) $patterns);
|
|
|
239 |
|
|
|
240 |
return $this;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* Adds rules that filenames must match.
|
|
|
245 |
*
|
|
|
246 |
* You can use patterns (delimited with / sign) or simple strings.
|
|
|
247 |
*
|
|
|
248 |
* $finder->path('some/special/dir')
|
|
|
249 |
* $finder->path('/some\/special\/dir/') // same as above
|
|
|
250 |
* $finder->path(['some dir', 'another/dir'])
|
|
|
251 |
*
|
|
|
252 |
* Use only / as dirname separator.
|
|
|
253 |
*
|
|
|
254 |
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
|
|
255 |
*
|
|
|
256 |
* @return $this
|
|
|
257 |
*
|
|
|
258 |
* @see FilenameFilterIterator
|
|
|
259 |
*/
|
|
|
260 |
public function path(string|array $patterns): static
|
|
|
261 |
{
|
|
|
262 |
$this->paths = array_merge($this->paths, (array) $patterns);
|
|
|
263 |
|
|
|
264 |
return $this;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Adds rules that filenames must not match.
|
|
|
269 |
*
|
|
|
270 |
* You can use patterns (delimited with / sign) or simple strings.
|
|
|
271 |
*
|
|
|
272 |
* $finder->notPath('some/special/dir')
|
|
|
273 |
* $finder->notPath('/some\/special\/dir/') // same as above
|
|
|
274 |
* $finder->notPath(['some/file.txt', 'another/file.log'])
|
|
|
275 |
*
|
|
|
276 |
* Use only / as dirname separator.
|
|
|
277 |
*
|
|
|
278 |
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
|
|
279 |
*
|
|
|
280 |
* @return $this
|
|
|
281 |
*
|
|
|
282 |
* @see FilenameFilterIterator
|
|
|
283 |
*/
|
|
|
284 |
public function notPath(string|array $patterns): static
|
|
|
285 |
{
|
|
|
286 |
$this->notPaths = array_merge($this->notPaths, (array) $patterns);
|
|
|
287 |
|
|
|
288 |
return $this;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
/**
|
|
|
292 |
* Adds tests for file sizes.
|
|
|
293 |
*
|
|
|
294 |
* $finder->size('> 10K');
|
|
|
295 |
* $finder->size('<= 1Ki');
|
|
|
296 |
* $finder->size(4);
|
|
|
297 |
* $finder->size(['> 10K', '< 20K'])
|
|
|
298 |
*
|
|
|
299 |
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
|
|
|
300 |
*
|
|
|
301 |
* @return $this
|
|
|
302 |
*
|
|
|
303 |
* @see SizeRangeFilterIterator
|
|
|
304 |
* @see NumberComparator
|
|
|
305 |
*/
|
|
|
306 |
public function size(string|int|array $sizes): static
|
|
|
307 |
{
|
|
|
308 |
foreach ((array) $sizes as $size) {
|
|
|
309 |
$this->sizes[] = new Comparator\NumberComparator($size);
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
return $this;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
/**
|
|
|
316 |
* Excludes directories.
|
|
|
317 |
*
|
|
|
318 |
* Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
|
|
|
319 |
*
|
|
|
320 |
* $finder->in(__DIR__)->exclude('ruby');
|
|
|
321 |
*
|
|
|
322 |
* @param string|array $dirs A directory path or an array of directories
|
|
|
323 |
*
|
|
|
324 |
* @return $this
|
|
|
325 |
*
|
|
|
326 |
* @see ExcludeDirectoryFilterIterator
|
|
|
327 |
*/
|
|
|
328 |
public function exclude(string|array $dirs): static
|
|
|
329 |
{
|
|
|
330 |
$this->exclude = array_merge($this->exclude, (array) $dirs);
|
|
|
331 |
|
|
|
332 |
return $this;
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
/**
|
|
|
336 |
* Excludes "hidden" directories and files (starting with a dot).
|
|
|
337 |
*
|
|
|
338 |
* This option is enabled by default.
|
|
|
339 |
*
|
|
|
340 |
* @return $this
|
|
|
341 |
*
|
|
|
342 |
* @see ExcludeDirectoryFilterIterator
|
|
|
343 |
*/
|
|
|
344 |
public function ignoreDotFiles(bool $ignoreDotFiles): static
|
|
|
345 |
{
|
|
|
346 |
if ($ignoreDotFiles) {
|
|
|
347 |
$this->ignore |= static::IGNORE_DOT_FILES;
|
|
|
348 |
} else {
|
|
|
349 |
$this->ignore &= ~static::IGNORE_DOT_FILES;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
return $this;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Forces the finder to ignore version control directories.
|
|
|
357 |
*
|
|
|
358 |
* This option is enabled by default.
|
|
|
359 |
*
|
|
|
360 |
* @return $this
|
|
|
361 |
*
|
|
|
362 |
* @see ExcludeDirectoryFilterIterator
|
|
|
363 |
*/
|
|
|
364 |
public function ignoreVCS(bool $ignoreVCS): static
|
|
|
365 |
{
|
|
|
366 |
if ($ignoreVCS) {
|
|
|
367 |
$this->ignore |= static::IGNORE_VCS_FILES;
|
|
|
368 |
} else {
|
|
|
369 |
$this->ignore &= ~static::IGNORE_VCS_FILES;
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
return $this;
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
/**
|
|
|
376 |
* Forces Finder to obey .gitignore and ignore files based on rules listed there.
|
|
|
377 |
*
|
|
|
378 |
* This option is disabled by default.
|
|
|
379 |
*
|
|
|
380 |
* @return $this
|
|
|
381 |
*/
|
|
|
382 |
public function ignoreVCSIgnored(bool $ignoreVCSIgnored): static
|
|
|
383 |
{
|
|
|
384 |
if ($ignoreVCSIgnored) {
|
|
|
385 |
$this->ignore |= static::IGNORE_VCS_IGNORED_FILES;
|
|
|
386 |
} else {
|
|
|
387 |
$this->ignore &= ~static::IGNORE_VCS_IGNORED_FILES;
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
return $this;
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
/**
|
|
|
394 |
* Adds VCS patterns.
|
|
|
395 |
*
|
|
|
396 |
* @see ignoreVCS()
|
|
|
397 |
*
|
|
|
398 |
* @param string|string[] $pattern VCS patterns to ignore
|
|
|
399 |
*/
|
|
|
400 |
public static function addVCSPattern(string|array $pattern)
|
|
|
401 |
{
|
|
|
402 |
foreach ((array) $pattern as $p) {
|
|
|
403 |
self::$vcsPatterns[] = $p;
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
self::$vcsPatterns = array_unique(self::$vcsPatterns);
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Sorts files and directories by an anonymous function.
|
|
|
411 |
*
|
|
|
412 |
* The anonymous function receives two \SplFileInfo instances to compare.
|
|
|
413 |
*
|
|
|
414 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
415 |
*
|
|
|
416 |
* @return $this
|
|
|
417 |
*
|
|
|
418 |
* @see SortableIterator
|
|
|
419 |
*/
|
|
|
420 |
public function sort(\Closure $closure): static
|
|
|
421 |
{
|
|
|
422 |
$this->sort = $closure;
|
|
|
423 |
|
|
|
424 |
return $this;
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
/**
|
|
|
428 |
* Sorts files and directories by extension.
|
|
|
429 |
*
|
|
|
430 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
431 |
*
|
|
|
432 |
* @return $this
|
|
|
433 |
*
|
|
|
434 |
* @see SortableIterator
|
|
|
435 |
*/
|
|
|
436 |
public function sortByExtension(): static
|
|
|
437 |
{
|
|
|
438 |
$this->sort = Iterator\SortableIterator::SORT_BY_EXTENSION;
|
|
|
439 |
|
|
|
440 |
return $this;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
/**
|
|
|
444 |
* Sorts files and directories by name.
|
|
|
445 |
*
|
|
|
446 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
447 |
*
|
|
|
448 |
* @return $this
|
|
|
449 |
*
|
|
|
450 |
* @see SortableIterator
|
|
|
451 |
*/
|
|
|
452 |
public function sortByName(bool $useNaturalSort = false): static
|
|
|
453 |
{
|
|
|
454 |
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\SortableIterator::SORT_BY_NAME;
|
|
|
455 |
|
|
|
456 |
return $this;
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
/**
|
|
|
460 |
* Sorts files and directories by name case insensitive.
|
|
|
461 |
*
|
|
|
462 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
463 |
*
|
|
|
464 |
* @return $this
|
|
|
465 |
*
|
|
|
466 |
* @see SortableIterator
|
|
|
467 |
*/
|
|
|
468 |
public function sortByCaseInsensitiveName(bool $useNaturalSort = false): static
|
|
|
469 |
{
|
|
|
470 |
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : Iterator\SortableIterator::SORT_BY_NAME_CASE_INSENSITIVE;
|
|
|
471 |
|
|
|
472 |
return $this;
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
/**
|
|
|
476 |
* Sorts files and directories by size.
|
|
|
477 |
*
|
|
|
478 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
479 |
*
|
|
|
480 |
* @return $this
|
|
|
481 |
*
|
|
|
482 |
* @see SortableIterator
|
|
|
483 |
*/
|
|
|
484 |
public function sortBySize(): static
|
|
|
485 |
{
|
|
|
486 |
$this->sort = Iterator\SortableIterator::SORT_BY_SIZE;
|
|
|
487 |
|
|
|
488 |
return $this;
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
/**
|
|
|
492 |
* Sorts files and directories by type (directories before files), then by name.
|
|
|
493 |
*
|
|
|
494 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
495 |
*
|
|
|
496 |
* @return $this
|
|
|
497 |
*
|
|
|
498 |
* @see SortableIterator
|
|
|
499 |
*/
|
|
|
500 |
public function sortByType(): static
|
|
|
501 |
{
|
|
|
502 |
$this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
|
|
|
503 |
|
|
|
504 |
return $this;
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
/**
|
|
|
508 |
* Sorts files and directories by the last accessed time.
|
|
|
509 |
*
|
|
|
510 |
* This is the time that the file was last accessed, read or written to.
|
|
|
511 |
*
|
|
|
512 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
513 |
*
|
|
|
514 |
* @return $this
|
|
|
515 |
*
|
|
|
516 |
* @see SortableIterator
|
|
|
517 |
*/
|
|
|
518 |
public function sortByAccessedTime(): static
|
|
|
519 |
{
|
|
|
520 |
$this->sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME;
|
|
|
521 |
|
|
|
522 |
return $this;
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
/**
|
|
|
526 |
* Reverses the sorting.
|
|
|
527 |
*
|
|
|
528 |
* @return $this
|
|
|
529 |
*/
|
|
|
530 |
public function reverseSorting(): static
|
|
|
531 |
{
|
|
|
532 |
$this->reverseSorting = true;
|
|
|
533 |
|
|
|
534 |
return $this;
|
|
|
535 |
}
|
|
|
536 |
|
|
|
537 |
/**
|
|
|
538 |
* Sorts files and directories by the last inode changed time.
|
|
|
539 |
*
|
|
|
540 |
* This is the time that the inode information was last modified (permissions, owner, group or other metadata).
|
|
|
541 |
*
|
|
|
542 |
* On Windows, since inode is not available, changed time is actually the file creation time.
|
|
|
543 |
*
|
|
|
544 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
545 |
*
|
|
|
546 |
* @return $this
|
|
|
547 |
*
|
|
|
548 |
* @see SortableIterator
|
|
|
549 |
*/
|
|
|
550 |
public function sortByChangedTime(): static
|
|
|
551 |
{
|
|
|
552 |
$this->sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME;
|
|
|
553 |
|
|
|
554 |
return $this;
|
|
|
555 |
}
|
|
|
556 |
|
|
|
557 |
/**
|
|
|
558 |
* Sorts files and directories by the last modified time.
|
|
|
559 |
*
|
|
|
560 |
* This is the last time the actual contents of the file were last modified.
|
|
|
561 |
*
|
|
|
562 |
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
563 |
*
|
|
|
564 |
* @return $this
|
|
|
565 |
*
|
|
|
566 |
* @see SortableIterator
|
|
|
567 |
*/
|
|
|
568 |
public function sortByModifiedTime(): static
|
|
|
569 |
{
|
|
|
570 |
$this->sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME;
|
|
|
571 |
|
|
|
572 |
return $this;
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
/**
|
|
|
576 |
* Filters the iterator with an anonymous function.
|
|
|
577 |
*
|
|
|
578 |
* The anonymous function receives a \SplFileInfo and must return false
|
|
|
579 |
* to remove files.
|
|
|
580 |
*
|
|
|
581 |
* @return $this
|
|
|
582 |
*
|
|
|
583 |
* @see CustomFilterIterator
|
|
|
584 |
*/
|
|
|
585 |
public function filter(\Closure $closure): static
|
|
|
586 |
{
|
|
|
587 |
$this->filters[] = $closure;
|
|
|
588 |
|
|
|
589 |
return $this;
|
|
|
590 |
}
|
|
|
591 |
|
|
|
592 |
/**
|
|
|
593 |
* Forces the following of symlinks.
|
|
|
594 |
*
|
|
|
595 |
* @return $this
|
|
|
596 |
*/
|
|
|
597 |
public function followLinks(): static
|
|
|
598 |
{
|
|
|
599 |
$this->followLinks = true;
|
|
|
600 |
|
|
|
601 |
return $this;
|
|
|
602 |
}
|
|
|
603 |
|
|
|
604 |
/**
|
|
|
605 |
* Tells finder to ignore unreadable directories.
|
|
|
606 |
*
|
|
|
607 |
* By default, scanning unreadable directories content throws an AccessDeniedException.
|
|
|
608 |
*
|
|
|
609 |
* @return $this
|
|
|
610 |
*/
|
|
|
611 |
public function ignoreUnreadableDirs(bool $ignore = true): static
|
|
|
612 |
{
|
|
|
613 |
$this->ignoreUnreadableDirs = $ignore;
|
|
|
614 |
|
|
|
615 |
return $this;
|
|
|
616 |
}
|
|
|
617 |
|
|
|
618 |
/**
|
|
|
619 |
* Searches files and directories which match defined rules.
|
|
|
620 |
*
|
|
|
621 |
* @param string|string[] $dirs A directory path or an array of directories
|
|
|
622 |
*
|
|
|
623 |
* @return $this
|
|
|
624 |
*
|
|
|
625 |
* @throws DirectoryNotFoundException if one of the directories does not exist
|
|
|
626 |
*/
|
|
|
627 |
public function in(string|array $dirs): static
|
|
|
628 |
{
|
|
|
629 |
$resolvedDirs = [];
|
|
|
630 |
|
|
|
631 |
foreach ((array) $dirs as $dir) {
|
|
|
632 |
if (is_dir($dir)) {
|
|
|
633 |
$resolvedDirs[] = [$this->normalizeDir($dir)];
|
|
|
634 |
} elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
|
|
|
635 |
sort($glob);
|
|
|
636 |
$resolvedDirs[] = array_map($this->normalizeDir(...), $glob);
|
|
|
637 |
} else {
|
|
|
638 |
throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
|
|
|
639 |
}
|
|
|
640 |
}
|
|
|
641 |
|
|
|
642 |
$this->dirs = array_merge($this->dirs, ...$resolvedDirs);
|
|
|
643 |
|
|
|
644 |
return $this;
|
|
|
645 |
}
|
|
|
646 |
|
|
|
647 |
/**
|
|
|
648 |
* Returns an Iterator for the current Finder configuration.
|
|
|
649 |
*
|
|
|
650 |
* This method implements the IteratorAggregate interface.
|
|
|
651 |
*
|
|
|
652 |
* @return \Iterator<string, SplFileInfo>
|
|
|
653 |
*
|
|
|
654 |
* @throws \LogicException if the in() method has not been called
|
|
|
655 |
*/
|
|
|
656 |
public function getIterator(): \Iterator
|
|
|
657 |
{
|
|
|
658 |
if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
|
|
659 |
throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
|
|
663 |
$iterator = $this->searchInDirectory($this->dirs[0]);
|
|
|
664 |
|
|
|
665 |
if ($this->sort || $this->reverseSorting) {
|
|
|
666 |
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
return $iterator;
|
|
|
670 |
}
|
|
|
671 |
|
|
|
672 |
$iterator = new \AppendIterator();
|
|
|
673 |
foreach ($this->dirs as $dir) {
|
|
|
674 |
$iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
|
|
|
675 |
return $this->searchInDirectory($dir);
|
|
|
676 |
})));
|
|
|
677 |
}
|
|
|
678 |
|
|
|
679 |
foreach ($this->iterators as $it) {
|
|
|
680 |
$iterator->append($it);
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
if ($this->sort || $this->reverseSorting) {
|
|
|
684 |
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
return $iterator;
|
|
|
688 |
}
|
|
|
689 |
|
|
|
690 |
/**
|
|
|
691 |
* Appends an existing set of files/directories to the finder.
|
|
|
692 |
*
|
|
|
693 |
* The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
|
|
|
694 |
*
|
|
|
695 |
* @return $this
|
|
|
696 |
*
|
|
|
697 |
* @throws \InvalidArgumentException when the given argument is not iterable
|
|
|
698 |
*/
|
|
|
699 |
public function append(iterable $iterator): static
|
|
|
700 |
{
|
|
|
701 |
if ($iterator instanceof \IteratorAggregate) {
|
|
|
702 |
$this->iterators[] = $iterator->getIterator();
|
|
|
703 |
} elseif ($iterator instanceof \Iterator) {
|
|
|
704 |
$this->iterators[] = $iterator;
|
|
|
705 |
} elseif (is_iterable($iterator)) {
|
|
|
706 |
$it = new \ArrayIterator();
|
|
|
707 |
foreach ($iterator as $file) {
|
|
|
708 |
$file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
|
|
|
709 |
$it[$file->getPathname()] = $file;
|
|
|
710 |
}
|
|
|
711 |
$this->iterators[] = $it;
|
|
|
712 |
} else {
|
|
|
713 |
throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
return $this;
|
|
|
717 |
}
|
|
|
718 |
|
|
|
719 |
/**
|
|
|
720 |
* Check if any results were found.
|
|
|
721 |
*/
|
|
|
722 |
public function hasResults(): bool
|
|
|
723 |
{
|
|
|
724 |
foreach ($this->getIterator() as $_) {
|
|
|
725 |
return true;
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
return false;
|
|
|
729 |
}
|
|
|
730 |
|
|
|
731 |
/**
|
|
|
732 |
* Counts all the results collected by the iterators.
|
|
|
733 |
*/
|
|
|
734 |
public function count(): int
|
|
|
735 |
{
|
|
|
736 |
return iterator_count($this->getIterator());
|
|
|
737 |
}
|
|
|
738 |
|
|
|
739 |
private function searchInDirectory(string $dir): \Iterator
|
|
|
740 |
{
|
|
|
741 |
$exclude = $this->exclude;
|
|
|
742 |
$notPaths = $this->notPaths;
|
|
|
743 |
|
|
|
744 |
if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
|
|
|
745 |
$exclude = array_merge($exclude, self::$vcsPatterns);
|
|
|
746 |
}
|
|
|
747 |
|
|
|
748 |
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
|
|
|
749 |
$notPaths[] = '#(^|/)\..+(/|$)#';
|
|
|
750 |
}
|
|
|
751 |
|
|
|
752 |
$minDepth = 0;
|
|
|
753 |
$maxDepth = \PHP_INT_MAX;
|
|
|
754 |
|
|
|
755 |
foreach ($this->depths as $comparator) {
|
|
|
756 |
switch ($comparator->getOperator()) {
|
|
|
757 |
case '>':
|
|
|
758 |
$minDepth = $comparator->getTarget() + 1;
|
|
|
759 |
break;
|
|
|
760 |
case '>=':
|
|
|
761 |
$minDepth = $comparator->getTarget();
|
|
|
762 |
break;
|
|
|
763 |
case '<':
|
|
|
764 |
$maxDepth = $comparator->getTarget() - 1;
|
|
|
765 |
break;
|
|
|
766 |
case '<=':
|
|
|
767 |
$maxDepth = $comparator->getTarget();
|
|
|
768 |
break;
|
|
|
769 |
default:
|
|
|
770 |
$minDepth = $maxDepth = $comparator->getTarget();
|
|
|
771 |
}
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
$flags = \RecursiveDirectoryIterator::SKIP_DOTS;
|
|
|
775 |
|
|
|
776 |
if ($this->followLinks) {
|
|
|
777 |
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
|
|
|
778 |
}
|
|
|
779 |
|
|
|
780 |
$iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
|
|
|
781 |
|
|
|
782 |
if ($exclude) {
|
|
|
783 |
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $exclude);
|
|
|
784 |
}
|
|
|
785 |
|
|
|
786 |
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
|
|
|
787 |
|
|
|
788 |
if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
|
|
|
789 |
$iterator = new Iterator\DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
if ($this->mode) {
|
|
|
793 |
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
|
|
|
794 |
}
|
|
|
795 |
|
|
|
796 |
if ($this->names || $this->notNames) {
|
|
|
797 |
$iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
|
|
|
798 |
}
|
|
|
799 |
|
|
|
800 |
if ($this->contains || $this->notContains) {
|
|
|
801 |
$iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
|
|
|
802 |
}
|
|
|
803 |
|
|
|
804 |
if ($this->sizes) {
|
|
|
805 |
$iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
if ($this->dates) {
|
|
|
809 |
$iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
|
|
|
810 |
}
|
|
|
811 |
|
|
|
812 |
if ($this->filters) {
|
|
|
813 |
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
|
|
|
814 |
}
|
|
|
815 |
|
|
|
816 |
if ($this->paths || $notPaths) {
|
|
|
817 |
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
|
|
|
821 |
$iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
|
|
|
822 |
}
|
|
|
823 |
|
|
|
824 |
return $iterator;
|
|
|
825 |
}
|
|
|
826 |
|
|
|
827 |
/**
|
|
|
828 |
* Normalizes given directory names by removing trailing slashes.
|
|
|
829 |
*
|
|
|
830 |
* Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
|
|
|
831 |
*/
|
|
|
832 |
private function normalizeDir(string $dir): string
|
|
|
833 |
{
|
|
|
834 |
if ('/' === $dir) {
|
|
|
835 |
return $dir;
|
|
|
836 |
}
|
|
|
837 |
|
|
|
838 |
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
|
|
|
839 |
|
|
|
840 |
if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
|
|
|
841 |
$dir .= '/';
|
|
|
842 |
}
|
|
|
843 |
|
|
|
844 |
return $dir;
|
|
|
845 |
}
|
|
|
846 |
}
|