Add PHP Media API scaffold and Docker configs
Initial project scaffold for a PHP Media API including routing, controllers, models and services under api/ (Router, Media/Cast/Image/Settings controllers, models, database/bootstrap files and automatic docs service). Adds Docker support (Dockerfile, docker-compose.yml, DOCKER_README.md, php-custom.ini), .htaccess for pretty URLs, API documentation and example payloads (API_EXAMPLES.md, api/README.md, api_examples/*.json), image handling service and logging, plus a comprehensive .gitignore. This commit provides a runnable development environment and example requests to get the API up and tested quickly.
This commit is contained in:
110
api/services/ApiLogger.php
Normal file
110
api/services/ApiLogger.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
class ApiLogger {
|
||||
private static $instance = null;
|
||||
private $pdo;
|
||||
private $enabled;
|
||||
|
||||
private function __construct() {
|
||||
$this->enabled = API_LOGGING_ENABLED;
|
||||
$db = new Database();
|
||||
$this->pdo = $db->getConnection();
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function logRequest($method, $path, $params = [], $body = null) {
|
||||
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 (Exception $e) {
|
||||
error_log('Failed to log request: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logResponse($method, $path, $statusCode, $response) {
|
||||
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 (Exception $e) {
|
||||
error_log('Failed to log response: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logError($method, $path, $error) {
|
||||
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 (Exception $e) {
|
||||
error_log('Failed to log error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logDebug($message) {
|
||||
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 (Exception $e) {
|
||||
error_log('Failed to log debug: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user