Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type api/ApiRouter.inc #1411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions SETUP/tests/unittests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function test_get_invalid_round_stats(): void
$this->expectExceptionCode(103);

$path = "v1/stats/site/rounds/P4";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "GET";
$router->route($path, $query_params);
Expand All @@ -128,7 +128,7 @@ public function test_get_invalid_page_data(): void

$project = $this->_create_project();
$path = "v1/projects/$project->projectid/pages/999.png/pagerounds/P1";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "GET";
$router->route($path, $query_params);
Expand All @@ -142,7 +142,7 @@ public function test_get_invalid_pageround_data(): void
$this->add_page($project, "001");
// P0 is not a valid round
$path = "v1/projects/$project->projectid/pages/001.png/pagerounds/P0";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "GET";
$router->route($path, $query_params);
Expand All @@ -153,7 +153,7 @@ public function test_get_valid_pageround_data(): void
$project = $this->_create_project();
$this->add_page($project, "001");
$path = "v1/projects/$project->projectid/pages/001.png/pagerounds/OCR";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "GET";
$result = $router->route($path, $query_params);
Expand All @@ -168,7 +168,7 @@ public function test_create_project_unauthorised(): void
$this->expectExceptionCode(3);

$path = "v1/projects";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "POST";
$router->route($path, $query_params);
Expand All @@ -181,7 +181,7 @@ public function test_create_project_no_data(): void

$pguser = $this->TEST_USERNAME_PM;
$path = "v1/projects";
$query_params = "";
$query_params = [];
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "POST";
$router->route($path, $query_params);
Expand Down
69 changes: 45 additions & 24 deletions api/ApiRouter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@ include_once("exceptions.inc");
// Raise exceptions on assert failures
ini_set("assert.exception", 1);

/**
* We use a trie to match the path to its handler(s).
*
* Handlers contains the individual handlers, keyed by the method.
*/
class TrieNode
{
/** @var array<string, TrieNode> */
public array $children;
/** @var array<string, callable> */
public array $handlers;
}

class ApiRouter
{
private $_url_map = [];
private $_validators = [];
private TrieNode $root;
/** @var array<string, callable> */
private $_validators;

public function add_route($method, $url, $function)
public function __construct()
{
// Confirm the function is defined or raise an assert exception
assert(function_exists($function), "$function not defined");
cpeel marked this conversation as resolved.
Show resolved Hide resolved
$this->root = new TrieNode();
$this->_validators = [];
}

$url_map = &$this->_url_map;
public function add_route(string $method, string $url, callable $function): void
{
$node = $this->root;
$parts = explode("/", $url);
foreach ($parts as $part) {
// If this is a param placeholder, confirm there is a validator
Expand All @@ -25,56 +42,60 @@ class ApiRouter
"No validator specified for $part"
);
}
if (!isset($url_map[$part])) {
$url_map[$part] = [];
if (!isset($node->children[$part])) {
$node->children[$part] = new TrieNode();
}
$url_map = &$url_map[$part];
$node = $node->children[$part];
}
$url_map["endpoint"][$method] = $function;
$node->handlers[$method] = $function;
}

public function route($url, $query_params)
/** @return mixed */
public function route(string $url, array $query_params)
{
$url_map = &$this->_url_map;
$node = $this->root;
$data = [];
$parts = explode("/", $url);
foreach ($parts as $part) {
if (isset($url_map[$part])) {
$url_map = &$url_map[$part];
$next_node = $node->children[$part] ?? null;
if ($next_node) {
$node = $next_node;
} else {
[$param_name, $validator] = $this->get_validator($url_map);
$url_map = &$url_map[$param_name];
[$param_name, $validator] = $this->get_validator($node);
$node = $node->children[$param_name];
$data[$param_name] = $validator($part, $data);
}
}
if (!isset($url_map["endpoint"])) {
if (empty($node->handlers)) {
throw new InvalidAPI();
}
$method = $_SERVER["REQUEST_METHOD"];
if (!isset($url_map["endpoint"][$method])) {
$handler = $node->handlers[$method] ?? null;
if (!$handler) {
throw new MethodNotAllowed();
}
$function = $url_map["endpoint"][$method];
return $function($method, $data, $query_params);
return $handler($method, $data, $query_params);
}

public function add_validator($label, $function)
public function add_validator(string $label, callable $function): void
{
$this->_validators[$label] = $function;
}

private function get_validator($url_map)
/** @return array{0: string, 1: callable} */
private function get_validator(TrieNode $node): array
{
foreach (array_keys($url_map) as $route) {
foreach (array_keys($node->children) as $route) {
if (startswith($route, ":")) {
return [$route, $this->_validators[$route]];
}
}
throw new InvalidAPI();
}

public static function get_router()
public static function get_router(): ApiRouter
{
/** @var ?ApiRouter */
static $router = null;
if (!$router) {
$router = new ApiRouter();
Expand Down