1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Mvc; |
16
|
|
|
|
17
|
|
|
use Quantum\Exceptions\ControllerException; |
18
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
19
|
|
|
use Quantum\Middleware\MiddlewareManager; |
20
|
|
|
use Quantum\Libraries\Csrf\Csrf; |
21
|
|
|
use Quantum\Http\Response; |
22
|
|
|
use Quantum\Http\Request; |
23
|
|
|
use Quantum\Di\Di; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class MvcManager |
27
|
|
|
* @package Quantum\Mvc |
28
|
|
|
*/ |
29
|
|
|
class MvcManager |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handles the request |
34
|
|
|
* @param Request $request |
35
|
|
|
* @param Response $response |
36
|
|
|
* @throws ControllerException |
37
|
|
|
* @throws \Quantum\Exceptions\MiddlewareException |
38
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
39
|
|
|
* @throws \Quantum\Exceptions\SessionException |
40
|
|
|
* @throws \Quantum\Exceptions\ConfigException |
41
|
|
|
* @throws \Quantum\Exceptions\CsrfException |
42
|
|
|
* @throws \Quantum\Exceptions\DiException |
43
|
|
|
* @throws \ReflectionException |
44
|
|
|
*/ |
45
|
|
|
public static function handle(Request $request, Response $response) |
46
|
|
|
{ |
47
|
|
|
if (current_middlewares()) { |
48
|
|
|
list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$callback = route_callback(); |
52
|
|
|
|
53
|
|
|
if ($callback) { |
54
|
|
|
call_user_func_array($callback, self::getArgs($callback)); |
55
|
|
|
} else { |
56
|
|
|
$controller = self::getController(); |
57
|
|
|
$action = self::getAction($controller); |
58
|
|
|
|
59
|
|
|
if ($controller->csrfVerification) { |
60
|
|
|
Csrf::checkToken($request, session()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (method_exists($controller, '__before')) { |
64
|
|
|
call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
68
|
|
|
|
69
|
|
|
if (method_exists($controller, '__after')) { |
70
|
|
|
call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Controller |
77
|
|
|
* @return QtController |
78
|
|
|
* @throws ControllerException |
79
|
|
|
* @throws \Quantum\Exceptions\DiException |
80
|
|
|
* @throws \ReflectionException |
81
|
|
|
*/ |
82
|
|
|
private static function getController(): QtController |
83
|
|
|
{ |
84
|
|
|
$fs = Di::get(FileSystem::class); |
85
|
|
|
|
86
|
|
|
$controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
87
|
|
|
|
88
|
|
|
if (!$fs->exists($controllerPath)) { |
89
|
|
|
throw ControllerException::controllerNotFound(current_controller()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
require_once $controllerPath; |
93
|
|
|
|
94
|
|
|
$controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
95
|
|
|
|
96
|
|
|
if (!class_exists($controllerClass, false)) { |
97
|
|
|
throw ControllerException::controllerNotDefined(current_controller()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new $controllerClass(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Action |
105
|
|
|
* @param QtController $controller |
106
|
|
|
* @return string|null |
107
|
|
|
* @throws ControllerException |
108
|
|
|
*/ |
109
|
|
|
private static function getAction(QtController $controller): ?string |
110
|
|
|
{ |
111
|
|
|
$action = current_action(); |
112
|
|
|
|
113
|
|
|
if ($action && !method_exists($controller, $action)) { |
114
|
|
|
throw ControllerException::actionNotDefined($action); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $action; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get arguments |
122
|
|
|
* @param callable $callable |
123
|
|
|
* @return array |
124
|
|
|
* @throws \Quantum\Exceptions\DiException |
125
|
|
|
* @throws \ReflectionException |
126
|
|
|
*/ |
127
|
|
|
private static function getArgs(callable $callable): array |
128
|
|
|
{ |
129
|
|
|
return Di::autowire($callable, self::routeParams()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets the route parameters |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
private static function routeParams(): array |
137
|
|
|
{ |
138
|
|
|
return array_map(function ($param) { |
139
|
|
|
return $param['value']; |
140
|
|
|
}, route_params()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|