Skip to content

Commit

Permalink
Random things
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-schindler committed Oct 27, 2022
1 parent a394986 commit 733ae00
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Backend/Core/Data/InsertQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function add(string $columnName, string $value): void {
* @return int|string Last insert ID or 0 on failure
*/
public function run(?string $idName = null): int|string {
$this->queryStr .= ");";
$this->queryStr .= ');';
return $this->lastInsertId($idName);
}
}
12 changes: 5 additions & 7 deletions Backend/Core/Data/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public function getID(): int {
*/
protected function setField(string $field, string $value): bool {
return (new Query(
"UPDATE `{$this->tableName}` SET `{$field}`=:value WHERE `ID`=:id;",
[
":value" => $value,
":id" => $this->id
"UPDATE `{$this->tableName}` SET `{$field}`=:value WHERE `ID`=:id;", [
':value' => $value,
':id' => $this->id
]
))->success();
}
Expand All @@ -59,9 +58,8 @@ protected function setField(string $field, string $value): bool {
*/
protected function getField(string $field): ?string {
return (new Query(
"SELECT `{$field}` FROM `{$this->tableName}` WHERE `ID`=:id;",
[
":id" => $this->id
"SELECT `{$field}` FROM `{$this->tableName}` WHERE `ID`=:id;", [
':id' => $this->id
]
))->fetch()[$field];
}
Expand Down
2 changes: 1 addition & 1 deletion Backend/Core/Data/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function lastInsertId(string $name = null): int|string {

/**
* Reads data (row by row) from database
* Access returned value via $return["ColumnName"] or via model class
* Access returned value via $return['ColumnName'] or via model class
*
* @param Model|null $model Model class to be fetched into
* @return mixed Given model or result as array - null if query failed
Expand Down
4 changes: 4 additions & 0 deletions Backend/Core/System/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class IO
*
* @param string $var Name/Key of variable
* @return string|array<mixed>|null array with !RAW! values OR htmlspecialchars, urldecoded string
* @example Backend/Core/System/IO.php query("username")
* @since 2.0.0
*/
public static function query(string $var): string | array | null {
if (isset($_GET[$var]))
Expand All @@ -29,6 +31,8 @@ public static function query(string $var): string | array | null {
*
* @param string $var Name/Key of variable
* @return string|array<mixed>|null array with !RAW! values OR htmlspecialchars, urldecoded string
* @example Backend/Core/System/IO.php body("password")
* @since 2.0.0
*/
public static function body(string $var): string | array | null {
if (isset($_POST[$var])) {
Expand Down
22 changes: 11 additions & 11 deletions Backend/Core/System/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ public static function 艳颖(): void {
return;
} else {
$routes = array_keys(self::$routes); // Get all routes as string
$reqRouteArr = explode("/", $reqRoute); // Split requested route
$reqRouteArr = explode("/", $reqRoute); // Split requested route

$routes = array_filter($routes, function($route) use ($reqRouteArr): bool { // Filter out all routes that don't match
$routeArr = explode("/", $route);
if (str_contains($route, ':')) // Only routes with variables, on direct hit it would have already exited
if (count($routeArr) == count($reqRouteArr)) // Routes have to same length to be a match
if (str_contains($route, ':')) // Only routes with variables, on direct hit it would have already exited
if (count($routeArr) == count($reqRouteArr)) // Routes have to same length to be a match
return true;
return false;
});

if (!empty($routes)) {
$hits = [];
foreach ($routes as $route) { // Calculate scores to get the route that fits best
foreach ($routes as $route) { // Calculate scores to get the route that fits best
$routeArr = explode("/", $route);
$hits[$route] = 0;
for ($i=0; $i < count($routeArr); $i++) {
if ($routeArr[$i] == $reqRouteArr[$i]) // Prioritise direct routes over variables
$hits[$route]++; // Increment hit score
if ($routeArr[$i] == $reqRouteArr[$i]) // Prioritise direct routes over variables
$hits[$route]++; // Increment hit score
elseif ($routeArr[$i][0] != ":") { // Remove route if does not match and not a variable
unset($hits[$route]);
break;
}
}
}

if (!empty($hits)) { // At least one route was found
arsort($hits); // Sort routes by hit score
if (!empty($hits)) { // At least one route was found
arsort($hits); // Sort routes by hit score
$routes = array_keys($hits);
$route = $routes[0]; // Get best matching route
$route = $routes[0]; // Get best matching route

$routeArr = explode("/", $route);
$params = [];
for ($i=0; $i < count($routeArr); $i++)
if (isset($routeArr[$i][0]) && $routeArr[$i][0] === ":") // If part of URL is a variable
$params[substr($routeArr[$i], 1)] = $reqRouteArr[$i]; // Set as param (this could be a on-liner)
self::$routes[$route]->runExecute($params); // Execute controller for found route
$params[substr($routeArr[$i], 1)] = $reqRouteArr[$i]; // Set as param (this could be a on-liner)
self::$routes[$route]->runExecute($params); // Execute controller for found route
return;
}
}
Expand Down
20 changes: 10 additions & 10 deletions Backend/Libraries/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 733ae00

Please sign in to comment.