<?php
declare(strict_types=1);
namespace App\EventSubscriber\Under_maintenance;
use App\Command\BaseCommand;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UnderMaintenanceCommandSubscriber implements EventSubscriberInterface
{
public function __construct(
private bool $isUnderMaintenance
) {
}
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::COMMAND => 'onCommand',
];
}
public function onCommand(ConsoleCommandEvent $event): void
{
if (false === $this->isUnderMaintenance) {
return;
}
if (!$event->getCommand() instanceof BaseCommand) {
return;
}
$event->getOutput()->writeln('<error>The system is currently under maintenance. The command has been blocked.</error>');
$event->disableCommand();
}
}