Routing test 1

This commit is contained in:
2020-06-19 14:12:07 +02:00
parent 077fe68914
commit 78c5934480
10 changed files with 136 additions and 23 deletions

View File

@@ -7,8 +7,6 @@ use \Driver\SQL\Condition\Compare;
class Fetch extends Request {
private array $notifications;
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array());
$this->loginRequired = true;

View File

@@ -0,0 +1,61 @@
<?php
namespace Api\Routes;
use Api\Parameter\StringType;
use \Api\Request;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\Regex;
class Find extends Request {
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array(
'request' => new StringType('request', 128, true, '/')
));
$this->isPublic = false;
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
$request = $this->getParam('request');
if (!startsWith($request, '/')) {
$request = "/$request";
}
$sql = $this->user->getSQL();
$res = $sql
->select("uid", "request", "action", "target", "extra")
->from("Route")
->where(new CondBool("active"))
->where(new Regex("^$request$", new Column("request")))
->limit(1)
->execute();
$this->lastError = $sql->getLastError();
$this->success = ($res !== FALSE);
if ($this->success) {
if (!empty($res)) {
$row = $res[0];
$this->result["route"] = array(
"uid" => intval($row["uid"]),
"request" => $row["request"],
"action" => $row["action"],
"target" => $row["target"],
"extra" => $row["extra"]
);
} else {
$this->result["route"] = NULL;
}
}
return $this->success;
}
}

View File

@@ -129,7 +129,8 @@ class CreateDatabase {
->primaryKey("uid");
$queries[] = $sql->insert("Route", array("request", "action", "target"))
->addRow("/admin(/.*)?", "dynamic", "\\Core\\Documents\\AdminDashboard");
->addRow("/admin(/.*)?", "dynamic", "\\Core\\Documents\\AdminDashboard")
->addRow("/api/(.*)", "dynamic", "\\Core\\Api\\$1");
return $queries;
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Driver\SQL\Condition;
class Regex extends Condition {
private $leftExpression;
private $rightExpression;
public function __construct($leftExpression, $rightExpression) {
$this->leftExpression = $leftExpression;
$this->rightExpression = $rightExpression;
}
public function getLeftExp() { return $this->leftExpression; }
public function getRightExp() { return $this->rightExpression; }
}

View File

@@ -6,6 +6,7 @@ use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\Condition\Regex;
use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey;
@@ -326,11 +327,17 @@ abstract class SQL {
return $this->buildCondition($condition[0], $params);
} else {
$conditions = array();
foreach($condition as $cond) {
foreach ($condition as $cond) {
$conditions[] = $this->buildCondition($cond, $params);
}
return implode(" AND ", $conditions);
}
} else if ($condition instanceof Regex) {
$left = $condition->getLeftExp();
$right = $condition->getRightExp();
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);
$right = ($right instanceof Column) ? $this->columnName($right->getName()) : $this->addValue($right, $params);
return $left . " REGEXP " . $right;
} else {
$this->lastError = "Unsupported condition type: " . get_class($condition);
return false;

View File

@@ -85,6 +85,7 @@
function getClassPath($class, $suffix=true) {
$path = str_replace('\\', '/', $class);
if (startsWith($path, "/")) $path = substr($path, 1);
$suffix = ($suffix ? ".class" : "");
return "core/$path$suffix.php";
}