8000 bug #15802 [Finder] Handle filtering of recursive iterators and use i… · symfony/symfony@150bcc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 150bcc9

Browse files
committed
bug #15802 [Finder] Handle filtering of recursive iterators and use it to skip looping over excluded directories (nicolas-grekas)
This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes #15802). Discussion ---------- [Finder] Handle filtering of recursive iterators and use it to skip looping over excluded directories | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #5951, #8685 | License | MIT | Doc PR | - By implementing RecursiveIterator in FilterIterator, we can make it able to skip children of excluded branches of recursive inner iterators. We use it immediately for our main target: skip over children of excluded directories in the Finder. This is a significant performance boost when iterating over big directories, thus the "bugfix" status. Commits ------- 8c691bd [Finder] Handle filtering of recursive iterators and use it to skip looping over excluded directories
2 parents 306d88a + 8c691bd commit 150bcc9

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Symfony/Component/Finder/Adapter/PhpAdapter.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ public function searchInDirectory($dir)
3131
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
3232
}
3333

34-
$iterator = new \RecursiveIteratorIterator(
35-
new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs),
36-
\RecursiveIteratorIterator::SELF_FIRST
37-
);
34+
$iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
35+
36+
if ($this->exclude) {
37+
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
38+
}
39+
40+
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
3841

3942
if ($this->minDepth > 0 || $this->maxDepth < PHP_INT_MAX) {
4043
$iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->minDepth, $this->maxDepth);
@@ -44,10 +47,6 @@ public function searchInDirectory($dir)
4447
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
4548
}
4649

47-
if ($this->exclude) {
48-
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
49-
}
50-
5150
if ($this->names || $this->notNames) {
5251
$iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
5352
}

src/Symfony/Component/Finder/Iterator/FilterIterator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@
1818
*
1919
* @author Alex Bogomazov
2020
*/
21-
abstract class FilterIterator extends \FilterIterator
21+
abstract class FilterIterator extends \FilterIterator implements \RecursiveIterator
2222
{
23+
public function hasChildren()
24+
{
25+
return $this->getInnerIterator() instanceof \RecursiveIterator && $this->getInnerIterator()->hasChildren();
26+
}
27+
28+
public function getChildren()
29+
{
30+
return $this->getInnerIterator()->getChildren();
31+
}
32+
2333
/**
2434
* This is a workaround for the problem with \FilterIterator leaving inner \FilesystemIterator in wrong state after
2535
* rewind in some cases.

0 commit comments

Comments
 (0)
0