diff --git a/controllers/ipaddress/GET.php b/controllers/ipaddress/GET.php index 83a0d56..ca9e115 100644 --- a/controllers/ipaddress/GET.php +++ b/controllers/ipaddress/GET.php @@ -1,7 +1,7 @@ $_SERVER['REMOTE_ADDR'], - ]); + ]; }; diff --git a/controllers/punycode/GET.php b/controllers/punycode/GET.php index f705065..2b728de 100644 --- a/controllers/punycode/GET.php +++ b/controllers/punycode/GET.php @@ -1,12 +1,14 @@ "Hello World!", - ]); + ]; }; diff --git a/controllers/whois/GET.php b/controllers/whois/GET.php index d8e8506..d5fecc5 100644 --- a/controllers/whois/GET.php +++ b/controllers/whois/GET.php @@ -94,25 +94,22 @@ function whoisDomain(string $domain) { } return function (array $context) { - $result = null; - if (key_exists("ip", $_GET)) { $ip = $_GET["ip"]; + if (!filter_var($ip, FILTER_VALIDATE_IP)) { setStatusCode(400); - $result = errorResponse("Invalid IP address", "Please provide a valid IP address."); + return errorResponse("Invalid IP address", "Please provide a valid IP address."); } else { - $result = whoisIp($ip); + return whoisIp($ip); } } elseif (key_exists("domain", $_GET)) { $domain = $_GET["domain"]; - $result = whoisDomain($domain); + return whoisDomain($domain); } else { setStatusCode(400); - $result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip, domain"); + return errorResponse("Unknown mode", "Please specify one of the following query parameters: ip, domain"); } - - $context["renderer"]($result); }; diff --git a/middleware/renderer.php b/middleware/renderer.php index c5667c0..240d7c3 100644 --- a/middleware/renderer.php +++ b/middleware/renderer.php @@ -6,8 +6,14 @@ function useRenderer($handler, string $default = "JSON", string $query_param = " return function (array $context) use ($handler, $default, $query_param) { $rendererMap = require(ROOT . "/renderer/renderer.php"); - $context["renderer"] = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default]; + $renderer = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default]; + $context["renderer"] = $renderer; - $handler($context); + $result = $handler($context); + if ($result !== null) { + $renderer($result); + } + + return $result; }; } \ No newline at end of file diff --git a/router/Router.php b/router/Router.php index 7456de6..be22688 100644 --- a/router/Router.php +++ b/router/Router.php @@ -52,9 +52,7 @@ class Router { array_get_or_add($method, $this->routes, [])[$path] = $handler; } - public function execute() { - //var_dump($this->routes); - + public function execute($context = []) { $path = $this->getPath($_SERVER["REQUEST_URI"]); $route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path); @@ -62,11 +60,14 @@ class Router { $route = $this->notFoundHandler; } - $context = [ - "REQUEST_PATH" => $path, - ]; + $context["REQUEST_PATH"] = $path; - $route($context); + return $route($context); + } + + // calling magic to make the router a handler and thus cascade-able + public function __call(string $name, array $arguments) { + return $this->execute($arguments[0] ?? []); } }