Skip to content

Commit

Permalink
[8.x] Fix $component not being reverted if component doesn't render (#…
Browse files Browse the repository at this point in the history
…39595)

* Fix $component not being reverted if component doesn't render

* Formatting
  • Loading branch information
tobyzerner authored Nov 16, 2021
1 parent 03a66a4 commit 40f2150
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
16 changes: 7 additions & 9 deletions src/Illuminate/View/Compilers/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ public static function compileClassComponentOpening(string $component, string $a
*/
protected function compileEndComponent()
{
$hash = array_pop(static::$componentHashStack);

return implode("\n", [
'<?php if (isset($__componentOriginal'.$hash.')): ?>',
'<?php $component = $__componentOriginal'.$hash.'; ?>',
'<?php unset($__componentOriginal'.$hash.'); ?>',
'<?php endif; ?>',
'<?php echo $__env->renderComponent(); ?>',
]);
return '<?php echo $__env->renderComponent(); ?>';
}

/**
Expand All @@ -96,8 +88,14 @@ protected function compileEndComponent()
*/
public function compileEndComponentClass()
{
$hash = array_pop(static::$componentHashStack);

return $this->compileEndComponent()."\n".implode("\n", [
'<?php endif; ?>',
'<?php if (isset($__componentOriginal'.$hash.')): ?>',
'<?php $component = $__componentOriginal'.$hash.'; ?>',
'<?php unset($__componentOriginal'.$hash.'); ?>',
'<?php endif; ?>',
]);
}

Expand Down
12 changes: 4 additions & 8 deletions tests/View/Blade/BladeComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ public function testEndComponentsAreCompiled()
{
$this->compiler->newComponentHash('foo');

$this->assertSame('<?php if (isset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33)): ?>
<?php $component = $__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33; ?>
<?php unset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33); ?>
<?php endif; ?>
<?php echo $__env->renderComponent(); ?>', $this->compiler->compileString('@endcomponent'));
$this->assertSame('<?php echo $__env->renderComponent(); ?>', $this->compiler->compileString('@endcomponent'));
}

public function testEndComponentClassesAreCompiled()
{
$this->compiler->newComponentHash('foo');

$this->assertSame('<?php if (isset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33)): ?>
$this->assertSame('<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33)): ?>
<?php $component = $__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33; ?>
<?php unset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33); ?>
<?php endif; ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>', $this->compiler->compileString('@endcomponentClass'));
}

Expand Down

0 comments on commit 40f2150

Please sign in to comment.