Backup #494
| ID | 494 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Framework/Router.php |
| Version | 4 |
| Typ |
modified |
| Größe | 3.0 KB |
| Hash | bee9626674eca210a41c59b7fed585989a9d736f6f6e6d741a5120d28e9ba7fc |
| Datum | 2025-12-22 15:32:13 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
namespace Framework;
class Router
{
protected array $routes = [];
protected ?Container $container = null;
/**
* Set the DI container for controller resolution.
*/
public function setContainer(Container $container): void
{
$this->container = $container;
}
public function get(string $path, array|callable $handler): self
{
return $this->add('GET', $path, $handler);
}
public function post(string $path, array|callable $handler): self
{
return $this->add('POST', $path, $handler);
}
public function put(string $path, array|callable $handler): self
{
return $this->add('PUT', $path, $handler);
}
public function delete(string $path, array|callable $handler): self
{
return $this->add('DELETE', $path, $handler);
}
public function add(string $method, string $path, array|callable $handler): self
{
$this->routes[] = [
'method' => $method,
'path' => $path,
'handler' => $handler,
];
return $this;
}
public function dispatch(string $uri, string $method): void
{
foreach ($this->routes as $route) {
if ($route['method'] !== $method) {
continue;
}
$pattern = $this->convertToRegex($route['path']);
if (preg_match($pattern, $uri, $matches)) {
array_shift($matches);
$this->handle($route['handler'], $matches);
return;
}
}
http_response_code(404);
echo '404 Not Found';
}
protected function convertToRegex(string $path): string
{
// Support {name:pattern} syntax for custom patterns (e.g., {path:.*})
$pattern = preg_replace_callback('/\{([a-zA-Z]+):([^}]+)\}/', function ($matches) {
return '(' . $matches[2] . ')';
}, $path);
// Support simple {name} syntax
$pattern = preg_replace('/\{([a-zA-Z]+)\}/', '([^/]+)', $pattern);
return '#^' . $pattern . '$#';
}
protected function handle(array|callable $handler, array $params): void
{
if (is_callable($handler)) {
call_user_func_array($handler, $params);
return;
}
[$controllerClass, $method] = $handler;
$controller = $this->resolveController($controllerClass);
/** @var callable $callback */
$callback = [$controller, $method];
call_user_func_array($callback, $params);
}
/**
* Resolve a controller using the container or direct instantiation.
*
* @param class-string $controllerClass
*/
protected function resolveController(string $controllerClass): Controller
{
// Use container if available (with autowiring)
if ($this->container !== null) {
return $this->container->get($controllerClass);
}
// Fallback to direct instantiation
return new $controllerClass();
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 817 |
5 |
modified |
3.0 KB |
2025-12-23 08:09 |
| 494 |
4 |
modified |
3.0 KB |
2025-12-22 15:32 |
| 491 |
3 |
modified |
2.5 KB |
2025-12-22 15:29 |
| 490 |
2 |
modified |
2.3 KB |
2025-12-22 15:29 |
| 402 |
1 |
modified |
2.2 KB |
2025-12-22 08:53 |
← Zurück zur Übersicht