0

所以正如标题所说,我在 Symfony 中的消息没有被处理。一点也不。甚至没有出现错误。

所以我的信使配置是完全默认的。但是我设置了多个总线,如下所示

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    ###MESAGE BUS
    _instanceof:
        App\App\Shared\Domain\Bus\CommandBusHandler:
            tags: [{ name: messenger.message_handler, bus: command.bus }]

        App\App\Shared\Domain\Bus\QueryBusHandler:
            tags: [{ name: messenger.message_handler, bus: query.bus }]

        App\App\Shared\Domain\Bus\EventBusHandler:
            tags: [{ name: messenger.message_handler, bus: event.bus }]

    command.bus.dispatcher:
        class: App\App\Shared\Infrastructure\Symfony\MessageBus
        arguments:
            $messageAllowedType: 'App\App\Shared\Domain\Bus\Message\CommandMessageInterface'

    query.bus.dispatcher:
        class: App\App\Shared\Infrastructure\Symfony\MessageBus
        arguments:
            $messageAllowedType: 'App\App\Shared\Domain\Bus\Message\QueryMessageInterface'

    event.bus.dispatcher:
        class: App\App\Shared\Infrastructure\Symfony\MessageBus
        arguments:
            $messageAllowedType: 'App\App\Shared\Domain\Bus\Message\EventMessageInterface'

    App\App\Shared\Infrastructure\Symfony\BlackBirdMessageBusInterface $commandDispatcher: '@command.bus.dispatcher'
    App\App\Shared\Infrastructure\Symfony\BlackBirdMessageBusInterface $query: '@query.bus.dispatcher'
    App\App\Shared\Infrastructure\Symfony\BlackBirdMessageBusInterface: '@event.bus.dispatcher'

我的输出bin/console debug:mess是: 在此处输入图像描述

我在课堂上注意到的:\Symfony\Component\Messenger\Handler\HandlersLocator空数组在构造函数中作为处理程序列表传递。

我在 5.2 版本中使用 symfony 和 symfony/messenger

和我的调度代理(如果重要):

class MessageBus implements BlackBirdMessageBusInterface
 {
  private MessageBusInterface $messageBus;

  private TranslatorInterface $translator;

  private string $messageAllowedType = ''; //if empty allow all types

  public function __construct(
      MessageBusInterface $messageBus,
      TranslatorInterface $translator,
      string $messageAllowedType = ''
  )
  { 
      $this->messageBus = $messageBus;
      $this->translator = $translator;
      $this->messageAllowedType = $messageAllowedType;
  }



  public function publish(array $events): void
  {
      foreach ($events as $event) {
          $this->dispatch($event);
      }
  }

  public function dispatch($message, array $stamps = []): Envelope
  {
      // $this->guardMessageType($message);

      return $this->messageBus->dispatch($message, $stamps);
  }

  private function guardMessageType($message)
  {
      if (false === empty($this->messageAllowedType) && false === is_a($message, $this->messageAllowedType)) {
          throw  new \LogicException($this->translator->trans('dispatcher.wrong_message'));
      }
  }

}

4

0 回答 0