Skip to content

Commit 9b73c4b

Browse files
committed
Improve Route
1 parent 9390b20 commit 9b73c4b

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

app/Route/Router.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
namespace App\Route;
55

66

7+
use InvalidArgumentException;
8+
use ReflectionException;
79
use ReflectionMethod;
810

911
class Router implements RouterInterface
1012
{
13+
private const NOT_FOUND_CODE = 204;
1114
private array $routes = [];
1215

1316
public function dispatch(string $uri, array $params = []): void
@@ -18,6 +21,8 @@ public function dispatch(string $uri, array $params = []): void
1821

1922
$reflectedControllerMethod->invokeArgs(new $uriContent['namespace'], $params);
2023
}
24+
25+
throw new InvalidArgumentException("'{$uri}' is an unregistered route", self::NOT_FOUND_CODE);
2126
}
2227

2328
public function registry(string $uri, string $controller, string $method): void
@@ -32,7 +37,7 @@ private static function createReflectionMethod(array $uriContent): ReflectionMet
3237
{
3338
try {
3439
return new ReflectionMethod($uriContent['namespace'], $uriContent['method']);
35-
} catch (\ReflectionException $exception) {
40+
} catch (ReflectionException $exception) {
3641
print $exception->getMessage();
3742
}
3843
}

app/Route/RouterInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace App\Route;
55

66

7-
use App\Base\ControllerInterface;
8-
97
interface RouterInterface
108
{
119
public function registry(string $uri, string $controller, string $method): void;

tests/Route/RouterTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Test\Route;
4+
5+
use App\Route\Router;
6+
7+
class RouterTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
}

0 commit comments

Comments
 (0)