Skip to content

Commit

Permalink
Merge pull request #8 from felix-schindler/feature/localization
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
felix-schindler authored Mar 23, 2022
2 parents 88d5209 + 15bb96e commit e822478
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Backend/Controllers/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class APIController extends Controller
{
protected array $paths = ['/api'];
protected array $paths = ['/api/sample'];
protected array $methods = ['POST'];

public function execute(): void {
Expand Down
8 changes: 0 additions & 8 deletions Backend/Controllers/ErrorController.php

This file was deleted.

3 changes: 0 additions & 3 deletions Backend/Core/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ private static function initControllers(): void {
}
}
}

// Run the class loader
ClassLoader::파람();
13 changes: 0 additions & 13 deletions Backend/Core/Functions/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ public static function encrypt(string $data, string $algo = "sha256"): string {
return hash($algo, $data);
}

/**
* Takes a path, looks if it is a url, if not -> makes a CDN URL out of it
*
* @param string $path Filename or URL
* @return string Valid url
*/
public static function getCdnUrl(string $path): string {
if (filter_var($path, FILTER_VALIDATE_URL) === false)
return DOMAIN . "/" . CDN_SUFFIX . $path;
else
return $path;
}

/**
* Generates a random uuid (Version 4)
*
Expand Down
4 changes: 2 additions & 2 deletions Backend/Core/System/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function runExecute(array $params): void {
$this->params = $params;
$this->execute();
} else {
(new ErrorView($code))->render();
(new ErrorView($code, str_contains(IO::path(), '/api/')))->render();
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ protected function redirect(string $url): never {
* @return int HTTP status code (200 === OK!)
*/
private function checkAccess(): int {
header('Access-Control-Allow-Methods: ' . implode(', ', array_merge($this->methods, ['OPTIONS', 'HEAD'])));
header('Access-Control-Allow-Methods: ' . implode(', ', array_merge(['OPTIONS', 'HEAD'], $this->methods)));
if (empty(array_intersect(['*', 'OPTIONS', 'HEAD', IO::method()], $this->methods)))
return 405;
if ($this->userRequired)
Expand Down
3 changes: 3 additions & 0 deletions Backend/Core/System/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static function PUT(string $var, bool $exact = false): array|string|null
* @return string|null Value of variable or null if not exists and on set
*/
public static function SESSION(string $var, string $value = null, bool $exact = false): ?string {
if (session_status() !== PHP_SESSION_ACTIVE)
session_start();

// Set variable
if ($value !== null) {
$_SESSION[$var] = $value;
Expand Down
2 changes: 1 addition & 1 deletion Backend/Core/System/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function 艳颖(): void {
}
}
}
(new ErrorController())->runExecute([]); // No route found -> ErrorController
(new ErrorView())->render(); // No route found -> ErrorController
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Backend/Views/Error/ErrorView.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ public function render(): void {
}

http_response_code($this->code); // Set the correct HTML response code

if ($this->isAPI) {
(new APIView([$this->code => $this->message]))->render(); // Render error as JSON
} else {
echo "<h1>" . $this->code . " - " . $this->message . "</h1>"; // Render error as HTML
(new LayoutView())
->addChild(new HeadingView($this->code . " &middot; " . $this->message))
->render(); // Render error as HTML
$this->renderChildren();
}
}
Expand Down
6 changes: 3 additions & 3 deletions Backend/Views/LayoutView.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class LayoutView extends View
public function render(): void {
?>
<!DOCTYPE html>
<html lang="de">
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -28,8 +28,8 @@ public function render(): void {
<header>
<a href="/">Home</a>
<a href="/article/2">Other one</a>
<a href="/user/2">Should not be called</a>
<a href="/api">API</a>
<a href="/user/2">All variables</a>
<a href="/api/sample">API</a>
<a href="/err">Error</a>
</header>

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rm .phpstan.neon
### Caddy
Just add the following line to your Caddyfile

```caddy
```nginx
try_files {path} /index.php
```

Expand Down
13 changes: 8 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

declare(strict_types = 1);

// $GLOBALS['start'] = microtime(true); // Stop execution time
// echo '<p>[Controller] Execution time router: ' . microtime(true) - $GLOBALS['start'] . ' seconds</p>';

// Display errors when debug is set
/* if (isset($_GET["DEBUG"])) {
ini_set('display_errors', '1');
Expand All @@ -15,13 +20,11 @@
define("DB_PASS", "pass");

define("TITLE", "Sample"); // Title for website
define("DOMAIN", "https://blog.schindlerfelix.de"); // Hosted on this domain
define("CDN_DOMAIN", "https://cdn.schindlerfelix.de"); // Domain of the CDN
define("CDN_SUFFIX", "sample/"); // https://cdn.schindlerfelix.de/blog/
define("DOMAIN", "https://schindlerfelix.de"); // Hosted on this domain

require_once("./Backend/Core/ClassLoader.php"); // Load classes
// require_once("./Backend/Libraries/vendor/autoload.php"); // Composer autoloader

// Application start
session_start(); // Start PHP session
Router::艳颖(); // Run router
ClassLoader::파람(); // Run the class loader
Router::艳颖(); // Run router

0 comments on commit e822478

Please sign in to comment.