1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (C) 2023 Davide Franco |
7
|
|
|
* |
8
|
|
|
* This file is part of Bacula-Web. |
9
|
|
|
* |
10
|
|
|
* Bacula-Web is free software: you can redistribute it and/or modify it under the terms of the GNU |
11
|
|
|
* General Public License as published by the Free Software Foundation, either version 2 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Bacula-Web is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
15
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16
|
|
|
* See the GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License along with Bacula-Web. If not, see |
19
|
|
|
* <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Middleware; |
23
|
|
|
|
24
|
|
|
use App\Controller\HomeController; |
25
|
|
|
use Core\App\View; |
26
|
|
|
use Core\App\WebApplication; |
27
|
|
|
use Core\Exception\PageNotFoundException; |
28
|
|
|
use Core\Middleware\MiddlewareInterface; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* RouterMiddleware is a "simple pass" psr-15 middleware implementation |
34
|
|
|
* |
35
|
|
|
* The process method checks if page request param has been provided, if |
36
|
|
|
* the requested page does not exist, it throw a PageNotFoundException. |
37
|
|
|
* If page request param is not provided, it return the Response from the |
38
|
|
|
* default controller (Home dashboard) |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
class RouterMiddleware implements MiddlewareInterface |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @param Request $request |
45
|
|
|
* @param Response $response |
46
|
|
|
* @return Response |
47
|
|
|
* @throws PageNotFoundException |
48
|
|
|
*/ |
49
|
|
|
public function process(Request $request, Response $response): Response |
50
|
|
|
{ |
51
|
|
|
// Inject request params into Request object instance |
52
|
|
|
$params = $request->query->all(); |
53
|
|
|
$params = array_merge($request->request->all(), $params); |
54
|
|
|
$request->attributes->add($params); |
55
|
|
|
$requestedpage = $request->attributes->get('page'); |
56
|
|
|
|
57
|
|
|
$routes = WebApplication::getRoutes(); |
58
|
|
|
|
59
|
|
|
if ( $requestedpage === null) { |
60
|
|
|
$fallback = $routes['home']['callback']; |
61
|
|
|
$response = call_user_func([(new $fallback($request,(new View()))), 'prepare']); |
62
|
|
|
} elseif ((array_key_exists($requestedpage, $routes))) { |
63
|
|
|
$callback = $routes[$requestedpage]['callback']; |
64
|
|
|
$response = call_user_func([(new $callback($request,(new View()))), 'prepare']); |
65
|
|
|
$response->setStatusCode(200); |
66
|
|
|
}else { |
67
|
|
|
throw new PageNotFoundException(); |
68
|
|
|
} |
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|