This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blog-slim.old/public/index.php
2026-03-09 16:07:17 +01:00

93 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Dotenv\Dotenv;
use Slim\Factory\AppFactory;
use Slim\Views\TwigMiddleware;
use Slim\Views\Twig;
use Medoo\Medoo;
use App\Controllers\PostController;
use App\Repositories\PostRepository;
use App\Services\HtmlSanitizer;
use App\Routes;
use App\Config;
// ============================================
// Charger les variables d'environnement
// ============================================
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
// ============================================
// Configuration
// ============================================
$env = $_ENV['APP_ENV'] ?? 'production';
$isDev = strtolower($env) === 'development';
// ============================================
// Initialisation des services
// ============================================
// Twig
$twig = Twig::create(
__DIR__ . '/../views',
['cache' => Config::getTwigCache($isDev)]
);
// Medoo (SQLite)
$dbFile = Config::getDatabasePath();
$db = new Medoo([
'type' => 'sqlite',
'database' => $dbFile,
]);
// Créer la table si elle n'existe pas
$db->pdo->exec("
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL DEFAULT '',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
");
// HtmlSanitizer
$htmlSanitizer = new HtmlSanitizer();
// PostRepository
$postRepository = new PostRepository($db);
// ============================================
// Slim App
// ============================================
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$app->add(TwigMiddleware::create($app, $twig));
// ============================================
// Routes
// ============================================
$controller = new PostController($twig, $postRepository, $htmlSanitizer);
Routes::register($app, $controller);
// ============================================
// Error Handling
// ============================================
$errorMiddleware = $app->addErrorMiddleware($isDev, $isDev, $isDev);
// ============================================
// Run
// ============================================
$app->run();