Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 <[email protected]>
*
* 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é <[email protected]>
*/
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 <[email protected]>
*
* 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);
}
}