abschluss

This commit is contained in:
2025-06-20 07:55:37 +00:00
parent 497e6a0bdf
commit 6c2e71dd53
34 changed files with 1370 additions and 382 deletions

39
app/Core/Container.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace Blog\Core;
use Exception;
/**
* Ein einfacher Dependency Injection Container.
*/
class Container {
/**
* @var array Liste der registrierten Instanzen.
*/
private array $instances = [];
/**
* Registriert eine Instanz oder Factory-Funktion im Container.
*
* @param string $key Der eindeutige Schlüssel für die Instanz.
* @param callable $factory Eine Factory-Funktion, die eine Instanz erzeugt.
* @return void
*/
public function set(string $key, callable $factory): void {
$this->instances[$key] = $factory;
}
/**
* Ruft eine registrierte Instanz ab und erstellt sie falls nötig.
*
* @param string $key Der Schlüssel der angeforderten Instanz.
* @return mixed Die abgerufene Instanz.
* @throws Exception Wenn keine Instanz mit dem gegebenen Schlüssel existiert.
*/
public function get(string $key): mixed {
if(!isset($this->instances[$key])) {
throw new Exception("No instance found for {$key}");
}
return $this->instances[$key]($this);
}
}

View File

@ -1,18 +1,40 @@
<?php
namespace Blog\Core;
use Blog\Middleware\middlewareInterface;
use Blog\Http\request;
use Blog\Http\response;
use Blog\Middleware\MiddlewareInterface;
use Blog\Http\Request;
use Blog\Http\Response;
/**
* Router-Klasse für das Routing von HTTP-Anfragen.
*/
class Router {
/**
* @var array Liste der registrierten Routen.
*/
private array $routes = [];
/**
* Fügt eine neue Route hinzu.
*
* @param string $method HTTP-Methode (z.B. GET, POST).
* @param string $path Pfad der Route, kann Platzhalter enthalten.
* @param callable $handler Handler-Funktion für die Route.
* @param array $middlewares Liste der Middlewares für die Route.
* @return void
*/
public function addRoute(string $method, string $path, callable $handler, array $middlewares = []): void {
$path = preg_replace('/{(\w+)}/', '(?P<$1>[^/]+)', $path);
$this->routes[] = compact("method", "path", "handler", "middlewares");
}
/**
* Verarbeitet eine eingehende HTTP-Anfrage und sucht nach einer passenden Route.
*
* @param Request $req Die eingehende HTTP-Anfrage.
* @param Response $res Die HTTP-Antwort.
* @return void
*/
public function dispatch(Request $req, Response $res): void {
$method = $req->getMethod();
$uri = $req->getPath();
@ -36,6 +58,14 @@ class Router {
->send();
}
/**
* Führt die definierten Middlewares für eine Anfrage aus.
*
* @param array $middlewares Liste der Middlewares.
* @param Request $req Die eingehende HTTP-Anfrage.
* @param Response $res Die HTTP-Antwort.
* @return bool Gibt true zurück, wenn alle Middlewares erfolgreich ausgeführt wurden, andernfalls false.
*/
private function handleMiddlewares(array $middlewares, Request $req, Response $res): bool {
foreach($middlewares as $middleware) {
$middlewareInstance = is_string($middleware) ? new $middleware() : $middleware;

View File

@ -1,19 +0,0 @@
<?php
namespace Blog\Core;
use Exception;
class Container {
private array $instances = [];
public function set(string $key, callable $factory): void {
$this->instances[$key] = $factory;
}
public function get(string $key): mixed {
if(!isset($this->instances[$key])) {
throw new Exception("No instance found for {$key}");
}
return $this->instances[$key]($this);
}
}