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
julien 6e5119348e Works
2026-03-09 02:11:53 +01:00

82 lines
2.1 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;
// -------------------------
// 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) — compatibilité options
// -------------------------
$medooOptions = [
'database_type' => 'sqlite',
'database_file' => $dbFile,
'error' => PDO::ERRMODE_EXCEPTION,
'charset' => 'utf8',
];
// Certaines variantes/versions de Medoo (selon l'installation/composer.lock)
// peuvent émettre un warning en cherchant 'database_name'.
// Dupliquer la valeur pour éviter le warning sans changer le comportement.
if (!isset($medooOptions['database_name'])) {
$medooOptions['database_name'] = $medooOptions['database_file'];
}
$database = new Medoo($medooOptions);
// Créer la table si nécessaire (schéma minimal)
$database->query(
<<<'SQL'
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL
);
SQL
);
// -------------------------
// Services
// -------------------------
$services = [];
$services['twig'] = new Twig(new FilesystemLoader(__DIR__ . '/../views'), ['cache' => false]);
$services['db'] = $database;
$services['post_repository'] = new App\Repositories\PostRepositoryMedoo($database);
// -------------------------
// Slim app
// -------------------------
$app = AppFactory::create();
$app->addErrorMiddleware(true, true, true);
// Body parsing middleware nécessaire pour que getParsedBody() fonctionne
$app->addBodyParsingMiddleware();
$app->add(TwigMiddleware::create($app, $services['twig']));
// Charger routes et injecter services
(require __DIR__ . '/../src/Routes/web.php')($app, $services);
$app->run();