diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index ff3332c3802e..5f50b247efd9 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -3,7 +3,6 @@ namespace Illuminate\View\Concerns; use Countable; -use Traversable; use Illuminate\Support\Arr; trait ManagesLoops @@ -23,13 +22,7 @@ trait ManagesLoops */ public function addLoop($data) { - $length = null; - - if (is_array($data) || $data instanceof Countable) { - $length = count($data); - } elseif ($data instanceof Traversable) { - $length = iterator_count($data); - } + $length = is_array($data) || $data instanceof Countable ? count($data) : null; $parent = Arr::last($this->loopsStack); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index b4e91ec5ab14..c88a45727e2e 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -555,26 +555,24 @@ public function testAddingLoops() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } - public function testAddingTraversableLoop() + public function testAddingLoopDoesNotCloseGenerator() { $factory = $this->getFactory(); - $data = new \DatePeriod(\Carbon\Carbon::today(), \Carbon\CarbonInterval::day(), \Carbon\Carbon::tomorrow()->endOfDay()); + $data = (new class { + public function generate() + { + for($count = 0; $count < 3; $count++) { + yield ['a', 'b']; + } + } + })->generate(); $factory->addLoop($data); - $expectedLoop = [ - 'iteration' => 0, - 'index' => 0, - 'remaining' => 2, - 'count' => 2, - 'first' => true, - 'last' => false, - 'depth' => 1, - 'parent' => null, - ]; - - $this->assertEquals([$expectedLoop], $factory->getLoopStack()); + foreach ($data as $chunk) { + $this->assertEquals(['a', 'b'], $chunk); + } } public function testAddingUncountableLoop()