src/EventListener/ConsoleCommandListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Kernel;
  4. use Exception;
  5. use Symfony\Bundle\FrameworkBundle\Console\Application;
  6. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  7. use Symfony\Component\Console\Input\ArrayInput;
  8. /**
  9.  * Class ConsoleCommandListener
  10.  * @package App\EventListener
  11.  */
  12. class ConsoleCommandListener
  13. {
  14.     /**
  15.      * @var boolean
  16.      */
  17.     private $debug;
  18.     /**
  19.      * @var string
  20.      */
  21.     private $environment;
  22.     /**
  23.      * ConsoleCommandListener constructor.
  24.      * @param string $environment
  25.      * @param bool   $debug
  26.      */
  27.     public function __construct(string $environmentbool $debug)
  28.     {
  29.         $this->environment $environment;
  30.         $this->debug       $debug;
  31.     }
  32.     /**
  33.      * @param ConsoleCommandEvent $event
  34.      * @throws Exception
  35.      */
  36.     public function onConsoleCommand(ConsoleCommandEvent $event)
  37.     {
  38.         // get the output instance and original input
  39.         $output        $event->getOutput();
  40.         $originalInput $event->getInput();
  41.         // get the command to be executed
  42.         $command     $event->getCommand();
  43.         $application $command->getApplication();
  44.         if (in_array($command->getName(), array('make:migration''doctrine:schema:update'))) {
  45.             $input             = new ArrayInput(array('command' => 'cache:clear'));
  46.             $commandCacheClear $application->find('cache:clear');
  47.             $commandCacheClear->run($input$output);
  48.             $application = new Application(new Kernel($this->environment$this->debug));
  49.             $calledCommand $application->find($command->getName());
  50.             $calledCommand->run($originalInput$output);
  51.             $event->disableCommand();
  52.         }
  53.     }
  54. }