Skip to content

[Finder] Use a lazyIterator to close files descriptors when no longer used #40040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
use Symfony\Component\Finder\Iterator\LazyIterator;
use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
use Symfony\Component\Finder\Iterator\SortableIterator;

Expand Down Expand Up @@ -635,7 +636,9 @@ public function getIterator()

$iterator = new \AppendIterator();
foreach ($this->dirs as $dir) {
$iterator->append($this->searchInDirectory($dir));
$iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
return $this->searchInDirectory($dir);
})));
}

foreach ($this->iterators as $it) {
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Finder/Iterator/LazyIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Iterator;

/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class LazyIterator implements \IteratorAggregate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mark it as @internal ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, see #40163

{
private $iteratorFactory;

public function __construct(callable $iteratorFactory)
{
$this->iteratorFactory = $iteratorFactory;
}

public function getIterator()
{
yield from ($this->iteratorFactory)();
}
}
53 changes: 53 additions & 0 deletions src/Symfony/Component/Finder/Tests/Iterator/LazyIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Tests\Iterator;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Iterator\LazyIterator;

class LazyIteratorTest extends TestCase
{
public function testLazy()
{
new LazyIterator(function () {
$this->markTestFailed('lazyIterator should not be called');
});

$this->expectNotToPerformAssertions();
}

public function testDelegate()
{
$iterator = new LazyIterator(function () {
return new Iterator(['foo', 'bar']);
});

$this->assertCount(2, $iterator);
}

public function testInnerDestructedAtTheEnd()
{
$count = 0;
$iterator = new LazyIterator(function () use (&$count) {
++$count;

return new Iterator(['foo', 'bar']);
});

foreach ($iterator as $x) {
}
$this->assertSame(1, $count);
foreach ($iterator as $x) {
}
$this->assertSame(2, $count);
}
}