Simplified
This commit is contained in:
@@ -6,28 +6,85 @@ require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
use Slim\Views\Twig;
|
||||
use Medoo\Medoo;
|
||||
use App\Controllers\PostController;
|
||||
|
||||
// Charger et créer les services centralisés
|
||||
$services = App\Factories\ServiceFactory::createServices();
|
||||
// ============================================
|
||||
// Configuration
|
||||
// ============================================
|
||||
|
||||
/** @var \Slim\Views\Twig $twig */
|
||||
$twig = $services['view'];
|
||||
$env = $_ENV['APP_ENV'] ?? 'production';
|
||||
$isDev = strtolower($env) === 'development';
|
||||
|
||||
// Dossier de cache Twig (false en dev, chemin en prod)
|
||||
$twigCache = $isDev ? false : __DIR__ . '/../var/cache/twig';
|
||||
if ($twigCache && !is_dir($twigCache)) {
|
||||
@mkdir($twigCache, 0755, true);
|
||||
}
|
||||
|
||||
// Chemin base de données
|
||||
$dbFile = __DIR__ . '/../database/app.sqlite';
|
||||
$dbDir = dirname($dbFile);
|
||||
if (!is_dir($dbDir)) {
|
||||
@mkdir($dbDir, 0755, true);
|
||||
}
|
||||
if (!file_exists($dbFile)) {
|
||||
@touch($dbFile);
|
||||
@chmod($dbFile, 0664);
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Initialisation des services
|
||||
// ============================================
|
||||
|
||||
// Twig
|
||||
$twig = Twig::create(
|
||||
__DIR__ . '/../views',
|
||||
['cache' => $twigCache]
|
||||
);
|
||||
|
||||
// Medoo (SQLite)
|
||||
$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,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
");
|
||||
|
||||
// ============================================
|
||||
// Slim App
|
||||
// ============================================
|
||||
|
||||
// Slim app
|
||||
$app = AppFactory::create();
|
||||
|
||||
// Middlewares essentiels
|
||||
$app->addBodyParsingMiddleware();
|
||||
$app->add(TwigMiddleware::create($app, $twig));
|
||||
|
||||
// Charger les routes
|
||||
$routesPath = __DIR__ . '/../src/Routes/web.php';
|
||||
if (file_exists($routesPath)) {
|
||||
/** @var callable $routes */
|
||||
$routes = require $routesPath;
|
||||
$routes($app);
|
||||
}
|
||||
// ============================================
|
||||
// Routes
|
||||
// ============================================
|
||||
|
||||
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
|
||||
$controller = new PostController($twig, $db);
|
||||
|
||||
$app->get('/', [$controller, 'index']);
|
||||
$app->get('/admin', [$controller, 'admin']);
|
||||
$app->get('/admin/edit/{id}', [$controller, 'form']);
|
||||
$app->post('/admin/create', [$controller, 'create']);
|
||||
$app->post('/admin/edit/{id}', [$controller, 'update']);
|
||||
$app->post('/admin/delete/{id}', [$controller, 'delete']);
|
||||
|
||||
// ============================================
|
||||
// Run
|
||||
// ============================================
|
||||
|
||||
$errorMiddleware = $app->addErrorMiddleware($isDev, $isDev, $isDev);
|
||||
$app->run();
|
||||
|
||||
Reference in New Issue
Block a user