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

[11.x] Add component view instance caching #51152

Closed
Closed
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
32 changes: 31 additions & 1 deletion src/Illuminate/View/Concerns/ManagesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ trait ManagesComponents
*/
protected $componentStack = [];

/**
* The View instances of components being rendered.
*
* @var array
*/
protected $componentViews = [];

/**
* Keeping depth of a component's view being rendered.
*
* @var array
*/
protected $componentViewsDepths = [];

/**
* The original data passed to the component.
*
Expand Down Expand Up @@ -100,7 +114,21 @@ public function renderComponent()
} elseif ($view instanceof Htmlable) {
return $view->toHtml();
} else {
return $this->make($view, $data)->render();
$componentViewDepth = $this->componentViewsDepths[$view] = ($this->componentViewsDepths[$view] ?? -1) + 1;

try {
if (! isset($this->componentViews[$view])) {
$this->componentViews[$view] = [];
}

if (! isset($this->componentViews[$view][$componentViewDepth])) {
$this->componentViews[$view][$componentViewDepth] = $this->make($view);
}

return (clone $this->componentViews[$view][$componentViewDepth])->with($data)->render();
} finally {
$this->componentViewsDepths[$view]--;
}
}
} finally {
$this->currentComponentData = $previousComponentData;
Expand Down Expand Up @@ -217,5 +245,7 @@ protected function flushComponents()
$this->componentStack = [];
$this->componentData = [];
$this->currentComponentData = [];
$this->componentViews = [];
$this->componentViewsDepths = [];
}
}
55 changes: 55 additions & 0 deletions tests/Integration/View/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,61 @@ public function test_consume_with_props()
<div>Slot: F, Color: yellow, Default: foo</div>', trim($view));
}

public function test_reuse_component_views_data()
{
$content = Blade::render('
<x-isset-check param1="1" />
<x-isset-check param1="2" param2="Hello" />
<x-isset-check param1="3" />
');

View::composer('*', fn ($view) => $view->with('globalParam', 'Test'));

$contentWithGlobal = Blade::render('
<x-isset-check param1="1" />
<x-isset-check param1="2" param2="Hello" />
<x-isset-check param1="3" />
');

$this->assertSame('<p>Global Param is not set</p>
<p>Param 1: 1</p>
<p>Param 2 is not set</p>
<p>Global Param is not set</p>
<p>Param 1: 2</p>
<p>Param 2: Hello</p>
<p>Global Param is not set</p>
<p>Param 1: 3</p>
<p>Param 2 is not set</p>', trim($content));

$this->assertSame('<p>Global Param: Test</p>
<p>Param 1: 1</p>
<p>Param 2 is not set</p>
<p>Global Param: Test</p>
<p>Param 1: 2</p>
<p>Param 2: Hello</p>
<p>Global Param: Test</p>
<p>Param 1: 3</p>
<p>Param 2 is not set</p>', trim($contentWithGlobal));
}

public function test_rendering_a_nested_component()
{
$content = Blade::render('@foreach(range(1, 2) as $count) <x-nested :$count /> @endforeach');

$this->assertSame('<div>
1
<small>1</small>
<small>2</small>
<small>3</small>
</div>
<div>
2
<small>1</small>
<small>2</small>
<small>3</small>
</div>', trim($content));
}

public function test_name_attribute_can_be_used_if_using_short_slot_names()
{
$content = Blade::render('<x-input-with-slot>
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/View/templates/components/isset-check.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@if (isset($globalParam))
<p>Global Param: {{ $globalParam }}</p>
@else
<p>Global Param is not set</p>
@endif
@if (isset($param1))
<p>Param 1: {{ $param1 }}</p>
@else
<p>Param 1 is not set</p>
@endif
@if (isset($param2))
<p>Param 2: {{ $param2 }}</p>
@else
<p>Param 2 is not set</p>
@endif
10 changes: 10 additions & 0 deletions tests/Integration/View/templates/components/nested.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@if (isset($nested) == true && $nested == true)
<small>{{ $count }}</small>
@else
<div>
{{ $count }}
@foreach (range(1, 3) as $c)
<x-nested :count="$c" :nested="true" />
@endforeach
</div>
@endif