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.
114 lines
3.9 KiB
PHP
114 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../database.php';
|
|
|
|
class ApiLogger {
|
|
private static ?ApiLogger $instance = null;
|
|
private PDO $pdo;
|
|
private bool $enabled;
|
|
|
|
private function __construct() {
|
|
$this->enabled = API_LOGGING_ENABLED;
|
|
$db = new Database();
|
|
$this->pdo = $db->getConnection();
|
|
}
|
|
|
|
public static function getInstance(): ApiLogger {
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function logRequest(string|array $method, string|array $path, array $params = [], string|array|null $body = null): void {
|
|
if (!$this->enabled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$stmt = $this->pdo->prepare("
|
|
INSERT INTO api_logs (type, method, path, params, body)
|
|
VALUES ('REQUEST', :method, :path, :params, :body)
|
|
");
|
|
|
|
$methodValue = is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method;
|
|
$pathValue = is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path;
|
|
$paramsValue = is_array($params) ? (json_encode($params) ?: '[array]') : (string)$params;
|
|
$bodyValue = null;
|
|
if ($body) {
|
|
$bodyValue = is_array($body) ? (json_encode($body) ?: '[array]') : (string)$body;
|
|
}
|
|
|
|
$stmt->execute([
|
|
':method' => $methodValue,
|
|
':path' => $pathValue,
|
|
':params' => $paramsValue,
|
|
':body' => $bodyValue
|
|
]);
|
|
} catch (Throwable $e) {
|
|
error_log('Failed to log request: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function logResponse(string|array $method, string|array $path, int $statusCode, array $response): void {
|
|
if (!$this->enabled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$stmt = $this->pdo->prepare("
|
|
INSERT INTO api_logs (type, method, path, status_code, response)
|
|
VALUES ('RESPONSE', :method, :path, :status_code, :response)
|
|
");
|
|
$stmt->execute([
|
|
':method' => is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method,
|
|
':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path,
|
|
':status_code' => $statusCode,
|
|
':response' => (json_encode($response) ?: '[encoding_failed]')
|
|
]);
|
|
} catch (Throwable $e) {
|
|
error_log('Failed to log response: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function logError(string|array $method, string|array $path, string|array $error): void {
|
|
if (!$this->enabled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$stmt = $this->pdo->prepare("
|
|
INSERT INTO api_logs (type, method, path, error)
|
|
VALUES ('ERROR', :method, :path, :error)
|
|
");
|
|
$stmt->execute([
|
|
':method' => is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method,
|
|
':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path,
|
|
':error' => is_array($error) ? (json_encode($error) ?: '[array]') : (string)$error
|
|
]);
|
|
} catch (Throwable $e) {
|
|
error_log('Failed to log error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function logDebug(string|array $message): void {
|
|
if (!$this->enabled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$stmt = $this->pdo->prepare("
|
|
INSERT INTO api_logs (type, message)
|
|
VALUES ('DEBUG', :message)
|
|
");
|
|
$stmt->execute([
|
|
':message' => is_array($message) ? (json_encode($message) ?: '[array]') : (string)$message
|
|
]);
|
|
} catch (Throwable $e) {
|
|
error_log('Failed to log debug: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|