mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
...
This commit is contained in:
257
public/index.php
Normal file
257
public/index.php
Normal file
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Load helper functions
|
||||
require_once __DIR__ . '/../app/helpers.php';
|
||||
|
||||
// Start sessions for authentication
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Load environment variables
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
// Load database configuration
|
||||
$dbConfig = require __DIR__ . '/../config/database.php';
|
||||
\App\Database\Database::setConfig($dbConfig);
|
||||
|
||||
// Initialize database
|
||||
try {
|
||||
$pdo = \App\Database\Database::getInstance();
|
||||
} catch (Exception $e) {
|
||||
die('Database connection failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Views\Twig;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
use DI\Container;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
// Create DI Container
|
||||
$container = new Container();
|
||||
|
||||
// Register PDO instance
|
||||
$container->set(PDO::class, function () use ($pdo) {
|
||||
return $pdo;
|
||||
});
|
||||
|
||||
// Register Twig view
|
||||
$container->set('view', function () use ($container) {
|
||||
$twig = Twig::create(__DIR__ . '/../resources/views', [
|
||||
'cache' => $_ENV['APP_ENV'] === 'production' ? __DIR__ . '/../storage/views' : false,
|
||||
'debug' => $_ENV['APP_DEBUG'] === 'true',
|
||||
]);
|
||||
|
||||
// Add custom functions
|
||||
$twig->getEnvironment()->addFunction(new TwigFunction('base_url', function () {
|
||||
return sprintf(
|
||||
"%s://%s",
|
||||
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
|
||||
$_SERVER['HTTP_HOST'] ?? 'localhost'
|
||||
);
|
||||
}));
|
||||
|
||||
// Placeholder path_for function - will be updated after routes are registered
|
||||
$twig->getEnvironment()->addFunction(new TwigFunction('path_for', function ($name, $data = [], $queryParams = []) {
|
||||
// Simple implementation for now - will be replaced with proper router-based version
|
||||
$basePath = '';
|
||||
|
||||
// Handle common route patterns
|
||||
switch ($name) {
|
||||
case 'home':
|
||||
$basePath = '/';
|
||||
break;
|
||||
case 'games.index':
|
||||
$basePath = '/media/games';
|
||||
break;
|
||||
case 'games.show':
|
||||
$basePath = '/media/games/' . ($data['game_key'] ?? '');
|
||||
break;
|
||||
case 'movies.index':
|
||||
$basePath = '/media/movies';
|
||||
break;
|
||||
case 'tvshows.index':
|
||||
$basePath = '/media/tv-shows';
|
||||
break;
|
||||
case 'music.index':
|
||||
$basePath = '/media/music';
|
||||
break;
|
||||
case 'admin.index':
|
||||
$basePath = '/admin';
|
||||
break;
|
||||
case 'admin.sync':
|
||||
$basePath = '/admin/sync/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'auth.login':
|
||||
$basePath = '/login';
|
||||
break;
|
||||
case 'auth.logout':
|
||||
$basePath = '/logout';
|
||||
break;
|
||||
case 'movies.show':
|
||||
$basePath = '/media/movies/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'tvshows.show':
|
||||
$basePath = '/media/tv-shows/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'music.show':
|
||||
$basePath = '/media/music/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'adult.index':
|
||||
$basePath = '/media/adult';
|
||||
break;
|
||||
case 'adult.show':
|
||||
$basePath = '/media/adult/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'actors.index':
|
||||
$basePath = '/media/actors';
|
||||
break;
|
||||
case 'actors.show':
|
||||
$basePath = '/media/actors/' . ($data['id'] ?? '');
|
||||
break;
|
||||
case 'search.index':
|
||||
$basePath = '/search';
|
||||
break;
|
||||
default:
|
||||
$basePath = '/' . str_replace('.', '/', $name);
|
||||
}
|
||||
|
||||
// Add query parameters
|
||||
if (!empty($queryParams)) {
|
||||
$basePath .= '?' . http_build_query($queryParams);
|
||||
}
|
||||
|
||||
return $basePath;
|
||||
}));
|
||||
|
||||
$twig->getEnvironment()->addFunction(new TwigFunction('is_admin', function () use ($container) {
|
||||
$authService = $container->get(\App\Services\AuthService::class);
|
||||
return $authService->isAdmin();
|
||||
}));
|
||||
|
||||
$twig->getEnvironment()->addFunction(new TwigFunction('current_user', function () use ($container) {
|
||||
$authService = $container->get(\App\Services\AuthService::class);
|
||||
$user = $authService->getCurrentUser();
|
||||
return $user ?: (object)['username' => 'Guest'];
|
||||
}));
|
||||
|
||||
$twig->getEnvironment()->addFunction(new TwigFunction('csrf_token', function () use ($container) {
|
||||
$authService = $container->get(\App\Services\AuthService::class);
|
||||
return $authService->generateCSRFToken();
|
||||
}));
|
||||
$twig->getEnvironment()->addFilter(new TwigFilter('format_duration', function ($minutes) {
|
||||
if (!$minutes || $minutes == 0) {
|
||||
return '0m';
|
||||
}
|
||||
|
||||
$hours = floor($minutes / 60);
|
||||
$remainingMinutes = $minutes % 60;
|
||||
|
||||
if ($hours > 0) {
|
||||
return $hours . 'h ' . $remainingMinutes . 'm';
|
||||
}
|
||||
|
||||
return $remainingMinutes . 'm';
|
||||
}));
|
||||
|
||||
$twig->getEnvironment()->addFilter(new TwigFilter('json_decode', function ($jsonString) {
|
||||
if (!$jsonString) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$decoded = json_decode($jsonString, true);
|
||||
return $decoded === null ? null : $decoded;
|
||||
}));
|
||||
|
||||
return $twig;
|
||||
});
|
||||
|
||||
// Register AuthService
|
||||
$container->set(\App\Services\AuthService::class, function ($c) {
|
||||
return new \App\Services\AuthService($c->get(PDO::class));
|
||||
});
|
||||
|
||||
// Register models
|
||||
$container->set(\App\Models\User::class, function ($c) {
|
||||
return new \App\Models\User($c->get(PDO::class));
|
||||
});
|
||||
|
||||
$container->set(\App\Models\SyncLog::class, function ($c) {
|
||||
return new \App\Models\SyncLog($c->get(PDO::class));
|
||||
});
|
||||
|
||||
// Register controllers
|
||||
$container->set(\App\Controllers\AuthController::class, function ($c) {
|
||||
return new \App\Controllers\AuthController($c->get(\App\Services\AuthService::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\AdminController::class, function ($c) {
|
||||
return new \App\Controllers\AdminController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\GameController::class, function ($c) {
|
||||
return new \App\Controllers\GameController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\DashboardController::class, function ($c) {
|
||||
return new \App\Controllers\DashboardController($c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\MovieController::class, function ($c) {
|
||||
return new \App\Controllers\MovieController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\TvShowController::class, function ($c) {
|
||||
return new \App\Controllers\TvShowController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\MusicController::class, function ($c) {
|
||||
return new \App\Controllers\MusicController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\AdultController::class, function ($c) {
|
||||
return new \App\Controllers\AdultController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\ActorController::class, function ($c) {
|
||||
return new \App\Controllers\ActorController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
$container->set(\App\Controllers\SearchController::class, function ($c) {
|
||||
return new \App\Controllers\SearchController($c->get(PDO::class), $c->get('view'));
|
||||
});
|
||||
|
||||
|
||||
// Register middleware
|
||||
$container->set(\App\Http\Middleware\AuthMiddleware::class, function ($c) {
|
||||
return new \App\Http\Middleware\AuthMiddleware($c->get(\App\Services\AuthService::class));
|
||||
});
|
||||
|
||||
$container->set(\App\Http\Middleware\AdminMiddleware::class, function ($c) {
|
||||
return new \App\Http\Middleware\AdminMiddleware($c->get(\App\Services\AuthService::class));
|
||||
});
|
||||
|
||||
// Create App with DI Container
|
||||
AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
|
||||
// Add Twig-View Middleware
|
||||
$twig = $container->get('view');
|
||||
$app->add(TwigMiddleware::create($app, $twig));
|
||||
|
||||
// Add Error Middleware
|
||||
$errorMiddleware = $app->addErrorMiddleware(
|
||||
$_ENV['APP_DEBUG'] === 'true',
|
||||
true,
|
||||
true
|
||||
);
|
||||
|
||||
// Register routes
|
||||
require __DIR__ . '/../routes/web.php';
|
||||
|
||||
$app->run();
|
||||
Reference in New Issue
Block a user