Не срабатывает Listener Symfony 4.3

205
16 ноября 2021, 12:20

конфиг:

house.listener:
    class: App\EventListener\HouseListener
    tags:
        - { name: kernel.event_listener, event: before.create }

сам слушатель:

namespace App\EventListener;
use App\Events\BeforeHouseCreateEvent;
class HouseListener
{
    public function onBeforeCreate(BeforeHouseCreateEvent $e)
    {
        var_dump($e);
    }
}

событие:

namespace App\Events;
use Symfony\Contracts\EventDispatcher\Event;
class BeforeHouseCreateEvent extends Event
{
    private $msg;
    private $house;
    public const NAME = 'before.create';
    public function __construct($house, $msg)
    {
        $this->msg = $msg;
        $this->house = $house;
    }
    public function getHouse()
    {
        return $this->house;
    }
    public function getMsg()
    {
        return $this->msg;
    }
}

и консольная команда, где генерируется событие:

namespace App\Command;

use App\EventListener\HouseListener;
use App\Events\BeforeHouseCreateEvent;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class HouseManageCommand extends Command
{
    /**
     * @var EventDispatcherInterface
     */
    private $eventDispatcher;
    public function __construct(EventDispatcherInterface $eventDispatcher, string $name = null)
    {
        $this->eventDispatcher = $eventDispatcher;
        parent::__construct($name);
    }
    protected function configure()
    {
        $this->setName('house:create');
    }
    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|void|null
     * @throws \Exception
     */
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $e = new BeforeHouseCreateEvent('house', 'предсоздание house');
//        при использовании addListener HouseListener корректно отрабатывает(т.е. если расскомментить нижележащую строку)
//        $this->eventDispatcher->addListener(BeforeHouseCreateEvent::NAME, [new HouseListener(), 'onBeforeCreate']);
        $this->eventDispatcher->dispatch($e, BeforeHouseCreateEvent::NAME);
    }
}

При запуске команды не срабатывает метод HouseListener-ра onBeforeCreate(). При этом при непосредственном добавлении листнера в команде через addListener(чего делать не хочется), HouseListener отрабатывает нормально. В чем может быть причина?

Answer 1

Проблема была в cache. Выполнил bin/console cache:clear и все заработало.

READ ALSO
Как объединить 2 массива в один?

Как объединить 2 массива в один?

Всем привет, есть 2 массиваПервый $attr_type:

221
strlen игнорируется

strlen игнорируется

Мне нужно проверить, не пустая ли формаесли пустая или меньше 10, то ошибка

120
Сортировка массива сразу по двум ключам с приоритетом

Сортировка массива сразу по двум ключам с приоритетом

Столкнулся с задачейНужно отсортировать многомерный массив сразу по двум значениям

125
Нужна помощь с алгоритмом на Java

Нужна помощь с алгоритмом на Java

Мне приходят данные в txt в виде:

202