Fix component style/script dedupe leakage

Use per-render instance state for component style/script dedupe in View::fetch so assets are deduped within a render without leaking across tests or subsequent renders.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
580-view-class-steroids
fadrian06 2 weeks ago
parent 7acf6c00b9
commit 8c277dadd6

@ -29,6 +29,11 @@ class View
private string $componentPrefix;
private string $componentsPath;
private int $fetchDepth = 0;
/** @var array<string, bool> */
private array $styles = [];
/** @var array<string, bool> */
private array $scripts = [];
/**
* @param string $path Path to templates directory
@ -119,6 +124,14 @@ class View
*/
public function fetch(string $file, ?array $data = null): string
{
if ($this->fetchDepth === 0) {
$this->styles = [];
$this->scripts = [];
}
$this->fetchDepth++;
try {
$template = $this->getTemplate($file);
if (!$this->exists($file)) {
@ -149,27 +162,24 @@ class View
$css = $component->css();
$js = $component->js();
static $styles = [];
static $scripts = [];
if ($css && !array_key_exists($template, $styles)) {
if ($css && !array_key_exists($template, $this->styles)) {
$view .= <<<html
<style>
$css
</style>
html;
$styles[$template] = true;
$this->styles[$template] = true;
}
if ($js && !array_key_exists($template, $scripts)) {
if ($js && !array_key_exists($template, $this->scripts)) {
$view .= <<<html
<script>
$js
</script>
html;
$scripts[$template] = true;
$this->scripts[$template] = true;
}
ob_end_clean();
@ -223,6 +233,9 @@ class View
}
return $view;
} finally {
$this->fetchDepth--;
}
}
/**

@ -266,124 +266,124 @@ class ViewTest extends TestCase
public static function pagesDataProvider(): array
{
return [
// [
// 'page-with-component-with-old-syntax',
// <<<'html'
// my-component
// html,
// ],
// [
// 'page-with-component-with-new-syntax',
// <<<'html'
// my-component
// html,
// ],
// [
// 'page-with-component-with-subcomponent',
// <<<'html'
// <div>
// my-component-with-subcomponent
// subcomponent
// </div>
// html,
// ],
// [
// 'page-with-multiple-components',
// <<<'html'
// <ul>
// <li>my-component</li>
// <li>my-component</li>
// </ul>
// html,
// ],
// [
// 'page-with-functional-component',
// <<<'html'
// my-functional-component
// html,
// ],
// [
// 'page-with-class-component',
// <<<'html'
// my-class-component
// html,
// ],
// [
// 'page-with-class-component-with-styles',
// <<<'html'
// <span class="my-class-component-with-styles">
// my-class-component-with-styles
// </span>
// <style>
// .my-class-component-with-styles {
// color: red;
// }
// </style>
// html,
// ],
// [
// 'page-with-class-component-with-scripts',
// <<<'html'
// my-class-component-with-scripts
// <script>console.log('my-class-component-with-scripts')</script>
// html,
// ],
// [
// 'page-with-class-component-that-extends-another-class-component',
// <<<'html'
// another-class-component extended by my-class-component-that-extends-another-class-component
// html,
// ],
// [
// 'page-with-component-with-one-prop',
// <<<'html'
// <html>
// <body>
// <h1>Hello, James</h1>
// </body>
// </html>
// html,
// ['name' => 'James'],
// ],
// [
// 'page-with-component-with-one-prop',
// <<<'html'
// <html>
// <body>
// <h1>Hello, Victoria</h1>
// </body>
// </html>
// html,
// ['name' => 'Victoria'],
// ],
// [
// 'page-with-component-with-two-props',
// <<<'html'
// <html>
// <body>
// <h1>Hello, Astronaut Victoria</h1>
// </body>
// </html>
// html,
// ['name' => 'Victoria', 'occupation' => 'Astronaut'],
// ],
// [
// 'page-with-three-different-components',
// <<<'html'
// my-component
// my-functional-component
// my-class-component
// html,
// ],
// [
// 'page-with-component-with-prop-which-value-contains-spaces-and-numbers',
// <<<'html'
// <h1>Hello, Constantine 1st the Great</h1>
// html,
// ['name' => 'Constantine 1st the Great'],
// ],
[
'page-with-component-with-old-syntax',
<<<'html'
my-component
html,
],
[
'page-with-component-with-new-syntax',
<<<'html'
my-component
html,
],
[
'page-with-component-with-subcomponent',
<<<'html'
<div>
my-component-with-subcomponent
subcomponent
</div>
html,
],
[
'page-with-multiple-components',
<<<'html'
<ul>
<li>my-component</li>
<li>my-component</li>
</ul>
html,
],
[
'page-with-functional-component',
<<<'html'
my-functional-component
html,
],
[
'page-with-class-component',
<<<'html'
my-class-component
html,
],
[
'page-with-class-component-with-styles',
<<<'html'
<span class="my-class-component-with-styles">
my-class-component-with-styles
</span>
<style>
.my-class-component-with-styles {
color: red;
}
</style>
html,
],
[
'page-with-class-component-with-scripts',
<<<'html'
my-class-component-with-scripts
<script>console.log('my-class-component-with-scripts')</script>
html,
],
[
'page-with-class-component-that-extends-another-class-component',
<<<'html'
another-class-component extended by my-class-component-that-extends-another-class-component
html,
],
[
'page-with-component-with-one-prop',
<<<'html'
<html>
<body>
<h1>Hello, James</h1>
</body>
</html>
html,
['name' => 'James'],
],
[
'page-with-component-with-one-prop',
<<<'html'
<html>
<body>
<h1>Hello, Victoria</h1>
</body>
</html>
html,
['name' => 'Victoria'],
],
[
'page-with-component-with-two-props',
<<<'html'
<html>
<body>
<h1>Hello, Astronaut Victoria</h1>
</body>
</html>
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
[
'page-with-three-different-components',
<<<'html'
my-component
my-functional-component
my-class-component
html,
],
[
'page-with-component-with-prop-which-value-contains-spaces-and-numbers',
<<<'html'
<h1>Hello, Constantine 1st the Great</h1>
html,
['name' => 'Constantine 1st the Great'],
],
[
'page-two-same-components-with-one-style-and-script-tag',
<<<'html'

Loading…
Cancel
Save