diff options
Diffstat (limited to 'src/Middleware')
-rw-r--r-- | src/Middleware/ErrorHandler.php | 80 | ||||
-rw-r--r-- | src/Middleware/LegacyMiddleware.php | 24 | ||||
-rw-r--r-- | src/Middleware/SetLocale.php | 49 |
3 files changed, 135 insertions, 18 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 276fb3ee..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,29 +277,17 @@ 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(__DIR__ . '/../../templates/layout.html', [ - 'theme' => isset($user) ? $user['color'] : config('theme'), + return response(view('layouts/app', [ 'title' => $title, - 'atom_link' => ($page == 'news' || $page == 'user_meetings') - ? ' <link href="' - . page_link_to('atom', $parameters) - . '" type = "application/atom+xml" rel = "alternate" title = "Atom Feed">' - : '', - 'start_page_url' => page_link_to('/'), - 'credits_url' => page_link_to('credits'), + 'atom_feed' => ($page == 'news' || $page == 'user_meetings') ? $parameters : [], 'menu' => make_menu(), 'content' => msg() . $content, 'header_toolbar' => header_toolbar(), - 'faq_url' => config('faq_url'), - 'contact_email' => config('contact_email'), - 'locale' => locale(), 'event_info' => EventConfig_info($event_config) . ' <br />' - ]), $status); + ]), 200); } } diff --git a/src/Middleware/SetLocale.php b/src/Middleware/SetLocale.php new file mode 100644 index 00000000..86fa0b7f --- /dev/null +++ b/src/Middleware/SetLocale.php @@ -0,0 +1,49 @@ +<?php + +namespace Engelsystem\Middleware; + +use Engelsystem\Helpers\Translator; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Session; + +class SetLocale implements MiddlewareInterface +{ + /** @var Translator */ + protected $translator; + + /** @var Session */ + protected $session; + + /** + * @param Translator $translator + * @param Session $session + */ + public function __construct(Translator $translator, Session $session) + { + $this->translator = $translator; + $this->session = $session; + } + + /** + * Process an incoming server request and setting the locale if required + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $query = $request->getQueryParams(); + if (isset($query['set-locale']) && $this->translator->hasLocale($query['set-locale'])) { + $locale = $query['set-locale']; + + $this->translator->setLocale($locale); + $this->session->set('locale', $locale); + } + + return $handler->handle($request); + } +} |