Skip to content
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

[5.5] Revert changes breaking blade templates using generators #23681

Merged
merged 3 commits into from
Mar 24, 2018
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
9 changes: 1 addition & 8 deletions src/Illuminate/View/Concerns/ManagesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\View\Concerns;

use Countable;
use Traversable;
use Illuminate\Support\Arr;

trait ManagesLoops
Expand All @@ -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);

Expand Down
26 changes: 12 additions & 14 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down