Skip to content

Commit

Permalink
[5.4] Implement standalone if empty in a NB-Change-Way (#18738)
Browse files Browse the repository at this point in the history
* 5.4 Implement if empty in as NB-Change

* Fix little StyleCI Issue

* Fix little StyleCI Issue #2

* Fix little StyleCI Issue #3

* Update CompilesLoops.php
  • Loading branch information
Lukas Kämmerling authored and taylorotwell committed Apr 9, 2017
1 parent 73c673e commit 3687363
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Illuminate/View/Compilers/Concerns/CompilesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ protected function compileForelse($expression)
}

/**
* Compile the for-else-empty statements into valid PHP.
* Compile the for-else-empty and empty statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEmpty()
protected function compileEmpty($expression)
{
if ($expression) {
return "<?php if(empty{$expression}): ?>";
}

$empty = '$__empty_'.$this->forElseCounter--;

return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>";
Expand All @@ -56,6 +61,16 @@ protected function compileEndforelse()
return '<?php endif; ?>';
}

/**
* Compile the end-empty statements into valid PHP.
*
* @return string
*/
protected function compileEndEmpty()
{
return '<?php endif; ?>';
}

/**
* Compile the for statements into valid PHP.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/View/Blade/BladeIfIEmptyStatementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Tests\Blade;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\View\Compilers\BladeCompiler;

class BladeIfEmptyStatementsTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testIfStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@empty ($test)
breeze
@endempty';
$expected = '<?php if(empty($test)): ?>
breeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
}

0 comments on commit 3687363

Please sign in to comment.