Skip to content

Commit 1b1a1b6

Browse files
authored
Merge pull request #43 from tbreuss/develop
Develop
2 parents 2a876d6 + 9ee7c5b commit 1b1a1b6

68 files changed

Lines changed: 229 additions & 209 deletions

Some content is hidden

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

src/Zack.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ public function __construct(
2828

2929
public function run(): void
3030
{
31-
error_reporting($this->config->php->errorReporting);
31+
error_reporting($this->config->php->errorLevel);
3232
ini_set('display_errors', $this->config->php->displayErrors ? '1' : '0');
3333
ini_set('display_startup_errors', $this->config->php->displayStartupErrors ? '1' : '0');
3434
ini_set('log_errors', $this->config->php->logErrors ? '1' : '0');
3535
ini_set('error_log', $this->config->php->errorLog);
3636

37+
set_error_handler(function (int $severity, string $message, string $file = '', int $line = 0) {
38+
throw new \ErrorException($message, 0, $severity, $file, $line);
39+
}, $this->config->php->errorLevel);
40+
3741
$this->initContainer();
3842

3943
$request = HttpFoundation\Request::createFromGlobals();

src/config/PhpConfig.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
readonly class PhpConfig
66
{
7-
public int $errorReporting;
87
public bool $displayErrors;
98
public bool $displayStartupErrors;
10-
public bool $logErrors;
9+
public int $errorLevel;
1110
public string $errorLog;
11+
public bool $logErrors;
1212

1313
public function __construct(array $config, string $logPath)
1414
{
15-
$this->errorReporting = $config['errorReporting'] ?? E_ALL;
1615
$this->displayErrors = $config['displayErrors'] ?? false;
1716
$this->displayStartupErrors = $config['displayStartupErrors'] ?? false;
18-
$this->logErrors = $config['logErrors'] ?? true;
17+
$this->errorLevel = $config['errorLevel'] ?? E_ALL;
1918
$this->errorLog = $config['errorLog'] ?? $logPath . '/errors.log';
19+
$this->logErrors = $config['logErrors'] ?? true;
2020
}
2121
}

src/routing/PhpRouteHandler.php

Lines changed: 32 additions & 7 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;
@@ -23,19 +26,30 @@ public function __invoke(Request $request): Response
2326
throw new \Exception('PHP file not found for path: ' . $path);
2427
}
2528

26-
$response = require $path;
29+
ob_start();
30+
$returnValue = require $path;
31+
$outputValue = ob_get_clean();
2732

28-
if (!$response instanceof Response) {
29-
throw new \Exception('PHP file must return a response object: ' . $path);
33+
if ($returnValue === 1 && is_string($outputValue)) {
34+
$title = html_extract_title($outputValue, basename($path));
35+
return $this->html('route-handler.html.twig', ['title' => $title, 'html' => $outputValue]);
36+
} elseif (is_string($returnValue)) {
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);
41+
} elseif (is_array($returnValue)) {
42+
return $this->json($returnValue);
43+
} elseif ($returnValue instanceof Response) {
44+
return $returnValue;
45+
} else {
46+
throw new \Exception('The PHP file must output something or return a string, an array or a response object: ' . $path);
3047
}
31-
32-
return $response;
3348
}
3449

3550
public function html(string $template, array $context = []): Response
3651
{
37-
$twig = $this->container->get('twig');
38-
$html = $twig->render($template, $context);
52+
$html = $this->render($template, $context);
3953
return new Response($html, 200);
4054
}
4155

@@ -46,4 +60,15 @@ public function json(array $context = []): Response
4660
'Content-Type' => 'application/json',
4761
]);
4862
}
63+
64+
public function redirect(string $url, int $status = 302): Response
65+
{
66+
return new RedirectResponse($url, $status);
67+
}
68+
69+
public function render(string $template, array $context = []): string
70+
{
71+
$twig = $this->container->get('twig');
72+
return $twig->render($template, $context);
73+
}
4974
}

tests/_data/routes/articles/[id]/comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
/** @var tebe\zack\routing\PhpRouteHandler $this */
44

5-
return $this->html('articles/comments.html.twig', [
5+
return $this->render('articles/comments.html.twig', [
66
'title' => 'GET: Articles.[id].Comments.Php',
77
]);
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php declare(strict_types=1);
22

3-
return new Symfony\Component\HttpFoundation\Response(
4-
'<h1>POST: Articles.[id].Comments.Php</h1>',
5-
);
3+
/** @var tebe\zack\routing\PhpRouteHandler $this */
4+
5+
return $this->render('articles/comments.html.twig', [
6+
'title' => 'POST: Articles.[id].Comments.Php',
7+
]);

tests/_data/routes/articles/[id]/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
/** @var tebe\zack\routing\PhpRouteHandler $this */
44

5-
return $this->html('articles/detail.html.twig', [
5+
return $this->render('articles/detail.html.twig', [
66
'title' => 'GET: Articles.[id].Php',
77
]);

tests/_data/routes/articles/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
/** @var tebe\zack\routing\PhpRouteHandler $this */
44

5-
return $this->html('articles/list.html.twig', [
5+
return $this->render('articles/list.html.twig', [
66
'title' => 'GET: Articles.Php',
77
]);
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: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<?php declare(strict_types=1);
1+
<?php /** @var Symfony\Component\HttpFoundation\Request $request */ ?>
22

3-
/** @var Symfony\Component\HttpFoundation\Request $request */
4-
5-
use Symfony\Component\HttpFoundation\Response;
6-
7-
$name = $request->attributes->get('name');
8-
9-
return new Response('Hello ' . $name . '!', 200);
3+
<h2>Hello <?= $request->get('name') ?>!</h2>
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
<?php declare(strict_types=1);
1+
<?php /** @var Symfony\Component\HttpFoundation\Request $request */ ?>
22

3-
/** @var Symfony\Component\HttpFoundation\Request $request */
4-
5-
use Symfony\Component\HttpFoundation\Response;
6-
7-
$name = $request->attributes->get('name');
8-
$age = $request->attributes->get('age');
9-
10-
return new Response("Hello $name! You are $age years old.", 200);
3+
<h2>Hello <?= $request->get('name') ?>! You are <?= $request->get('age') ?> years old.</h2>

0 commit comments

Comments
 (0)