<?php
namespace App\EventListener;
use App\Kernel;
use Exception;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
/**
* Class ConsoleCommandListener
* @package App\EventListener
*/
class ConsoleCommandListener
{
/**
* @var boolean
*/
private $debug;
/**
* @var string
*/
private $environment;
/**
* ConsoleCommandListener constructor.
* @param string $environment
* @param bool $debug
*/
public function __construct(string $environment, bool $debug)
{
$this->environment = $environment;
$this->debug = $debug;
}
/**
* @param ConsoleCommandEvent $event
* @throws Exception
*/
public function onConsoleCommand(ConsoleCommandEvent $event)
{
// get the output instance and original input
$output = $event->getOutput();
$originalInput = $event->getInput();
// get the command to be executed
$command = $event->getCommand();
$application = $command->getApplication();
if (in_array($command->getName(), array('make:migration', 'doctrine:schema:update'))) {
$input = new ArrayInput(array('command' => 'cache:clear'));
$commandCacheClear = $application->find('cache:clear');
$commandCacheClear->run($input, $output);
$application = new Application(new Kernel($this->environment, $this->debug));
$calledCommand = $application->find($command->getName());
$calledCommand->run($originalInput, $output);
$event->disableCommand();
}
}
}