diff --git a/src/Middleware/Exceptions/MiddlewareException.php b/src/Middleware/Exceptions/MiddlewareException.php index 77798632..0fe1695e 100644 --- a/src/Middleware/Exceptions/MiddlewareException.php +++ b/src/Middleware/Exceptions/MiddlewareException.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Middleware\Exceptions; @@ -22,14 +22,6 @@ */ class MiddlewareException extends BaseException { - /** - * @param string $name - * @return MiddlewareException - */ - public static function notDefined(string $name): MiddlewareException - { - return new static(t('exception.middleware_not_defined', $name), E_WARNING); - } /** * @param string $name @@ -37,6 +29,6 @@ public static function notDefined(string $name): MiddlewareException */ public static function middlewareNotFound(string $name): MiddlewareException { - return new static(t('exception.middleware_not_found', $name), E_WARNING); + return new static("Middleware class `$name` not found.", E_ERROR); } } \ No newline at end of file diff --git a/src/Middleware/MiddlewareManager.php b/src/Middleware/MiddlewareManager.php index a3539a21..b622d0f8 100644 --- a/src/Middleware/MiddlewareManager.php +++ b/src/Middleware/MiddlewareManager.php @@ -9,11 +9,12 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Middleware; +use Quantum\Middleware\Exceptions\MiddlewareException; use Quantum\Http\Response; use Quantum\Http\Request; @@ -50,6 +51,7 @@ public function __construct() * @param Request $request * @param Response $response * @return array + * @throws MiddlewareException */ public function applyMiddlewares(Request $request, Response $response): array { @@ -72,16 +74,15 @@ public function applyMiddlewares(Request $request, Response $response): array * @param Request $request * @param Response $response * @return QtMiddleware + * @throws MiddlewareException */ private function getMiddleware(Request $request, Response $response): QtMiddleware { - $middlewareName = current($this->middlewares); + $middlewareClass = module_base_namespace() . '\\' . $this->module . '\\Middlewares\\' . current($this->middlewares); - $middlewarePath = modules_dir() . DS . $this->module . DS . 'Middlewares' . DS . $middlewareName . '.php'; - - require_once $middlewarePath; - - $middlewareClass = module_base_namespace() . '\\' . $this->module . '\\Middlewares\\' . $middlewareName; + if (!class_exists($middlewareClass)) { + throw MiddlewareException::middlewareNotFound($middlewareClass); + } return new $middlewareClass($request, $response); } diff --git a/src/Router/Exceptions/RouteControllerException.php b/src/Router/Exceptions/RouteControllerException.php index d48009a6..2da16ad0 100644 --- a/src/Router/Exceptions/RouteControllerException.php +++ b/src/Router/Exceptions/RouteControllerException.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Router\Exceptions; @@ -22,14 +22,6 @@ */ class RouteControllerException extends BaseException { - /** - * @param string|null $name - * @return RouteControllerException - */ - public static function controllerNotFound(?string $name): RouteControllerException - { - return new static(t('exception.controller_not_found', $name), E_ERROR); - } /** * @param string|null $name @@ -37,7 +29,7 @@ public static function controllerNotFound(?string $name): RouteControllerExcepti */ public static function controllerNotDefined(?string $name): RouteControllerException { - return new static(t('exception.controller_not_defined', $name), E_ERROR); + return new static("Controller class `$name` not found.", E_ERROR); } /** @@ -46,15 +38,6 @@ public static function controllerNotDefined(?string $name): RouteControllerExcep */ public static function actionNotDefined(string $name): RouteControllerException { - return new static(t('exception.action_not_defined', $name), E_ERROR); - } - - /** - * @param string $name - * @return RouteControllerException - */ - public static function undefinedMethod(string $name): RouteControllerException - { - return new static(t('exception.undefined_method', $name), E_ERROR); + return new static("Action `$name` not defined", E_ERROR); } } \ No newline at end of file diff --git a/src/Router/RouteController.php b/src/Router/RouteController.php index 078aa4a1..bb966234 100644 --- a/src/Router/RouteController.php +++ b/src/Router/RouteController.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Router; @@ -83,6 +83,6 @@ public static function getRoutes(): array */ public function __call(string $method, array $arguments) { - throw RouteControllerException::undefinedMethod($method); + throw RouteControllerException::actionNotDefined($method); } } diff --git a/src/Router/RouteDispatcher.php b/src/Router/RouteDispatcher.php index c9075859..cad4f67b 100644 --- a/src/Router/RouteDispatcher.php +++ b/src/Router/RouteDispatcher.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Router; @@ -56,18 +56,15 @@ public static function handle(Request $request): void /** * Loads and gets the current route's controller instance. * @return RouteController - * @throws DiException - * @throws ReflectionException + * @throws RouteControllerException */ private static function resolveController(): RouteController { - $controllerName = current_controller(); - $moduleName = current_module(); + $controllerClass = module_base_namespace() . '\\' . current_module() . '\\Controllers\\' . current_controller(); - $controllerPath = modules_dir() . DS . $moduleName . DS . 'Controllers' . DS . $controllerName . '.php'; - $controllerClass = module_base_namespace() . '\\' . $moduleName . '\\Controllers\\' . $controllerName; - - require_once $controllerPath; + if (!class_exists($controllerClass)) { + throw RouteControllerException::controllerNotDefined($controllerClass); + } return new $controllerClass(); } diff --git a/tests/Unit/Router/RouteControllerTest.php b/tests/Unit/Router/RouteControllerTest.php index 0be08e73..1c30805d 100644 --- a/tests/Unit/Router/RouteControllerTest.php +++ b/tests/Unit/Router/RouteControllerTest.php @@ -32,11 +32,11 @@ public function testMissingMethods() { $this->expectException(RouteControllerException::class); - $this->expectExceptionMessage('undefined_method'); + $this->expectExceptionMessage('Action `undefinedAction` not defined'); $controller = new SomeController(); - $controller->undefinedMethod(); + $controller->undefinedAction(); } }