feat(maintenance): Ajoute mode maintenance via fichier au lieu de variable d'env
```
This commit is contained in:
Serreau Jovann
2026-01-21 14:04:33 +01:00
parent 0d58e9fd26
commit 20d4c07f1a
3 changed files with 29 additions and 40 deletions

View File

@@ -24,8 +24,13 @@ services:
# Utilisation du listener de Nelmio (identifiant officiel)
$cspListener: '@nelmio_security.csp_listener'
App\Security\MaintenanceListener:
arguments:
$isMaintenance: '%env(MAINTENANCE_ENABLED)%'
$projectDir: '%kernel.project_dir%'
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 255 }
App\Command\MaintenanceCommand:
arguments:
$projectDir: '%kernel.project_dir%'

View File

@@ -8,16 +8,19 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
#[AsCommand(
name: 'app:maintenance',
description: 'Active ou désactive le mode maintenance',
)]
#[AsCommand(name: 'app:maintenance')]
class MaintenanceCommand extends Command
{
protected function configure(): void
private $projectDir;
public function __construct(string $projectDir)
{
parent::__construct();
$this->projectDir = $projectDir;
}
protected function configure(): void {
$this->addArgument('status', InputArgument::REQUIRED, 'on ou off');
}
@@ -25,32 +28,16 @@ class MaintenanceCommand extends Command
{
$io = new SymfonyStyle($input, $output);
$status = $input->getArgument('status');
$envFile = dirname(__DIR__, 2) . '/.env.local';
// Si .env.local n'existe pas, on prend le .env
if (!file_exists($envFile)) {
$envFile = dirname(__DIR__, 2) . '/.env';
}
$content = file_get_contents($envFile);
$value = ($status === 'on') ? 'true' : 'false';
// On remplace la valeur de la variable
if (preg_match('/MAINTENANCE_ENABLED=/', $content)) {
$content = preg_replace('/MAINTENANCE_ENABLED=(true|false)/', "MAINTENANCE_ENABLED=$value", $content);
} else {
$content .= "\nMAINTENANCE_ENABLED=$value";
}
file_put_contents($envFile, $content);
// Vider le cache pour appliquer le changement
$io->note("Mise à jour du fichier .env et nettoyage du cache...");
$maintenanceFile = $this->projectDir . '/var/.maintenance';
if ($status === 'on') {
$io->success('Mode maintenance ACTIVÉ ⚙️ (Le site est caché au public)');
touch($maintenanceFile);
$io->success('Mode maintenance ACTIVÉ (Fichier var/.maintenance créé)');
} else {
$io->success('Mode maintenance DÉSACTIVÉ ✅ (Le site est en ligne)');
if (file_exists($maintenanceFile)) {
unlink($maintenanceFile);
}
$io->success('Mode maintenance DÉSACTIVÉ (Fichier var/.maintenance supprimé)');
}
return Command::SUCCESS;

View File

@@ -9,32 +9,29 @@ use Twig\Environment;
class MaintenanceListener
{
private $twig;
private $isMaintenance;
private $projectDir;
private $ipsWhiteList;
public function __construct(Environment $twig, string $isMaintenance)
public function __construct(Environment $twig, string $projectDir)
{
$this->twig = $twig;
$this->isMaintenance = filter_var($isMaintenance, FILTER_VALIDATE_BOOLEAN);
$this->projectDir = $projectDir;
}
public function onKernelRequest(RequestEvent $event)
{
// On n'active la maintenance que sur la requête principale (pas les sous-requêtes)
if (!$event->isMainRequest()) {
return;
}
// Si la maintenance est désactivée, on ne fait rien
if (!$this->isMaintenance) {
// On vérifie si le fichier existe dans var/.maintenance
$maintenanceFile = $this->projectDir . '/var/.maintenance';
if (!file_exists($maintenanceFile)) {
return;
}
// Si l'IP du visiteur est dans la whitelist, on le laisse passer
// On affiche la page de maintenance
$content = $this->twig->render('security/maintenance.twig');
// On renvoie une réponse 503 (Service Unavailable)
$event->setResponse(new Response($content, 503));
}
}