Remove all routing cache logic (cacheRoutes, loadCachedRoutes) and related code; switch to pure dynamic routing for demo and framework.
This commit is contained in:
parent
6866c1e347
commit
0c21bceb82
3 changed files with 189 additions and 67 deletions
189
example/insomnia.json
Normal file
189
example/insomnia.json
Normal file
|
|
@ -0,0 +1,189 @@
|
||||||
|
{
|
||||||
|
"_type": "export",
|
||||||
|
"__export_format": 4,
|
||||||
|
"__export_date": "2024-05-15T12:00:00.000Z",
|
||||||
|
"__export_source": "insomnia.desktop.app:v2024.2.0",
|
||||||
|
"resources": [
|
||||||
|
{
|
||||||
|
"_id": "wrk_1",
|
||||||
|
"created": 1680000000000,
|
||||||
|
"description": "Demo Workspace für MariaCore Router",
|
||||||
|
"name": "MariaCore Demo",
|
||||||
|
"_type": "workspace"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "env_1",
|
||||||
|
"parentId": "wrk_1",
|
||||||
|
"name": "Base Environment",
|
||||||
|
"data": {
|
||||||
|
"base_url": "http://localhost:8080"
|
||||||
|
},
|
||||||
|
"_type": "environment"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "fld_demo",
|
||||||
|
"parentId": "wrk_1",
|
||||||
|
"name": "Demo Endpunkte",
|
||||||
|
"_type": "request_group"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_home",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Home (HTML)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_routes_json",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Routenübersicht (JSON)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/routes",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_routes_html",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Routenübersicht (HTML)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/routes.html",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_hello",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Hello (mit Name)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/hello/Insomnia",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_user",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "User Info (mit ID)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/user/42",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_protected_fail",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Protected (ohne Auth, sollte 401 liefern)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/protected",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_protected_auth",
|
||||||
|
"parentId": "fld_demo",
|
||||||
|
"name": "Protected (mit Auth-Header)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/protected",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"name": "x-auth",
|
||||||
|
"value": "geheim"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "fld_api",
|
||||||
|
"parentId": "wrk_1",
|
||||||
|
"name": "API Endpunkte",
|
||||||
|
"_type": "request_group"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_api_users",
|
||||||
|
"parentId": "fld_api",
|
||||||
|
"name": "API: Alle Users (GET)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/api/users",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_api_user_id",
|
||||||
|
"parentId": "fld_api",
|
||||||
|
"name": "API: User mit ID (GET)",
|
||||||
|
"method": "GET",
|
||||||
|
"url": "{{ base_url }}/api/users/42",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_api_user_create",
|
||||||
|
"parentId": "fld_api",
|
||||||
|
"name": "API: User anlegen (POST)",
|
||||||
|
"method": "POST",
|
||||||
|
"url": "{{ base_url }}/api/users",
|
||||||
|
"body": {
|
||||||
|
"mimeType": "application/json",
|
||||||
|
"text": "{\n \"name\": \"Insomnia User\"\n}"
|
||||||
|
},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"name": "Content-Type",
|
||||||
|
"value": "application/json"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_api_user_update",
|
||||||
|
"parentId": "fld_api",
|
||||||
|
"name": "API: User aktualisieren (PUT)",
|
||||||
|
"method": "PUT",
|
||||||
|
"url": "{{ base_url }}/api/users/42",
|
||||||
|
"body": {
|
||||||
|
"mimeType": "application/json",
|
||||||
|
"text": "{\n \"name\": \"Neuer Name\"\n}"
|
||||||
|
},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"name": "Content-Type",
|
||||||
|
"value": "application/json"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_type": "request"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "req_api_user_delete",
|
||||||
|
"parentId": "fld_api",
|
||||||
|
"name": "API: User löschen (DELETE)",
|
||||||
|
"method": "DELETE",
|
||||||
|
"url": "{{ base_url }}/api/users/42",
|
||||||
|
"body": {},
|
||||||
|
"parameters": [],
|
||||||
|
"headers": [],
|
||||||
|
"_type": "request"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -10,14 +10,6 @@ use MyCodebox\MariaCore\Router;
|
||||||
// Router-Instanz erstellen
|
// Router-Instanz erstellen
|
||||||
$router = new Router();
|
$router = new Router();
|
||||||
|
|
||||||
// Beispiel: Routen-Caching verwenden
|
|
||||||
$cacheFile = __DIR__ . '/../cache/routes.cache.php';
|
|
||||||
|
|
||||||
// Wenn Cache existiert, lade ihn, sonst speichere nach Registrierung der Routen
|
|
||||||
if (file_exists($cacheFile)) {
|
|
||||||
$router->loadCachedRoutes($cacheFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Beispiel: Globale Middleware (wird bei jedem Request ausgeführt)
|
// Beispiel: Globale Middleware (wird bei jedem Request ausgeführt)
|
||||||
$router->addMiddleware(function (Request $request, callable $next) {
|
$router->addMiddleware(function (Request $request, callable $next) {
|
||||||
// Beispiel: Logging
|
// Beispiel: Logging
|
||||||
|
|
|
||||||
|
|
@ -349,65 +349,6 @@ class Router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// --- Routen-Caching ---
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Speichert die aktuelle Routenstruktur und benannten Routen als Cache-Datei.
|
|
||||||
*
|
|
||||||
* @param string $file
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function cacheRoutes(string $file): void
|
|
||||||
{
|
|
||||||
// Prüfe auf Closures in den Handlern
|
|
||||||
foreach ($this->routes as $method => $routes) {
|
|
||||||
foreach ($routes as $pattern => $route) {
|
|
||||||
if ($route['handler'] instanceof \Closure) {
|
|
||||||
throw new \RuntimeException("Routen-Caching nicht möglich: Handler für $method $pattern ist ein Closure.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$data = [
|
|
||||||
'routes' => $this->routes,
|
|
||||||
'namedRoutes' => $this->namedRoutes,
|
|
||||||
];
|
|
||||||
file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lädt die Routenstruktur und benannten Routen aus einer Cache-Datei.
|
|
||||||
*
|
|
||||||
* @param string $file
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function loadCachedRoutes(string $file): void
|
|
||||||
{
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
throw new \RuntimeException("Routen-Cache-Datei nicht gefunden: $file");
|
|
||||||
}
|
|
||||||
$data = include $file;
|
|
||||||
if (!is_array($data) || !isset($data['routes'], $data['namedRoutes'])) {
|
|
||||||
throw new \RuntimeException("Ungültiges Routen-Cache-Format in: $file");
|
|
||||||
}
|
|
||||||
// Prüfe auf Closures im Cache (kann durch vorherigen Fehler entstanden sein)
|
|
||||||
foreach ($data['routes'] as $method => $routes) {
|
|
||||||
foreach ($routes as $pattern => $route) {
|
|
||||||
if (isset($route['handler']) && $route['handler'] instanceof \Closure) {
|
|
||||||
// Lösche fehlerhaften Cache und lade nicht
|
|
||||||
@unlink($file);
|
|
||||||
// Optional: Log oder Hinweis
|
|
||||||
trigger_error("Fehlerhafter Routen-Cache (enthält Closures) wurde gelöscht: $file", E_USER_WARNING);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->routes = $data['routes'];
|
|
||||||
$this->namedRoutes = $data['namedRoutes'];
|
|
||||||
$this->routesAreCached = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wandelt ein Routen-Pattern in einen Regex um und extrahiert Parameternamen
|
* Wandelt ein Routen-Pattern in einen Regex um und extrahiert Parameternamen
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue