99 lines
2.9 KiB
PHP
99 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use Slim\Factory\AppFactory;
|
|
use Slim\Views\Twig;
|
|
use Slim\Views\TwigMiddleware;
|
|
use Twig\Loader\FilesystemLoader;
|
|
use Medoo\Medoo;
|
|
use Dotenv\Dotenv;
|
|
|
|
// Charger .env
|
|
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
|
|
$dotenv->safeLoad(); // safeLoad pour tolérer l'absence du fichier en dev
|
|
|
|
// Environnement
|
|
$appEnv = strtolower($_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'production');
|
|
$isDebug = ($appEnv === 'development' || $appEnv === 'dev');
|
|
|
|
// Configuration PHP display errors selon l'environnement
|
|
if ($isDebug) {
|
|
ini_set('display_errors', '1');
|
|
ini_set('display_startup_errors', '1');
|
|
error_reporting(E_ALL);
|
|
} else {
|
|
ini_set('display_errors', '0');
|
|
ini_set('display_startup_errors', '0');
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
|
|
}
|
|
|
|
// -------------------------
|
|
// DB SQLite + fichier
|
|
// -------------------------
|
|
function ensureDatabaseFile(string $path): void
|
|
{
|
|
if (!file_exists($path)) {
|
|
if (!is_dir(dirname($path))) {
|
|
mkdir(dirname($path), 0755, true);
|
|
}
|
|
touch($path);
|
|
chmod($path, 0664);
|
|
}
|
|
}
|
|
$dbFile = __DIR__ . '/../database/app.sqlite';
|
|
ensureDatabaseFile($dbFile);
|
|
|
|
// -------------------------
|
|
// Instancier Medoo (SQLite)
|
|
// -------------------------
|
|
$medooOptions = [
|
|
'database_type' => 'sqlite',
|
|
'database_file' => $dbFile,
|
|
'error' => PDO::ERRMODE_EXCEPTION,
|
|
'charset' => 'utf8',
|
|
];
|
|
if (!isset($medooOptions['database_name'])) {
|
|
$medooOptions['database_name'] = $medooOptions['database_file'];
|
|
}
|
|
$database = new Medoo($medooOptions);
|
|
|
|
// -------------------------
|
|
// Services (container simple)
|
|
// -------------------------
|
|
$container = [];
|
|
|
|
// Vue Twig
|
|
$container['view'] = new Twig(new FilesystemLoader(__DIR__ . '/../views'), ['cache' => false]);
|
|
|
|
// Repository Post (Medoo)
|
|
$container['postRepository'] = new App\Repositories\PostRepositoryMedoo($database);
|
|
|
|
// -------------------------
|
|
// Slim app
|
|
// -------------------------
|
|
$app = AppFactory::create();
|
|
|
|
// Configurer Error Middleware selon l'environnement
|
|
$errorMiddleware = $app->addErrorMiddleware($isDebug, $isDebug, $isDebug);
|
|
|
|
// Optionnel : personnaliser le rendu d'erreur en production pour éviter fuite d'info
|
|
if (!$isDebug) {
|
|
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
|
|
$errorHandler->registerErrorRenderer('text/html', function () {
|
|
// message générique sans trace
|
|
return '<html><head><meta charset="utf-8"><title>Erreur</title></head><body><h1>Erreur serveur</h1><p>Une erreur est survenue. Veuillez réessayer plus tard.</p></body></html>';
|
|
});
|
|
}
|
|
|
|
// Middlewares essentiels
|
|
$app->addBodyParsingMiddleware();
|
|
$app->add(TwigMiddleware::create($app, $container['view']));
|
|
|
|
// Charger routes (web.php reçoit maintenant le container)
|
|
(require __DIR__ . '/../src/Routes/web.php')($app, $container);
|
|
|
|
$app->run();
|