Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/Middleware/Exceptions/MiddlewareException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand All @@ -22,21 +22,13 @@
*/
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
* @return 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);
}
}
15 changes: 8 additions & 7 deletions src/Middleware/MiddlewareManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;

Expand Down Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
23 changes: 3 additions & 20 deletions src/Router/Exceptions/RouteControllerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand All @@ -22,22 +22,14 @@
*/
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
* @return RouteControllerException
*/
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);
}

/**
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Router/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand Down Expand Up @@ -83,6 +83,6 @@ public static function getRoutes(): array
*/
public function __call(string $method, array $arguments)
{
throw RouteControllerException::undefinedMethod($method);
throw RouteControllerException::actionNotDefined($method);
}
}
15 changes: 6 additions & 9 deletions src/Router/RouteDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Router/RouteControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Expand Down