@@ -36,8 +36,8 @@ discard block |
||
| 36 | 36 | protected static $currentRoute = null; |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | - * @var bool |
|
| 40 | - */ |
|
| 39 | + * @var bool |
|
| 40 | + */ |
|
| 41 | 41 | public $csrfVerification = true; |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -75,12 +75,12 @@ discard block |
||
| 75 | 75 | return static::$routes; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Handles the missing methods of the controller |
|
| 80 | - * @param string $method |
|
| 81 | - * @param array $arguments |
|
| 82 | - * @throws ControllerException |
|
| 83 | - */ |
|
| 78 | + /** |
|
| 79 | + * Handles the missing methods of the controller |
|
| 80 | + * @param string $method |
|
| 81 | + * @param array $arguments |
|
| 82 | + * @throws ControllerException |
|
| 83 | + */ |
|
| 84 | 84 | public function __call(string $method, array $arguments) |
| 85 | 85 | { |
| 86 | 86 | throw ControllerException::undefinedMethod($method); |
@@ -12,95 +12,95 @@ |
||
| 12 | 12 | * @since 2.9.5 |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | - namespace Quantum\Mvc; |
|
| 15 | + namespace Quantum\Mvc; |
|
| 16 | 16 | |
| 17 | - use Quantum\Handlers\ViewCacheHandler; |
|
| 18 | - use Quantum\Http\Request; |
|
| 19 | - use Quantum\Http\Response; |
|
| 20 | - use Quantum\Middleware\MiddlewareExecutor; |
|
| 21 | - use Quantum\Libraries\Csrf\Csrf; |
|
| 22 | - use Quantum\Loader\Loader; |
|
| 23 | - use Quantum\Di\Di; |
|
| 24 | - use Quantum\Exceptions\ControllerException; |
|
| 25 | - use Quantum\Di\Exceptions\DiException; |
|
| 26 | - use ReflectionException; |
|
| 27 | - use Quantum\Router\RouteController; |
|
| 28 | - |
|
| 29 | - class RouteDispatcher |
|
| 30 | - { |
|
| 31 | - public static function handle(Request $request, Response $response): void |
|
| 32 | - { |
|
| 33 | - // 1. Apply middleware |
|
| 34 | - [$request, $response] = (new MiddlewareExecutor())->execute($request, $response); |
|
| 35 | - |
|
| 36 | - // 2. Try serving from view cache |
|
| 37 | - $viewCacheHandler = new ViewCacheHandler(); |
|
| 38 | - if ($viewCacheHandler->serveCachedView(route_uri(), $response)) { |
|
| 39 | - return; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - // 3. Route callback or controller handling |
|
| 43 | - $callback = route_callback(); |
|
| 44 | - |
|
| 45 | - if ($callback) { |
|
| 46 | - call_user_func_array($callback, self::getArgs($callback)); |
|
| 47 | - } else { |
|
| 48 | - $controller = self::getController(); |
|
| 49 | - $action = self::getAction($controller); |
|
| 50 | - |
|
| 51 | - if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 52 | - csrf()->checkToken($request); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - if (method_exists($controller, '__before')) { |
|
| 56 | - call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 60 | - |
|
| 61 | - if (method_exists($controller, '__after')) { |
|
| 62 | - call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - private static function getController(): RouteController |
|
| 68 | - { |
|
| 69 | - $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 70 | - |
|
| 71 | - $loader = Di::get(Loader::class); |
|
| 72 | - |
|
| 73 | - return $loader->loadClassFromFile( |
|
| 74 | - $controllerPath, |
|
| 75 | - function () { |
|
| 76 | - return ControllerException::controllerNotFound(current_controller()); |
|
| 77 | - }, |
|
| 78 | - function () { |
|
| 79 | - return ControllerException::controllerNotDefined(current_controller()); |
|
| 80 | - } |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - private static function getAction(RouteController $controller): ?string |
|
| 85 | - { |
|
| 86 | - $action = current_action(); |
|
| 87 | - |
|
| 88 | - if ($action && !method_exists($controller, $action)) { |
|
| 89 | - throw ControllerException::actionNotDefined($action); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return $action; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - private static function getArgs(callable $callable): array |
|
| 96 | - { |
|
| 97 | - return Di::autowire($callable, self::routeParams()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - private static function routeParams(): array |
|
| 101 | - { |
|
| 102 | - return array_map(function ($param) { |
|
| 103 | - return $param['value']; |
|
| 104 | - }, route_params()); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | 17 | \ No newline at end of file |
| 18 | + use Quantum\Handlers\ViewCacheHandler; |
|
| 19 | + use Quantum\Http\Request; |
|
| 20 | + use Quantum\Http\Response; |
|
| 21 | + use Quantum\Middleware\MiddlewareExecutor; |
|
| 22 | + use Quantum\Libraries\Csrf\Csrf; |
|
| 23 | + use Quantum\Loader\Loader; |
|
| 24 | + use Quantum\Di\Di; |
|
| 25 | + use Quantum\Exceptions\ControllerException; |
|
| 26 | + use Quantum\Di\Exceptions\DiException; |
|
| 27 | + use ReflectionException; |
|
| 28 | + use Quantum\Router\RouteController; |
|
| 29 | + |
|
| 30 | + class RouteDispatcher |
|
| 31 | + { |
|
| 32 | + public static function handle(Request $request, Response $response): void |
|
| 33 | + { |
|
| 34 | + // 1. Apply middleware |
|
| 35 | + [$request, $response] = (new MiddlewareExecutor())->execute($request, $response); |
|
| 36 | + |
|
| 37 | + // 2. Try serving from view cache |
|
| 38 | + $viewCacheHandler = new ViewCacheHandler(); |
|
| 39 | + if ($viewCacheHandler->serveCachedView(route_uri(), $response)) { |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + // 3. Route callback or controller handling |
|
| 44 | + $callback = route_callback(); |
|
| 45 | + |
|
| 46 | + if ($callback) { |
|
| 47 | + call_user_func_array($callback, self::getArgs($callback)); |
|
| 48 | + } else { |
|
| 49 | + $controller = self::getController(); |
|
| 50 | + $action = self::getAction($controller); |
|
| 51 | + |
|
| 52 | + if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 53 | + csrf()->checkToken($request); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + if (method_exists($controller, '__before')) { |
|
| 57 | + call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 61 | + |
|
| 62 | + if (method_exists($controller, '__after')) { |
|
| 63 | + call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + private static function getController(): RouteController |
|
| 69 | + { |
|
| 70 | + $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 71 | + |
|
| 72 | + $loader = Di::get(Loader::class); |
|
| 73 | + |
|
| 74 | + return $loader->loadClassFromFile( |
|
| 75 | + $controllerPath, |
|
| 76 | + function () { |
|
| 77 | + return ControllerException::controllerNotFound(current_controller()); |
|
| 78 | + }, |
|
| 79 | + function () { |
|
| 80 | + return ControllerException::controllerNotDefined(current_controller()); |
|
| 81 | + } |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + private static function getAction(RouteController $controller): ?string |
|
| 86 | + { |
|
| 87 | + $action = current_action(); |
|
| 88 | + |
|
| 89 | + if ($action && !method_exists($controller, $action)) { |
|
| 90 | + throw ControllerException::actionNotDefined($action); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return $action; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + private static function getArgs(callable $callable): array |
|
| 97 | + { |
|
| 98 | + return Di::autowire($callable, self::routeParams()); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + private static function routeParams(): array |
|
| 102 | + { |
|
| 103 | + return array_map(function ($param) { |
|
| 104 | + return $param['value']; |
|
| 105 | + }, route_params()); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | 108 | \ No newline at end of file |
@@ -72,10 +72,10 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | return $loader->loadClassFromFile( |
| 74 | 74 | $controllerPath, |
| 75 | - function () { |
|
| 75 | + function() { |
|
| 76 | 76 | return ControllerException::controllerNotFound(current_controller()); |
| 77 | 77 | }, |
| 78 | - function () { |
|
| 78 | + function() { |
|
| 79 | 79 | return ControllerException::controllerNotDefined(current_controller()); |
| 80 | 80 | } |
| 81 | 81 | ); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | private static function routeParams(): array |
| 101 | 101 | { |
| 102 | - return array_map(function ($param) { |
|
| 102 | + return array_map(function($param) { |
|
| 103 | 103 | return $param['value']; |
| 104 | 104 | }, route_params()); |
| 105 | 105 | } |