Skip to content

Commit 18d28da

Browse files
committed
Simplified code
1 parent 40c48d6 commit 18d28da

45 files changed

Lines changed: 118 additions & 129 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/routing/PhpRouteHandler.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace tebe\zack\routing;
44

55
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\HttpFoundation\RedirectResponse;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpFoundation\Response;
89

10+
use function tebe\zack\html_extract_title;
11+
912
class PhpRouteHandler
1013
{
1114
private ?ContainerBuilder $container;
@@ -28,11 +31,13 @@ public function __invoke(Request $request): Response
2831
$outputValue = ob_get_clean();
2932

3033
if ($returnValue === 1 && is_string($outputValue)) {
31-
return new Response($outputValue, 200);
32-
} elseif (is_string($returnValue) && is_string($outputValue) && strlen($outputValue) > 0) {
33-
throw new \Exception('In the PHP file the return value must be omitted if an output was made via echo: ' . $path);
34+
$title = html_extract_title($outputValue, basename($path));
35+
return $this->html('route-handler.html.twig', ['title' => $title, 'html' => $outputValue]);
3436
} elseif (is_string($returnValue)) {
35-
return new Response($returnValue, 200);
37+
if (is_string($outputValue) && strlen($outputValue) > 0) {
38+
throw new \Exception('In the PHP file the return value must be omitted if an output was made via echo: ' . $path);
39+
}
40+
return new Response($returnValue, 200);
3641
} elseif (is_array($returnValue)) {
3742
return $this->json($returnValue);
3843
} elseif ($returnValue instanceof Response) {
@@ -56,6 +61,11 @@ public function json(array $context = []): Response
5661
]);
5762
}
5863

64+
public function redirect(string $url, int $status = 302): Response
65+
{
66+
return new RedirectResponse($url, $status);
67+
}
68+
5969
public function render(string $template, array $context = []): string
6070
{
6171
$twig = $this->container->get('twig');
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1);
22

3-
return new Symfony\Component\HttpFoundation\Response(
4-
'<h1>POST: Articles.[id].Comments.Php</h1>',
5-
);
3+
return $this->render('articles/comments.html.twig', [
4+
'title' => 'POST: Articles.[id].Comments.Php',
5+
]);
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
<?php declare(strict_types=1);
22

3-
use Symfony\Component\HttpFoundation\Response;
4-
5-
return new Response('{"ping": "pong"}', 200, [
6-
'Content-Type' => 'application/json',
7-
]);
3+
return ['ping' => 'pong'];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>htm-page</h1>
1+
<h2>htm-page</h2>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>html-page</h1>
1+
<h2>html-page</h2>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>Hello World!</h1>
1+
<h2>Hello World!</h2>
Lines changed: 1 addition & 1 deletion
Lines changed: 1 addition & 1 deletion
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
<?php declare(strict_types=1);
2-
3-
echo "<h1>php-page</h1>";
1+
<h2>php-page</h2>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>delete.delete.htm</h1>
1+
<h2>delete.delete.htm</h2>

0 commit comments

Comments
 (0)