From d59479aaf0eab4c7a9b3cbe0a381c5d0d59ef4da Mon Sep 17 00:00:00 2001 From: Jeffrey Angenent Date: Thu, 7 Oct 2021 20:23:06 +0200 Subject: [PATCH 1/2] Prevent calling count() on LazyCollection in Blade loops --- src/Illuminate/View/Concerns/ManagesLoops.php | 5 ++-- tests/View/ViewFactoryTest.php | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index edd6363ec7d3..884607f6406a 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -2,8 +2,8 @@ namespace Illuminate\View\Concerns; -use Countable; use Illuminate\Support\Arr; +use Illuminate\Support\LazyCollection; trait ManagesLoops { @@ -22,7 +22,8 @@ trait ManagesLoops */ public function addLoop($data) { - $length = is_array($data) || $data instanceof Countable ? count($data) : null; + $length = is_countable($data) && ! $data instanceof LazyCollection + ? count($data) : null; $parent = Arr::last($this->loopsStack); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index a3b2a316c2cd..dddc7f261464 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -11,6 +11,7 @@ use Illuminate\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\HtmlString; +use Illuminate\Support\LazyCollection; use Illuminate\View\Compilers\CompilerInterface; use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\Engines\EngineResolver; @@ -702,6 +703,30 @@ public function testAddingUncountableLoop() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } + public function testAddingLazyCollection() + { + $factory = $this->getFactory(); + + $factory->addLoop(new LazyCollection(function () { + $this->fail('LazyCollection\'s generator should not have been called'); + })); + + $expectedLoop = [ + 'iteration' => 0, + 'index' => 0, + 'remaining' => null, + 'count' => null, + 'first' => true, + 'last' => null, + 'odd' => false, + 'even' => true, + 'depth' => 1, + 'parent' => null, + ]; + + $this->assertEquals([$expectedLoop], $factory->getLoopStack()); + } + public function testIncrementingLoopIndices() { $factory = $this->getFactory(); From bab26a6762719bddc66eb8ffe4d2321cb31da23d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 8 Oct 2021 16:10:04 -0500 Subject: [PATCH 2/2] Update ManagesLoops.php --- src/Illuminate/View/Concerns/ManagesLoops.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index 884607f6406a..cb7ccde35f37 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -23,7 +23,8 @@ trait ManagesLoops public function addLoop($data) { $length = is_countable($data) && ! $data instanceof LazyCollection - ? count($data) : null; + ? count($data) + : null; $parent = Arr::last($this->loopsStack);