summaryrefslogtreecommitdiff
path: root/src/Router.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Router.php')
-rw-r--r--src/Router.php81
1 files changed, 46 insertions, 35 deletions
diff --git a/src/Router.php b/src/Router.php
index 377c00b..5635d5c 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -7,64 +7,75 @@ use Elements\Controller\Home;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Matcher\UrlMatcher;
+use Symfony\Component\Routing\RequestContext;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
class Router
{
- private $routes = [
- 'GET' => [
- '/' => [Home::class, 'index'],
- ],
-
- 'POST' => [
- '/card/add' => [Card::class, 'add'],
- ],
- ];
+ private ?RouteCollection $routes;
+ private array $routeFunctions = [];
/**
* Router constructor.
*/
public function __construct()
{
+ /**@var Request $request*/
$request = Request::createFromGlobals();
$response = new Response();
- $method = $request->getMethod();
- $path = $request->getPathInfo();
+ $this->routes = new RouteCollection();
+ $this->addRoute('GET', '/', [Home::class, 'index']);
+ $this->addRoute('GET', '/card/{id}', [Card::class, 'get']);
+ $this->addRoute('POST', '/card/add', [Card::class, 'add']);
+
+ $context = new RequestContext();
+ $context->fromRequest($request);
+ $matcher = new UrlMatcher($this->routes, $context);
- // if the route is defined
- if (isset($this->routes[$method][$path])) {
- try {
- // run controller function
- $content = call_user_func($this->routes[$method][$path], $request);
+ try {
+ $match = $matcher->match($request->getPathInfo());
+ $request->attributes->set('route', $match);
- // set response to new response
- if ($content instanceof Response) {
- $response = $content;
- }
+ // run controller function
+ $content = call_user_func($this->routeFunctions[$match['_route']], $request);
- // set content directly otherwise
- else {
- $response->setContent($content);
- }
+ // set response to new response
+ if ($content instanceof Response) {
+ $response = $content;
}
- // catch any errors
- catch (AppException $exception) {
- $response->setStatusCode($exception->getCode());
- $response->setContent($exception->getMessage());
- } catch (\Exception $exception) {
- $response->setStatusCode(Response::HTTP_BAD_REQUEST);
- $response->setContent($exception->getMessage());
+ // set content directly otherwise
+ else {
+ $response->setContent($content);
}
}
- // route is not defined
- else {
- $response->setStatusCode(Response::HTTP_NOT_FOUND);
- $response->setContent('Not Found');
+ // catch any errors
+ catch (AppException $exception) {
+ $response->setStatusCode($exception->getCode());
+ $response->setContent($exception->getMessage());
+ } catch (\Exception $exception) {
+ $response->setStatusCode(Response::HTTP_BAD_REQUEST);
+ $response->setContent($exception->getMessage());
}
$response->send();
}
+
+ /**
+ * @param string|array $methods
+ * @param string $path
+ * @param callable $function
+ */
+ public function addRoute(string|array $methods, string $path, array $function)
+ {
+ $name = "$function[0]::$function[1]";
+
+ $this->routes->add($name, new Route($path, methods: (array)$methods));
+ $this->routeFunctions[$name] = $function;
+ }
}