commit 8598ba9a4a3cc74ec5a9fcb1117016b377f8b4ed Author: Spelunky Date: Sun Feb 15 01:28:04 2026 +0100 Initial commit: Projektstruktur, Router, Composer, Autoloading, Beispielrouten diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9f260c --- /dev/null +++ b/.gitignore @@ -0,0 +1,48 @@ +# Composer dependencies +/vendor/ +/composer.lock + +# OS generated files +.DS_Store +Thumbs.db + +# IDEs and editors +/.idea/ +/.vscode/ +/*.sublime-project +/*.sublime-workspace + +# Logs and reports +*.log + +# PHPUnit +/phpunit.xml +/phpunit.xml.dist +/.phpunit.result.cache + +# Cache, temp, and session files +/cache/ +/tmp/ +/sessions/ + +# User uploads +/uploads/ + +# Environment files +.env +.env.* + +# Coverage reports +/coverage/ + +# Build artifacts +/build/ +/dist/ + +# Node (if used) +/node_modules/ + +# Misc +*.swp +*.bak +*.tmp diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4f49a40 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "mycodebox/mariacore", + "description": "Ein einfacher, PSR-inspirierter PHP-Router", + "type": "project", + "require": {}, + "autoload": { + "psr-4": { + "MyCodebox\\MariaCore\\": "src/" + } + }, + "scripts": { + "dump": "composer dump-autoload", + "serve": "php -S localhost:8080 -t public" + }, + "config": { + "sort-packages": true, + "process-timeout": 0 + }, + "require-dev": {}, + "license": "MIT", + "authors": [ + { + "name": "Dein Name", + "email": "dein@email.de" + } + ], + "minimum-stability": "stable" +} diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..735a73c --- /dev/null +++ b/public/index.php @@ -0,0 +1,36 @@ +get('/', function (Request $request, array $params) { + return new Response('

Willkommen auf der Startseite!

'); +}); + +$router->get('/hello/{name}', function (Request $request, array $params) { + $name = htmlspecialchars($params['name'] ?? 'Gast'); + return new Response("

Hallo, {$name}!

"); +}); + +$router->post('/submit', function (Request $request, array $params) { + $data = $request->getParsedBody(); + $name = htmlspecialchars($data['name'] ?? 'Unbekannt'); + return new Response("

Formular empfangen, Name: {$name}

"); +}); + +// Request erzeugen und dispatchen +$request = new Request(); +$response = $router->dispatch($request); + +// Response senden +$response->send(); diff --git a/src/Http/Request.php b/src/Http/Request.php new file mode 100644 index 0000000..147e7ca --- /dev/null +++ b/src/Http/Request.php @@ -0,0 +1,72 @@ +method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; + $this->uri = strtok($_SERVER['REQUEST_URI'] ?? '/', '?'); + $this->queryParams = $_GET ?? []; + $this->parsedBody = $_POST ?? []; + $this->headers = $this->parseHeaders(); + } + + public function getMethod(): string + { + return $this->method; + } + + public function getUri(): string + { + return $this->uri; + } + + public function getQueryParams(): array + { + return $this->queryParams; + } + + public function getParsedBody(): array + { + return $this->parsedBody; + } + + public function getHeaders(): array + { + return $this->headers; + } + + public function getHeader(string $name): ?string + { + $name = strtolower($name); + foreach ($this->headers as $key => $value) { + if (strtolower($key) === $name) { + return $value; + } + } + return null; + } + + protected function parseHeaders(): array + { + $headers = []; + foreach ($_SERVER as $key => $value) { + if (str_starts_with($key, 'HTTP_')) { + $header = str_replace('_', '-', strtolower(substr($key, 5))); + $headers[$header] = $value; + } + } + return $headers; + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php new file mode 100644 index 0000000..5d76c8c --- /dev/null +++ b/src/Http/Response.php @@ -0,0 +1,62 @@ +body = $body; + $this->statusCode = $statusCode; + $this->headers = $headers; + } + + public function getStatusCode(): int + { + return $this->statusCode; + } + + public function withStatus(int $code): self + { + $clone = clone $this; + $clone->statusCode = $code; + return $clone; + } + + public function getHeaders(): array + { + return $this->headers; + } + + public function withHeader(string $name, string $value): self + { + $clone = clone $this; + $clone->headers[$name] = $value; + return $clone; + } + + public function getBody(): string + { + return $this->body; + } + + public function withBody(string $body): self + { + $clone = clone $this; + $clone->body = $body; + return $clone; + } + + public function send(): void + { + http_response_code($this->statusCode); + foreach ($this->headers as $name => $value) { + header("$name: $value"); + } + echo $this->body; + } +} diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 0000000..cd2f63d --- /dev/null +++ b/src/Router.php @@ -0,0 +1,109 @@ + [], + 'POST' => [], + // Weitere Methoden können ergänzt werden + ]; + + /** + * Route registrieren + * + * @param string $method + * @param string $pattern + * @param callable $handler + */ + public function add(string $method, string $pattern, callable $handler) + { + $method = strtoupper($method); + $this->routes[$method][$pattern] = $handler; + } + + /** + * GET-Route registrieren + * + * @param string $pattern + * @param callable $handler + */ + public function get(string $pattern, callable $handler) + { + $this->add('GET', $pattern, $handler); + } + + /** + * POST-Route registrieren + * + * @param string $pattern + * @param callable $handler + */ + public function post(string $pattern, callable $handler) + { + $this->add('POST', $pattern, $handler); + } + + /** + * Routing ausführen + * + * @param Request $request + * @return Response + */ + public function dispatch(Request $request): Response + { + $method = $request->getMethod(); + $uri = $request->getUri(); + + if (!isset($this->routes[$method])) { + return new Response('Method Not Allowed', 405); + } + + foreach ($this->routes[$method] as $pattern => $handler) { + $params = []; + $regex = $this->convertPatternToRegex($pattern, $params); + + if (preg_match($regex, $uri, $matches)) { + // Extrahiere Parameter + $params = []; + foreach ($matches as $key => $value) { + if (!is_int($key)) { + $params[$key] = $value; + } + } + return call_user_func($handler, $request, $params); + } + } + + return new Response('Not Found', 404); + } + + /** + * Wandelt ein Routen-Pattern in einen Regex um und extrahiert Parameternamen + * + * Beispiel: /user/{id} => #^/user/(?P[^/]+)$# + * + * @param string $pattern + * @param array &$params + * @return string + */ + private function convertPatternToRegex(string $pattern, array &$params): string + { + $regex = preg_replace_callback( + '#\{([a-zA-Z_][a-zA-Z0-9_]*)\}#', + function ($matches) use (&$params) { + $params[] = $matches[1]; + return '(?P<' . $matches[1] . '>[^/]+)'; + }, + $pattern + ); + return '#^' . $regex . '$#'; + } +}