summaryrefslogtreecommitdiff
path: root/src/Middleware
diff options
context:
space:
mode:
Diffstat (limited to 'src/Middleware')
-rw-r--r--src/Middleware/ErrorHandler.php80
-rw-r--r--src/Middleware/LegacyMiddleware.php10
2 files changed, 84 insertions, 6 deletions
diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php
new file mode 100644
index 00000000..a7c4cfe6
--- /dev/null
+++ b/src/Middleware/ErrorHandler.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Engelsystem\Middleware;
+
+use Engelsystem\Http\Response;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Twig_LoaderInterface as TwigLoader;
+
+class ErrorHandler implements MiddlewareInterface
+{
+ /** @var TwigLoader */
+ protected $loader;
+
+ /** @var string */
+ protected $viewPrefix = 'errors/';
+
+ /**
+ * @param TwigLoader $loader
+ */
+ public function __construct(TwigLoader $loader)
+ {
+ $this->loader = $loader;
+ }
+
+ /**
+ * Handles any error messages
+ *
+ * Should be added at the beginning
+ *
+ * @param ServerRequestInterface $request
+ * @param RequestHandlerInterface $handler
+ * @return ResponseInterface
+ */
+ public function process(
+ ServerRequestInterface $request,
+ RequestHandlerInterface $handler
+ ): ResponseInterface {
+ $response = $handler->handle($request);
+
+ $statusCode = $response->getStatusCode();
+ if ($statusCode < 400 || !$response instanceof Response) {
+ return $response;
+ }
+
+ $view = $this->selectView($statusCode);
+
+ return $response->withView(
+ $this->viewPrefix . $view,
+ [
+ 'status' => $statusCode,
+ 'content' => $response->getContent(),
+ ],
+ $statusCode,
+ $response->getHeaders()
+ );
+ }
+
+ /**
+ * Select a view based on the given status code
+ *
+ * @param int $statusCode
+ * @return string
+ */
+ protected function selectView(int $statusCode): string
+ {
+ $hundreds = intdiv($statusCode, 100);
+
+ $viewsList = [$statusCode, $hundreds, $hundreds * 100];
+ foreach ($viewsList as $view) {
+ if ($this->loader->exists($this->viewPrefix . $view)) {
+ return $view;
+ }
+ }
+
+ return 'default';
+ }
+}
diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php
index bf849611..78132815 100644
--- a/src/Middleware/LegacyMiddleware.php
+++ b/src/Middleware/LegacyMiddleware.php
@@ -83,7 +83,7 @@ class LegacyMiddleware implements MiddlewareInterface
}
if (empty($title) and empty($content)) {
- $page = '404';
+ $page = 404;
$title = _('Page not found');
$content = _('This page could not be found or you don\'t have permission to view it. You probably have to sign in or register in order to gain access!');
}
@@ -277,10 +277,8 @@ class LegacyMiddleware implements MiddlewareInterface
$parameters['meetings'] = 1;
}
- $status = 200;
- if ($page == '404') {
- $status = 404;
- $content = info($content, true);
+ if (!empty($page) && is_int($page)) {
+ return response($content, (int)$page);
}
return response(view('layouts/app', [
@@ -290,6 +288,6 @@ class LegacyMiddleware implements MiddlewareInterface
'content' => msg() . $content,
'header_toolbar' => header_toolbar(),
'event_info' => EventConfig_info($event_config) . ' <br />'
- ]), $status);
+ ]), 200);
}
}