src/Listener/Command/DatabaseListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Listener\Command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  5. /**
  6.  * Class DatabaseListener
  7.  * @package App\Listener\Command
  8.  */
  9. class DatabaseListener
  10. {
  11.     private array $allowedCommands = [
  12.         'doctrine:database:drop',
  13.         'doctrine:database:create'
  14.     ];
  15.     /**
  16.      * @param ConsoleCommandEvent $event
  17.      * @throws \Exception
  18.      */
  19.     public function onConsoleCommand(ConsoleCommandEvent $event)
  20.     {
  21.         $command $event->getCommand();
  22.         $input $event->getInput();
  23.         if (!$this->isProperCommand($command)) {
  24.             return;
  25.         }
  26.         if ($input->hasOption('tenant') && $input->getOption('tenant') !== null) {
  27.             $connection 'tenant';
  28.         } else {
  29.             $connection 'main';
  30.         }
  31.         $input->setOption('connection'$connection);
  32.         $input->setOption('em'$connection);
  33.         $command->getDefinition()->getOption('connection')->setDefault($connection);
  34.         $command->getDefinition()->getOption('em')->setDefault($connection);
  35.     }
  36.     /**
  37.      * @param Command $command
  38.      * @return bool
  39.      */
  40.     private function isProperCommand(Command $command)
  41.     {
  42.         return in_array($command->getName(), $this->allowedCommands);
  43.     }
  44. }