Skip to content

Commit 6ce796c

Browse files
authored
fix(view): use bug when compiling view (#893)
1 parent 4cbb932 commit 6ce796c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/Tempest/View/src/Renderers/TempestViewRenderer.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ final class TempestViewRenderer implements ViewRenderer
2222
public function __construct(
2323
private readonly TempestViewCompiler $compiler,
2424
private readonly ViewCache $viewCache,
25-
) {
26-
}
25+
) {}
2726

2827
public function __get(string $name): mixed
2928
{
@@ -54,20 +53,21 @@ private function cleanupCompiled(string $compiled): string
5453

5554
// Cleanup and bundle imports
5655
$imports = arr();
57-
$compiled = $compiled
58-
->replaceRegex('/use .*;/', function (array $matches) use (&$imports) {
59-
$imports[$matches[0]] = $matches[0];
60-
61-
return '';
62-
})
63-
->prepend(
64-
sprintf(
65-
'<?php
56+
57+
$compiled = $compiled->replaceRegex("/^\s*use (function )?.*;/m", function (array $matches) use (&$imports) {
58+
$imports[$matches[0]] = $matches[0];
59+
60+
return '';
61+
});
62+
63+
$compiled = $compiled->prepend(
64+
sprintf(
65+
'<?php
6666
%s
6767
?>',
68-
$imports->implode(PHP_EOL),
69-
),
70-
);
68+
$imports->implode(PHP_EOL),
69+
),
70+
);
7171

7272
// Remove empty PHP blocks
7373
$compiled = $compiled->replaceRegex('/<\?php\s*\?>/', '');
@@ -100,9 +100,9 @@ private function renderCompiled(View $_view, string $_path): string
100100
public function escape(null|string|HtmlString|Stringable $value): string
101101
{
102102
if ($value instanceof HtmlString) {
103-
return (string) $value;
103+
return (string)$value;
104104
}
105105

106-
return htmlentities((string) $value);
106+
return htmlentities((string)$value);
107107
}
108108
}

tests/Fixtures/Views/view-component-with-use-import.view.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
?>
77

88
<x-component name="x-view-component-with-use-import">
9+
because this;
910
<?= uri(HomeController::class) ?>
1011
</x-component>

0 commit comments

Comments
 (0)