src/EventSubscriber/Under_maintenance/UnderMaintenanceCommandSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\Under_maintenance;
  4. use App\Command\BaseCommand;
  5. use Symfony\Component\Console\ConsoleEvents;
  6. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class UnderMaintenanceCommandSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private bool $isUnderMaintenance
  12.     ) {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             ConsoleEvents::COMMAND => 'onCommand',
  18.         ];
  19.     }
  20.     public function onCommand(ConsoleCommandEvent $event): void
  21.     {
  22.         if (false === $this->isUnderMaintenance) {
  23.             return;
  24.         }
  25.         if (!$event->getCommand() instanceof BaseCommand) {
  26.             return;
  27.         }
  28.         $event->getOutput()->writeln('<error>The system is currently under maintenance. The command has been blocked.</error>');
  29.         $event->disableCommand();
  30.     }
  31. }