Initial commit: Projektstruktur, Router, Composer, Autoloading, Beispielrouten
This commit is contained in:
commit
8598ba9a4a
6 changed files with 355 additions and 0 deletions
48
.gitignore
vendored
Normal file
48
.gitignore
vendored
Normal file
|
|
@ -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
|
||||||
28
composer.json
Normal file
28
composer.json
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
36
public/index.php
Normal file
36
public/index.php
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use MyCodebox\MariaCore\Http\Request;
|
||||||
|
use MyCodebox\MariaCore\Http\Response;
|
||||||
|
use MyCodebox\MariaCore\Router;
|
||||||
|
|
||||||
|
// Composer Autoloader
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
// Router-Instanz erstellen
|
||||||
|
$router = new Router();
|
||||||
|
|
||||||
|
// Beispielrouten registrieren
|
||||||
|
$router->get('/', function (Request $request, array $params) {
|
||||||
|
return new Response('<h1>Willkommen auf der Startseite!</h1>');
|
||||||
|
});
|
||||||
|
|
||||||
|
$router->get('/hello/{name}', function (Request $request, array $params) {
|
||||||
|
$name = htmlspecialchars($params['name'] ?? 'Gast');
|
||||||
|
return new Response("<h1>Hallo, {$name}!</h1>");
|
||||||
|
});
|
||||||
|
|
||||||
|
$router->post('/submit', function (Request $request, array $params) {
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
$name = htmlspecialchars($data['name'] ?? 'Unbekannt');
|
||||||
|
return new Response("<h1>Formular empfangen, Name: {$name}</h1>");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Request erzeugen und dispatchen
|
||||||
|
$request = new Request();
|
||||||
|
$response = $router->dispatch($request);
|
||||||
|
|
||||||
|
// Response senden
|
||||||
|
$response->send();
|
||||||
72
src/Http/Request.php
Normal file
72
src/Http/Request.php
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyCodebox\MariaCore\Http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vereinfachte PSR-7-inspirierte Request-Klasse.
|
||||||
|
*/
|
||||||
|
class Request
|
||||||
|
{
|
||||||
|
protected string $method;
|
||||||
|
protected string $uri;
|
||||||
|
protected array $queryParams;
|
||||||
|
protected array $parsedBody;
|
||||||
|
protected array $headers;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/Http/Response.php
Normal file
62
src/Http/Response.php
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyCodebox\MariaCore\Http;
|
||||||
|
|
||||||
|
class Response
|
||||||
|
{
|
||||||
|
protected int $statusCode = 200;
|
||||||
|
protected array $headers = [];
|
||||||
|
protected string $body = '';
|
||||||
|
|
||||||
|
public function __construct(string $body = '', int $statusCode = 200, array $headers = [])
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
109
src/Router.php
Normal file
109
src/Router.php
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyCodebox\MariaCore;
|
||||||
|
|
||||||
|
use MyCodebox\MariaCore\Http\Request;
|
||||||
|
use MyCodebox\MariaCore\Http\Response;
|
||||||
|
|
||||||
|
class Router
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $routes = [
|
||||||
|
'GET' => [],
|
||||||
|
'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<id>[^/]+)$#
|
||||||
|
*
|
||||||
|
* @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 . '$#';
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue