Skip to content

Commit cd7809b

Browse files
committed
Apply same logic in PHP handler for echoing and returning content, add tests
1 parent 000af42 commit cd7809b

17 files changed

Lines changed: 141 additions & 8 deletions

src/routing/PhpRouteHandler.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@ public function __invoke(Request $request): Response
3333
$outputValue = ob_get_clean();
3434

3535
if ($returnValue === 1 && is_string($outputValue)) {
36-
if (html_contains_full_html($outputValue)) {
37-
return new Response($outputValue, 200);
38-
} else {
39-
$layout = html_extract_layout($outputValue);
40-
$title = html_extract_title($outputValue, basename($path));
41-
return $this->html($layout, ['title' => $title, 'html' => $outputValue]);
42-
}
36+
return $this->handleHtml($outputValue, basename($path));
4337
} elseif (is_string($returnValue)) {
4438
if (is_string($outputValue) && strlen($outputValue) > 0) {
4539
throw new \Exception('In the PHP file the return value must be omitted if an output was made via echo: ' . $path);
4640
}
47-
return new Response($returnValue, 200);
41+
return $this->handleHtml($returnValue, basename($path));
4842
} elseif (is_array($returnValue)) {
4943
return $this->json($returnValue);
5044
} elseif ($returnValue instanceof Response) {
@@ -54,6 +48,17 @@ public function __invoke(Request $request): Response
5448
}
5549
}
5650

51+
private function handleHtml(string $content, string $defaultTitle): Response
52+
{
53+
if (html_contains_full_html($content)) {
54+
return new Response($content, 200);
55+
} else {
56+
$layout = html_extract_layout($content);
57+
$title = html_extract_title($content, $defaultTitle);
58+
return $this->html($layout, ['title' => $title, 'html' => $content]);
59+
}
60+
}
61+
5762
public function html(string $template, array $context = []): Response
5863
{
5964
$html = $this->render($template, $context);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2>HTML Default</h2>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- layout: invalid.html.twig -->
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- layout: test.html.twig -->
2+
<h2>HTML Test</h2>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Markdown Default
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- layout: invalid.html.twig -->
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- layout: test.html.twig -->
2+
## Markdown Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2>PHP Echo Default</h2>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<h2>PHP Echo Full</h2>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- layout: invalid.html.twig -->

0 commit comments

Comments
 (0)