Skip to content

Commit 91ceaf6

Browse files
authored
Merge pull request #338 from armanist/336--Refactor-RouteDispatcher-and-MiddlewareManager-to-rely-on-PSR-4-autoloading
Refactor `RouteDispatcher` and `MiddlewareManager` to rely on PSR-4 autoloading
2 parents 04b6934 + 4d57786 commit 91ceaf6

File tree

6 files changed

+23
-50
lines changed

6 files changed

+23
-50
lines changed

src/Middleware/Exceptions/MiddlewareException.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.9.7
12+
* @since 2.9.9
1313
*/
1414

1515
namespace Quantum\Middleware\Exceptions;
@@ -22,21 +22,13 @@
2222
*/
2323
class MiddlewareException extends BaseException
2424
{
25-
/**
26-
* @param string $name
27-
* @return MiddlewareException
28-
*/
29-
public static function notDefined(string $name): MiddlewareException
30-
{
31-
return new static(t('exception.middleware_not_defined', $name), E_WARNING);
32-
}
3325

3426
/**
3527
* @param string $name
3628
* @return MiddlewareException
3729
*/
3830
public static function middlewareNotFound(string $name): MiddlewareException
3931
{
40-
return new static(t('exception.middleware_not_found', $name), E_WARNING);
32+
return new static("Middleware class `$name` not found.", E_ERROR);
4133
}
4234
}

src/Middleware/MiddlewareManager.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.9.7
12+
* @since 2.9.9
1313
*/
1414

1515
namespace Quantum\Middleware;
1616

17+
use Quantum\Middleware\Exceptions\MiddlewareException;
1718
use Quantum\Http\Response;
1819
use Quantum\Http\Request;
1920

@@ -50,6 +51,7 @@ public function __construct()
5051
* @param Request $request
5152
* @param Response $response
5253
* @return array
54+
* @throws MiddlewareException
5355
*/
5456
public function applyMiddlewares(Request $request, Response $response): array
5557
{
@@ -72,16 +74,15 @@ public function applyMiddlewares(Request $request, Response $response): array
7274
* @param Request $request
7375
* @param Response $response
7476
* @return QtMiddleware
77+
* @throws MiddlewareException
7578
*/
7679
private function getMiddleware(Request $request, Response $response): QtMiddleware
7780
{
78-
$middlewareName = current($this->middlewares);
81+
$middlewareClass = module_base_namespace() . '\\' . $this->module . '\\Middlewares\\' . current($this->middlewares);
7982

80-
$middlewarePath = modules_dir() . DS . $this->module . DS . 'Middlewares' . DS . $middlewareName . '.php';
81-
82-
require_once $middlewarePath;
83-
84-
$middlewareClass = module_base_namespace() . '\\' . $this->module . '\\Middlewares\\' . $middlewareName;
83+
if (!class_exists($middlewareClass)) {
84+
throw MiddlewareException::middlewareNotFound($middlewareClass);
85+
}
8586

8687
return new $middlewareClass($request, $response);
8788
}

src/Router/Exceptions/RouteControllerException.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.9.7
12+
* @since 2.9.9
1313
*/
1414

1515
namespace Quantum\Router\Exceptions;
@@ -22,22 +22,14 @@
2222
*/
2323
class RouteControllerException extends BaseException
2424
{
25-
/**
26-
* @param string|null $name
27-
* @return RouteControllerException
28-
*/
29-
public static function controllerNotFound(?string $name): RouteControllerException
30-
{
31-
return new static(t('exception.controller_not_found', $name), E_ERROR);
32-
}
3325

3426
/**
3527
* @param string|null $name
3628
* @return RouteControllerException
3729
*/
3830
public static function controllerNotDefined(?string $name): RouteControllerException
3931
{
40-
return new static(t('exception.controller_not_defined', $name), E_ERROR);
32+
return new static("Controller class `$name` not found.", E_ERROR);
4133
}
4234

4335
/**
@@ -46,15 +38,6 @@ public static function controllerNotDefined(?string $name): RouteControllerExcep
4638
*/
4739
public static function actionNotDefined(string $name): RouteControllerException
4840
{
49-
return new static(t('exception.action_not_defined', $name), E_ERROR);
50-
}
51-
52-
/**
53-
* @param string $name
54-
* @return RouteControllerException
55-
*/
56-
public static function undefinedMethod(string $name): RouteControllerException
57-
{
58-
return new static(t('exception.undefined_method', $name), E_ERROR);
41+
return new static("Action `$name` not defined", E_ERROR);
5942
}
6043
}

src/Router/RouteController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.9.7
12+
* @since 2.9.9
1313
*/
1414

1515
namespace Quantum\Router;
@@ -83,6 +83,6 @@ public static function getRoutes(): array
8383
*/
8484
public function __call(string $method, array $arguments)
8585
{
86-
throw RouteControllerException::undefinedMethod($method);
86+
throw RouteControllerException::actionNotDefined($method);
8787
}
8888
}

src/Router/RouteDispatcher.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.9.7
12+
* @since 2.9.9
1313
*/
1414

1515
namespace Quantum\Router;
@@ -56,18 +56,15 @@ public static function handle(Request $request): void
5656
/**
5757
* Loads and gets the current route's controller instance.
5858
* @return RouteController
59-
* @throws DiException
60-
* @throws ReflectionException
59+
* @throws RouteControllerException
6160
*/
6261
private static function resolveController(): RouteController
6362
{
64-
$controllerName = current_controller();
65-
$moduleName = current_module();
63+
$controllerClass = module_base_namespace() . '\\' . current_module() . '\\Controllers\\' . current_controller();
6664

67-
$controllerPath = modules_dir() . DS . $moduleName . DS . 'Controllers' . DS . $controllerName . '.php';
68-
$controllerClass = module_base_namespace() . '\\' . $moduleName . '\\Controllers\\' . $controllerName;
69-
70-
require_once $controllerPath;
65+
if (!class_exists($controllerClass)) {
66+
throw RouteControllerException::controllerNotDefined($controllerClass);
67+
}
7168

7269
return new $controllerClass();
7370
}

tests/Unit/Router/RouteControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function testMissingMethods()
3232
{
3333
$this->expectException(RouteControllerException::class);
3434

35-
$this->expectExceptionMessage('undefined_method');
35+
$this->expectExceptionMessage('Action `undefinedAction` not defined');
3636

3737
$controller = new SomeController();
3838

39-
$controller->undefinedMethod();
39+
$controller->undefinedAction();
4040
}
4141

4242
}

0 commit comments

Comments
 (0)