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,21 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../models/Cast.php';
|
||||
require_once __DIR__ . '/../models/AdultCast.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class CastController {
|
||||
private $cast;
|
||||
private $adultCast;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private Cast $cast;
|
||||
private AdultCast $adultCast;
|
||||
private ApiLogger $logger;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->cast = new Cast($pdo);
|
||||
$this->adultCast = new AdultCast($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
|
||||
public function handleRequest(string $method, array $segments): array {
|
||||
$id = isset($segments[1]) ? (int)$segments[1] : null;
|
||||
$subResource = isset($segments[2]) ? $segments[2] : null;
|
||||
|
||||
@@ -26,7 +28,7 @@ class CastController {
|
||||
// die("adult");
|
||||
return $this->handleAdult($method, $id, $segments);
|
||||
}
|
||||
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $id ? $this->getOne($id, $segments) : $this->getAll();
|
||||
@@ -42,7 +44,7 @@ class CastController {
|
||||
}
|
||||
}
|
||||
|
||||
private function handleAdult($method, $id, $segments) {
|
||||
private function handleAdult(string $method, ?int $id, array $segments): array {
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
@@ -62,7 +64,7 @@ class CastController {
|
||||
}
|
||||
}
|
||||
|
||||
private function getAdultAll() {
|
||||
private function getAdultAll(): array {
|
||||
$filters = [];
|
||||
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
|
||||
if (isset($_GET['ethnicity'])) $filters['ethnicity'] = $_GET['ethnicity'];
|
||||
@@ -75,7 +77,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => $result];
|
||||
}
|
||||
|
||||
private function getAdultOne($id) {
|
||||
private function getAdultOne(?int $id): array {
|
||||
$cast = $this->adultCast->getWithAdultSpecifics($id);
|
||||
if (!$cast) {
|
||||
http_response_code(404);
|
||||
@@ -84,7 +86,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => $cast];
|
||||
}
|
||||
|
||||
private function createAdult() {
|
||||
private function createAdult(): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
@@ -119,7 +121,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => ['id' => $castId]];
|
||||
}
|
||||
|
||||
private function updateAdult($id) {
|
||||
private function updateAdult(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
@@ -137,7 +139,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => ['id' => $id]];
|
||||
}
|
||||
|
||||
private function deleteAdultSpecifics($id) {
|
||||
private function deleteAdultSpecifics(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
@@ -152,7 +154,7 @@ class CastController {
|
||||
$this->logger->logResponse('DELETE', "/api/cast/adult/$id", 200, ['message' => 'Adult specifics deleted successfully']);
|
||||
return ['success' => true, 'message' => 'Adult specifics deleted successfully'];
|
||||
}
|
||||
private function getOne($id, $segments) {
|
||||
private function getOne(?int $id, array $segments): array {
|
||||
// Prüfen ob /media angehängt wurde
|
||||
if (isset($segments[2]) && $segments[2] === 'media') {
|
||||
return $this->getMedia($id);
|
||||
@@ -169,12 +171,12 @@ class CastController {
|
||||
return ['success' => true, 'data' => $cast];
|
||||
}
|
||||
|
||||
private function getMedia($castId) {
|
||||
private function getMedia(?int $castId): array {
|
||||
$media = $this->cast->getMediaForCast($castId);
|
||||
return ['success' => true, 'data' => ['items' => $media]];
|
||||
}
|
||||
|
||||
private function getAll() {
|
||||
private function getAll(): array {
|
||||
$filters = [];
|
||||
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
|
||||
|
||||
@@ -185,7 +187,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => $result];
|
||||
}
|
||||
|
||||
private function create() {
|
||||
private function create(): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
@@ -220,7 +222,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => ['id' => $castId]];
|
||||
}
|
||||
|
||||
private function update($id) {
|
||||
private function update(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
@@ -238,7 +240,7 @@ class CastController {
|
||||
return ['success' => true, 'data' => ['id' => $id]];
|
||||
}
|
||||
|
||||
private function delete($id) {
|
||||
private function delete(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
|
||||
@@ -1,46 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class ImageController {
|
||||
private $imageDir;
|
||||
|
||||
private string $imageDir;
|
||||
|
||||
public function __construct() {
|
||||
$this->imageDir = __DIR__ . '/../public/images/';
|
||||
}
|
||||
|
||||
public function handleRequest($method, $pathSegments) {
|
||||
|
||||
public function handleRequest(string $method, array $pathSegments): array {
|
||||
// Remove 'images' from path segments
|
||||
array_shift($pathSegments);
|
||||
|
||||
|
||||
// Build file path
|
||||
$imagePath = implode('/', $pathSegments);
|
||||
$fullPath = $this->imageDir . $imagePath;
|
||||
|
||||
|
||||
// Security check: ensure the path is within the images directory
|
||||
$realPath = realpath($fullPath);
|
||||
$realImageDir = realpath($this->imageDir);
|
||||
|
||||
|
||||
if ($realPath === false || strpos($realPath, $realImageDir) !== 0) {
|
||||
http_response_code(403);
|
||||
return ['success' => false, 'error' => 'Access denied'];
|
||||
}
|
||||
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($realPath)) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Image not found'];
|
||||
}
|
||||
|
||||
|
||||
// Check if it's actually a file
|
||||
if (!is_file($realPath)) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Not a file'];
|
||||
}
|
||||
|
||||
|
||||
// Get file info
|
||||
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($fileInfo, $realPath);
|
||||
finfo_close($fileInfo);
|
||||
|
||||
|
||||
if ($mimeType === false) {
|
||||
// Fallback to common image types
|
||||
$extension = strtolower(pathinfo($realPath, PATHINFO_EXTENSION));
|
||||
@@ -54,13 +56,13 @@ class ImageController {
|
||||
];
|
||||
$mimeType = $mimeTypes[$extension] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
|
||||
// Set headers for image serving
|
||||
header('Content-Type: ' . $mimeType);
|
||||
header('Content-Length: ' . filesize($realPath));
|
||||
header('Cache-Control: public, max-age=31536000'); // Cache for 1 year
|
||||
header('Pragma: public');
|
||||
|
||||
|
||||
// Output the image
|
||||
readfile($realPath);
|
||||
exit;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../models/Media.php';
|
||||
require_once __DIR__ . '/../models/Series.php';
|
||||
require_once __DIR__ . '/../models/Music.php';
|
||||
@@ -7,24 +9,24 @@ require_once __DIR__ . '/../models/Game.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class MediaController {
|
||||
private $media;
|
||||
private $series;
|
||||
private $music;
|
||||
private $game;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private Media $media;
|
||||
private Series $series;
|
||||
private Music $music;
|
||||
private Game $game;
|
||||
private ApiLogger $logger;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->media = new Media($pdo);
|
||||
$this->series = new Series($pdo);
|
||||
$this->music = new Music($pdo);
|
||||
$this->game = new Game($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
|
||||
public function handleRequest(string $method, array $segments): array {
|
||||
$id = isset($segments[1]) ? (int)$segments[1] : null;
|
||||
$subResource = isset($segments[2]) ? $segments[2] : null;
|
||||
|
||||
|
||||
// Sub-Endpunkte für Episoden und Tracks
|
||||
if ($id && $subResource) {
|
||||
if ($subResource === 'episodes') {
|
||||
@@ -34,7 +36,7 @@ class MediaController {
|
||||
return $this->handleTracks($method, $id, $segments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $id ? $this->getOne($id) : $this->getAll();
|
||||
@@ -50,7 +52,7 @@ class MediaController {
|
||||
}
|
||||
}
|
||||
|
||||
private function handleEpisodes($method, $mediaId, $segments) {
|
||||
private function handleEpisodes(string $method, ?int $mediaId, array $segments): array {
|
||||
$episodeId = isset($segments[3]) ? (int)$segments[3] : null;
|
||||
|
||||
switch ($method) {
|
||||
@@ -71,7 +73,7 @@ class MediaController {
|
||||
}
|
||||
}
|
||||
|
||||
private function handleTracks($method, $mediaId, $segments) {
|
||||
private function handleTracks(string $method, ?int $mediaId, array $segments): array {
|
||||
$trackId = isset($segments[3]) ? (int)$segments[3] : null;
|
||||
|
||||
switch ($method) {
|
||||
@@ -92,7 +94,7 @@ class MediaController {
|
||||
}
|
||||
}
|
||||
|
||||
private function getEpisodes($mediaId) {
|
||||
private function getEpisodes(?int $mediaId): array {
|
||||
$season = isset($_GET['season']) ? (int)$_GET['season'] : null;
|
||||
$episodes = $this->series->getEpisodes($mediaId, $season);
|
||||
return ['success' => true, 'data' => ['items' => $episodes]];
|
||||
@@ -103,7 +105,7 @@ class MediaController {
|
||||
* @param int $mediaId Media ID
|
||||
* @return array Created episode ID
|
||||
*/
|
||||
private function addEpisode($mediaId) {
|
||||
private function addEpisode(?int $mediaId): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
@@ -120,7 +122,7 @@ class MediaController {
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Updated episode ID
|
||||
*/
|
||||
private function updateEpisode($episodeId) {
|
||||
private function updateEpisode(?int $episodeId): array {
|
||||
if (!$episodeId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Episode ID required'];
|
||||
@@ -141,7 +143,7 @@ class MediaController {
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Success message
|
||||
*/
|
||||
private function deleteEpisode($episodeId) {
|
||||
private function deleteEpisode(?int $episodeId): array {
|
||||
if (!$episodeId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Episode ID required'];
|
||||
@@ -160,7 +162,7 @@ class MediaController {
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Episode data
|
||||
*/
|
||||
private function getEpisode($episodeId) {
|
||||
private function getEpisode(?int $episodeId): array {
|
||||
// Episode direkt aus Datenbank abrufen
|
||||
$stmt = $this->series->getConnection()->prepare("SELECT * FROM episodes WHERE id = ?");
|
||||
$stmt->execute([$episodeId]);
|
||||
@@ -173,12 +175,12 @@ class MediaController {
|
||||
return ['success' => true, 'data' => $episode];
|
||||
}
|
||||
|
||||
private function getTracks($mediaId) {
|
||||
private function getTracks(?int $mediaId): array {
|
||||
$tracks = $this->music->getTracks($mediaId);
|
||||
return ['success' => true, 'data' => ['items' => $tracks]];
|
||||
}
|
||||
|
||||
private function addTrack($mediaId) {
|
||||
private function addTrack(?int $mediaId): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
@@ -190,7 +192,7 @@ class MediaController {
|
||||
return ['success' => true, 'data' => ['id' => $trackId]];
|
||||
}
|
||||
|
||||
private function updateTrack($trackId) {
|
||||
private function updateTrack(?int $trackId): array {
|
||||
if (!$trackId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Track ID required'];
|
||||
@@ -206,7 +208,7 @@ class MediaController {
|
||||
return ['success' => true, 'data' => ['id' => $trackId]];
|
||||
}
|
||||
|
||||
private function deleteTrack($trackId) {
|
||||
private function deleteTrack(?int $trackId): array {
|
||||
if (!$trackId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Track ID required'];
|
||||
@@ -220,7 +222,7 @@ class MediaController {
|
||||
return ['success' => true, 'message' => 'Track deleted successfully'];
|
||||
}
|
||||
|
||||
private function getTrack($trackId) {
|
||||
private function getTrack(?int $trackId): array {
|
||||
// Track direkt aus Datenbank abrufen
|
||||
$stmt = $this->music->getConnection()->prepare("SELECT * FROM tracks WHERE id = ?");
|
||||
$stmt->execute([$trackId]);
|
||||
@@ -238,7 +240,7 @@ class MediaController {
|
||||
* @param int $id Media ID
|
||||
* @return array Media object with relations
|
||||
*/
|
||||
private function getOne($id) {
|
||||
private function getOne(?int $id): array {
|
||||
// Zuerst Basis-Media abrufen um Typ zu bestimmen
|
||||
$baseMedia = $this->media->getBase($id);
|
||||
if (!$baseMedia) {
|
||||
@@ -268,7 +270,7 @@ class MediaController {
|
||||
* Get all media items with filtering and pagination
|
||||
* @return array Paginated media list
|
||||
*/
|
||||
private function getAll() {
|
||||
private function getAll(): array {
|
||||
$filters = [];
|
||||
if (isset($_GET['category'])) $filters['category'] = $_GET['category'];
|
||||
if (isset($_GET['type'])) $filters['type'] = $_GET['type'];
|
||||
@@ -286,7 +288,7 @@ class MediaController {
|
||||
* Create a new media item
|
||||
* @return array Created media ID
|
||||
*/
|
||||
private function create() {
|
||||
private function create(): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
@@ -341,7 +343,7 @@ class MediaController {
|
||||
* @param int $id Media ID
|
||||
* @return array Updated media ID
|
||||
*/
|
||||
private function update($id) {
|
||||
private function update(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
@@ -375,7 +377,7 @@ class MediaController {
|
||||
* @param int $id Media ID
|
||||
* @return array Success message
|
||||
*/
|
||||
private function delete($id) {
|
||||
private function delete(?int $id): array {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../models/Settings.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class SettingsController {
|
||||
private $settings;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private Settings $settings;
|
||||
private ApiLogger $logger;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->settings = new Settings($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
|
||||
public function handleRequest(string $method, array $segments): array {
|
||||
$path = '/' . implode('/', $segments);
|
||||
$this->logger->logRequest($method, $path);
|
||||
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $this->get();
|
||||
@@ -26,36 +28,36 @@ class SettingsController {
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function get() {
|
||||
|
||||
private function get(): array {
|
||||
$settings = $this->settings->getSettings();
|
||||
|
||||
|
||||
if (!$settings) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Settings not found'];
|
||||
}
|
||||
|
||||
|
||||
return ['success' => true, 'data' => $settings];
|
||||
}
|
||||
|
||||
private function update() {
|
||||
|
||||
private function update(): array {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
|
||||
$settings = $this->settings->updateSettings($data);
|
||||
|
||||
|
||||
if (!$settings) {
|
||||
http_response_code(500);
|
||||
return ['success' => false, 'error' => 'Failed to update settings'];
|
||||
}
|
||||
|
||||
|
||||
$this->logger->logRequest('PUT', '/api/settings', [], $data);
|
||||
$this->logger->logResponse('PUT', '/api/settings', 200, $settings);
|
||||
|
||||
|
||||
return ['success' => true, 'data' => $settings];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user