phpsymfonydoctrine

Disable a Symfony command from vendor


I have a Symfony project with Doctrine. Doctrine has a command doctrine:schema:update. It gets used too often, lets say out of habits. After running the doctrine:schema:update all the migrations are messed up and I have to come and fix their development machine.

I want to disable that command (or at least make it not the easiest route), but cant figure out how (or even IF) i can do that.

I have tried:

This all does nothing, each time the update command returns the default result.


Solution

  • You can disable the command this way with an EventListener:

    namespace App\EventListener;
    
    use Symfony\Component\Console\Event\ConsoleCommandEvent;
    use Symfony\Component\Console\ConsoleEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class CommandListener implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [
                ConsoleEvents::COMMAND => 'onConsoleCommand',
            ];
        }
    
        public function onConsoleCommand(ConsoleCommandEvent $event)
        {
            if ($event->getCommand()->getName() === 'doctrine:schema:update') {
                $event->getOutput()->writeln('<error>The command doctrine:schema:update is disabled.</error>');
                $event->disableCommand(); 
            }
        }
    }
    

    And then enable it:

    services:
      App\EventListener\DisableDoctrineSchemaUpdateListener:
        tags: [{name: 'kernel.event_subscriber'}]