Skip to content
This repository was archived by the owner on Sep 1, 2025. It is now read-only.

Commit 5f01480

Browse files
committed
NotFoundException and Error reporting refactoring in Router::run
1 parent 9b8cd0a commit 5f01480

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

src/Exceptions/MethodNotSupportedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct($method, $supportedMethods = [])
1414

1515
public function __toString(): string
1616
{
17-
return "MethodNotSupportedException" . $this->message;
17+
return "MethodNotSupportedException: " . $this->message;
1818
}
1919

2020
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Mrfoo\PHPRouter\Exceptions;
4+
5+
use Exception;
6+
7+
class NotFoundException extends Exception {
8+
9+
public function __construct($uri)
10+
{
11+
$this->message = "route " . $uri . " doesn't exists.";
12+
}
13+
14+
public function __toString(): string
15+
{
16+
return "MethodNotSupportedException: " . $this->message;
17+
}
18+
19+
}

src/Router.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Mrfoo\PHPRouter\Exceptions\MethodNotSupportedException;
99
use Exception;
1010
use Mrfoo\PHPRouter\Core\HashTable;
11+
use Mrfoo\PHPRouter\Exceptions\NotFoundException;
1112

1213
class Router
1314
{
1415
public static HashTable $routeList;
1516
public static ?HashTable $tmpGroup;
1617
public static bool $isTrackingGroup = false;
18+
public static Router $instance;
1719

1820
public static function get($uri, $handler)
1921
{
@@ -55,6 +57,7 @@ public static function init()
5557
if (!isset(self::$routeList)) {
5658
self::$routeList = new HashTable();
5759
self::$tmpGroup = new HashTable();
60+
self::$instance = new Router();
5861
}
5962
}
6063

@@ -69,18 +72,10 @@ public static function run()
6972
$route->handle();
7073
$route->postHandleMiddlewares();
7174
} else {
72-
// shoud be moved to another method ie: performTests($route);
73-
if ($route && $route->getMethod() != $user_method) {
74-
try {
75-
throw new MethodNotSupportedException($user_method, [$route->getMethod()]);
76-
} catch (Exception $e) {
77-
print($e->getMessage());
78-
return false;
79-
}
80-
} else {
81-
// TODO: NotFoundException
82-
header("HTTP/1.0 404 Not Found");
83-
echo "404 Not Found";
75+
try {
76+
self::$instance->performTests($route, $user_uri, $user_method);
77+
} catch (Exception $e) {
78+
print($e->getMessage());
8479
return false;
8580
}
8681
}
@@ -111,6 +106,17 @@ public static function buildRoute($uri, $handler, $method)
111106
return $route;
112107
}
113108

109+
private function performTests($route, $uri, $user_method)
110+
{
111+
// shoud be moved to another method ie: performTests($route);
112+
if ($route == null) {
113+
return throw new NotFoundException($uri);
114+
}
115+
if ($route->getMethod() != $user_method) {
116+
return throw new MethodNotSupportedException($user_method, [$route->getMethod()]);
117+
}
118+
}
119+
114120
public static function group(array $options, $callback): void
115121
{
116122
self::$isTrackingGroup = true;
@@ -172,12 +178,12 @@ public static function generateURL($routeName, ...$params): string
172178
$parameters = $route->getURI()->getParameters();
173179

174180
if (count($params) == count($parameters)) {
175-
181+
176182
// construct the URL string
177183
foreach ($parameters as $key => $p) {
178184
$parameters[$key] = $params[array_search($key, array_keys($parameters))];
179185
}
180-
186+
181187
$base = getBaseUrl();
182188
$uri = [];
183189
$nextParam = 0;
@@ -187,16 +193,14 @@ public static function generateURL($routeName, ...$params): string
187193
$uri[$i] = $parameters[array_keys($parameters)[$nextParam]];
188194
$nextParam++;
189195
} else {
190-
$uri[$i] = $seg;
196+
$uri[$i] = $seg;
191197
}
192198
}
193199

194200
return $base . implode("/", $uri);
195-
196201
} else {
197202
return die("Parameters provided for route '$routeName' do not match parameters count");
198203
}
199-
200204
} else {
201205
return die("No route with name '$routeName'");
202206
}

0 commit comments

Comments
 (0)