Add strict types and type hints across API
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.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/controllers/MediaController.php';
|
||||
require_once __DIR__ . '/controllers/CastController.php';
|
||||
require_once __DIR__ . '/controllers/ImageController.php';
|
||||
@@ -8,15 +10,15 @@ require_once __DIR__ . '/services/DocumentationService.php';
|
||||
require_once __DIR__ . '/services/ApiLogger.php';
|
||||
|
||||
class Router {
|
||||
private $pdo;
|
||||
private $mediaController;
|
||||
private $castController;
|
||||
private $imageController;
|
||||
private $settingsController;
|
||||
private $documentationService;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
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);
|
||||
@@ -25,27 +27,27 @@ class Router {
|
||||
$this->documentationService = new DocumentationService();
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function route($method, $pathSegments) {
|
||||
|
||||
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':
|
||||
@@ -77,20 +79,20 @@ class Router {
|
||||
$this->logger->logResponse($method, $fullPath, 404, $response);
|
||||
return $response;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
} 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() {
|
||||
|
||||
private function getDocumentation(): array {
|
||||
$docs = $this->documentationService->generateDocumentation();
|
||||
return ['success' => true, 'data' => $docs];
|
||||
}
|
||||
|
||||
private function getRoot() {
|
||||
|
||||
private function getRoot(): array {
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Media API v1.0',
|
||||
|
||||
Reference in New Issue
Block a user