0

I want to create a symfony console command that list the listeners subscribed to the doctrine event dispatcher. something like symfony console app:debug-doctrine-dispatcher.

For that I need access from my command execute method to the doctrine.dbal.event_manager service. I can't manage to autowire Symfony\Bridge\Doctrine\ContainerAwareEventManager (doctrine.dbal.event_manager):

Cannot autowire service "App\Command\DebugDoctrineDispatcher": argument "$manager" of method "__construct()" references class "Symfony\Bridge\Doctrine\ContainerAwareEventManager" but no such service  
   exists. You should maybe alias this class to the existing "doctrine.dbal.default_connection.event_manager" service.

When I want to alias this service:

services:
    doctrine.dbal.default_connection.event_manager:
        alias: 'Doctrine\Common\EventManager'

I got a error too:

  You have requested a non-existent service "doctrine.dbal.default_connection.event_manager".  

The posts I've found mention autowiring and DI in __construct method of the command like other services.

Here is my command code:

<?php

namespace App\Command;

use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand('app:debug-doctrine-dispatcher')]
class DebugDoctrineDispatcher extends Command
{
    public function __construct(
        private ContainerAwareEventManager $manager,
        string $name = null
    )
    {
        parent::__construct($name);
    }

    public function execute(InputInterface $input, OutputInterface $output): int
    {
    // TODO: Change the autogenerated stub
        $output->writeln([
        'Entity Listeners and Events Subscribers registered in Doctrine dispatcher',
        '============',
        '',
    ]);

        $o = print_r($this->manager->getAllListeners(), true);
        $output->writeln($o);

        //parent::execute($input, $output);
        return Command::SUCCESS;
    }
}

Can you enlight me?

Thanks a lot!

  • You almost had it. Just flip the two lines where your defined the alias. It's is kind of backwards but the alias attribute is the service being aliased. Probably want to change `ContainerAwareEventManager` to `EventManager` as well. Also, off-topic but get rid of the $name parameter. It is going away. – Cerad Aug 26 '23 at 20:54

0 Answers0