Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
119 lines
5.1 KiB
PHP
119 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/controllers/MediaController.php';
|
|
require_once __DIR__ . '/controllers/CastController.php';
|
|
require_once __DIR__ . '/controllers/ImageController.php';
|
|
require_once __DIR__ . '/controllers/SettingsController.php';
|
|
require_once __DIR__ . '/services/DocumentationService.php';
|
|
require_once __DIR__ . '/services/ApiLogger.php';
|
|
|
|
class Router {
|
|
private PDO $pdo;
|
|
private MediaController $mediaController;
|
|
private CastController $castController;
|
|
private ImageController $imageController;
|
|
private SettingsController $settingsController;
|
|
private DocumentationService $documentationService;
|
|
private ApiLogger $logger;
|
|
|
|
public function __construct(PDO $pdo) {
|
|
$this->pdo = $pdo;
|
|
$this->mediaController = new MediaController($pdo);
|
|
$this->castController = new CastController($pdo);
|
|
$this->imageController = new ImageController();
|
|
$this->settingsController = new SettingsController($pdo);
|
|
$this->documentationService = new DocumentationService();
|
|
$this->logger = ApiLogger::getInstance();
|
|
}
|
|
|
|
public function route(string $method, array $pathSegments): array {
|
|
$path = '/' . implode('/', $pathSegments);
|
|
$queryString = $_SERVER['QUERY_STRING'] ?? '';
|
|
$fullPath = $queryString ? $path . '?' . $queryString : $path;
|
|
|
|
// Request loggen
|
|
$body = null;
|
|
if ($method === 'POST' || $method === 'PUT') {
|
|
$body = json_decode(file_get_contents('php://input'), true);
|
|
}
|
|
$this->logger->logRequest($method, $fullPath, $_GET, $body);
|
|
|
|
if (empty($pathSegments)) {
|
|
$response = $this->getRoot();
|
|
$this->logger->logResponse($method, $fullPath, 200, $response);
|
|
return $response;
|
|
}
|
|
|
|
$resource = $pathSegments[0];
|
|
|
|
try {
|
|
switch ($resource) {
|
|
case 'images':
|
|
// Images are served directly, bypass JSON response
|
|
$this->imageController->handleRequest($method, $pathSegments);
|
|
exit;
|
|
case 'media':
|
|
$response = $this->mediaController->handleRequest($method, $pathSegments);
|
|
$statusCode = http_response_code();
|
|
$this->logger->logResponse($method, $fullPath, $statusCode, $response);
|
|
return $response;
|
|
case 'cast':
|
|
$response = $this->castController->handleRequest($method, $pathSegments);
|
|
$statusCode = http_response_code();
|
|
$this->logger->logResponse($method, $fullPath, $statusCode, $response);
|
|
return $response;
|
|
case 'settings':
|
|
$response = $this->settingsController->handleRequest($method, $pathSegments);
|
|
$statusCode = http_response_code();
|
|
$this->logger->logResponse($method, $fullPath, $statusCode, $response);
|
|
return $response;
|
|
case 'docs':
|
|
$response = $this->getDocumentation();
|
|
$this->logger->logResponse($method, $fullPath, 200, $response);
|
|
return $response;
|
|
default:
|
|
http_response_code(404);
|
|
$response = ['success' => false, 'error' => 'Endpoint not found'];
|
|
$this->logger->logResponse($method, $fullPath, 404, $response);
|
|
return $response;
|
|
}
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
$response = ['success' => false, 'error' => $e->getMessage()];
|
|
$this->logger->logError($method, $fullPath, $e->getMessage());
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
private function getDocumentation(): array {
|
|
$docs = $this->documentationService->generateDocumentation();
|
|
return ['success' => true, 'data' => $docs];
|
|
}
|
|
|
|
private function getRoot(): array {
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Media API v1.0',
|
|
'endpoints' => [
|
|
'GET /api/docs' => 'Automatische API-Dokumentation',
|
|
'GET /api/images/*' => 'Bilder abrufen (z.B. /api/images/games/poster_xxx.webp)',
|
|
'GET /api/media' => 'Alle Medien abrufen',
|
|
'GET /api/media/:id' => 'Ein Medium abrufen',
|
|
'POST /api/media' => 'Neues Medium erstellen',
|
|
'PUT /api/media/:id' => 'Medium aktualisieren',
|
|
'DELETE /api/media/:id' => 'Medium löschen',
|
|
'GET /api/cast' => 'Alle Cast-Mitglieder abrufen',
|
|
'GET /api/cast/:id' => 'Cast-Mitglied abrufen',
|
|
'GET /api/cast/:id/media' => 'Alle Medien eines Cast-Mitglieds abrufen',
|
|
'POST /api/cast' => 'Neues Cast-Mitglied erstellen',
|
|
'PUT /api/cast/:id' => 'Cast-Mitglied aktualisieren',
|
|
'DELETE /api/cast/:id' => 'Cast-Mitglied löschen',
|
|
'GET /api/settings' => 'Einstellungen abrufen',
|
|
'PUT /api/settings' => 'Einstellungen aktualisieren'
|
|
]
|
|
];
|
|
}
|
|
}
|