diff --git a/example/insomnia.json b/example/insomnia.json new file mode 100644 index 0000000..1cac8e7 --- /dev/null +++ b/example/insomnia.json @@ -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" + } + ] +} diff --git a/example/public/index.php b/example/public/index.php index 40f34e2..508a2d3 100644 --- a/example/public/index.php +++ b/example/public/index.php @@ -10,14 +10,6 @@ use MyCodebox\MariaCore\Router; // Router-Instanz erstellen $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) $router->addMiddleware(function (Request $request, callable $next) { // Beispiel: Logging diff --git a/src/Router.php b/src/Router.php index 1917a1e..60d4e26 100644 --- a/src/Router.php +++ b/src/Router.php @@ -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, ' $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 *