Symfony 通知程序将自定义元数据附加到信封

Symfony notifier attach custom metadata to envelope

我正在使用 Symfony 通知程序和信使组件异步发送 SMS 消息(以及未来的推送和电子邮件通知)。

一切正常,但是发送消息后,我想记录相关信息。

我可以通过订阅 WorkerMessageHandledEvent 来捕获一条成功的消息,它为我提供了 Message 对象,以及包含 Envelope 及其内部的所有 Stamp 对象。根据所有可用信息,我将使用名为 MessageLog.

的实体将其记录在我的数据库中
class MessengerSubscriber implements EventSubscriberInterface {

    public static function getSubscribedEvents() {
        return [
            WorkerMessageHandledEvent::class => ['onHandled']
        ];
    }

    public function onHandled(WorkerMessageHandledEvent $event) {
        $log = new MessageLog();
        $log->setSentAt(new DateTime());

        if($event->getEnvelope()->getMessage() instanceof SmsMessage) {
            $log->setSubject($event->getEnvelope()->getMessage()->getSubject());
            $log->setRecipient($event->getEnvelope()->getMessage()->getPhone());
        }

        // Do more tracking
    }

}

我想做的是跟踪“调用”消息的对象。例如,如果我有一个新闻提要,并且 posting post 发送了一个通知,我想将每条记录的消息归因于那个 post(以显示受众 reach/delivery 每个 post 的统计数据 - 来自管理员 POV 审核和报告)。

我尝试着手添加 Stamp,或尝试将自定义元数据附加到消息的其他方法,但在使用 symfony/notifier 包时似乎被抽象化了。

以下是我用来发送通知的内容(或多或少的 WIP):

class PostService {

    protected NotifierInterface $notifier;

    public function ___construct(NotifierInterface $notifier) {
        $this->notifier = $notifier;
    }

    public function sendNotifications(Post $post) {
        $notification = new PostNotification($post);
        
        $recipients = [];
        foreach($post->getNewsFeed()->getSubscribers() as $user) {
            $recipients[] = new Recipient($user->getEmail(), $user->getMobilePhone());
        }

        $this->notifier->send($notification, ...$recipients);
    }

}
class PostNotification extends Notification implements SmsNotificationInterface {

    protected Post $post;

    public function __construct(Post $post) {
        parent::__construct();
        $this->post = $post;
    }

    public function getChannels(RecipientInterface $recipient): array {
        return ['sms'];
    }

    public function asSmsMessage(SmsRecipientInterface $recipient, string $transport = null): ?SmsMessage {
        if($transport === 'sms') {
            return new SmsMessage($recipient->getPhone(), $this->getPostContentAsSms());
        }

        return null;
    }

    private function getPostContentAsSms() {
        return $post->getTitle()."\n\n".$post->getContent();
    }

}

当这一切都完成时,这就是我在 WorkerMessageHandledEvent

中的全部内容
^ Symfony\Component\Messenger\Event\WorkerMessageHandledEvent^ {#5590
  -envelope: Symfony\Component\Messenger\Envelope^ {#8022
    -stamps: array:7 [
      "Symfony\Component\Messenger\Stamp\BusNameStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\BusNameStamp^ {#10417
          -busName: "messenger.bus.default"
        }
      ]
      "Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp" => array:1 [
        0 => Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp^ {#10419
          -id: "2031"
        }
      ]
      "Symfony\Component\Messenger\Stamp\TransportMessageIdStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\TransportMessageIdStamp^ {#10339
          -id: "2031"
        }
      ]
      "Symfony\Component\Messenger\Stamp\ReceivedStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\ReceivedStamp^ {#5628
          -transportName: "async"
        }
      ]
      "Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp^ {#7306}
      ]
      "Symfony\Component\Messenger\Stamp\AckStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\AckStamp^ {#7159
          -ack: Closure(Envelope $envelope, Throwable $e = null)^ {#6205
            class: "Symfony\Component\Messenger\Worker"
            this: Symfony\Component\Messenger\Worker {#5108 …}
            use: {
              $transportName: "async"
              $acked: & false
            }
          }
        }
      ]
      "Symfony\Component\Messenger\Stamp\HandledStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\HandledStamp^ {#11445
          -result: Symfony\Component\Notifier\Message\SentMessage^ {#2288
            -original: Symfony\Component\Notifier\Message\NullMessage^ {#6625
              -decoratedMessage: Symfony\Component\Notifier\Message\SmsMessage^ {#10348
                -transport: null
                -subject: ".................................................."
                -phone: "0412345678"
              }
            }
            -transport: "null"
            -messageId: null
          }
          -handlerName: "Symfony\Component\Notifier\Messenger\MessageHandler::__invoke"
        }
      ]
    ]
    -message: Symfony\Component\Notifier\Message\SmsMessage^ {#10348}
  }
  -receiverName: "async"
}

doco 向我展示了将我自己的邮票添加到信封的方法,我猜我可以用它来附加元数据,例如我的 Post 对象,但这意味着我需要使用 MessageBusInterface 发送通知。我不想这样做,因为我想通过 NotifierInterface 路由消息以获得频道策略、短信传输等的所有好处。


tl;dr:如果我使用 NotifierInterface

发送消息,我如何将一些元数据传递给 WorkerMessageHandledEvent

我找到了让它发挥作用的方法!

基本上发生的是我们这里有两个组件,Symfony notifier 和 Symfony messenger。当一起使用时,它们创建了一种向任意数量的端点发送消息的强大方式。

首先,我所做的是创建一个名为 NotificationStampsInterface 的接口和一个名为 NotificationStamps 的特征来满足该接口(通过使用接口方法将受保护数组存储到 read/write 到它).

class NotificationStampsInterface {
    
    public function getStamps(): array;

    public function addStamp(StampInterface $stamp);

    public function removeStamp(StampInterface $stamp);
}

然后可以将此接口添加到您的自定义通知对象上,在本例中 PostNotification,连同 NotificationStamps 特性一起满足接口方法。

这里的技巧是,当通过通知程序发送通知时,它最终会调用信使组件来发送消息。处理这个的位是 Symfony\Component\Notifier\Channel\SmsChannel。本质上,如果 MessageBus 可用,它将通过它推送消息,而不是直接通过通知程序。

我们可以扩展 SmsChannel class 以在 notify() 方法中添加我们自己的逻辑。

class SmsNotify extends \Symfony\Component\Notifer\Channel\SmsChannel {
   
    public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void {
        $message = null;
        if ($notification instanceof SmsNotificationInterface) {
            $message = $notification->asSmsMessage($recipient, $transportName);
        }

        if (null === $message) {
            $message = SmsMessage::fromNotification($notification, $recipient);
        }

        if (null !== $transportName) {
            $message->transport($transportName);
        }

        if (null === $this->bus) {
            $this->transport->send($message);
        } else {
            // New logic
            if($notification instanceof NotificationStampsInterface) {
                $envelope = Envelope::wrap($message, $notification->getStamps());
                $this->bus->dispatch($envelope);
            } else {
                $this->bus->dispatch($message);
            }

            // Old logic
            // $this->bus->dispatch($message);
        }
    }
   
}

最后,我们需要通过在 services.yaml

中添加以下内容来覆盖服务
    notifier.channel.sms:
        class: App\Notifier\Channel\SmsChannel
        arguments: ['@texter.transports', '@messenger.default_bus']
        tags:
            - { name: notifier.channel, channel: sms }

就是这样!我们现在有一种方法可以将标记附加到我们的 Notification 对象,该对象将一直传递到 WorkerMessageHandledEvent.

一个示例用法是(至少对于我的情况)

class RelatedEntityStamp implements StampInterface {

    private string $className;
    private int $classId;

    public function __construct(object $entity) {
        $this->className = get_class($entity);
        $this->classId = $entity->getId();
    }

    /**
     * @return string
     */
    public function getClassName(): string {
        return $this->className;
    }

    /**
     * @return int
     */
    public function getClassId(): int {
        return $this->classId;
    }

}
class PostService {

    protected NotifierInterface $notifier;

    public function ___construct(NotifierInterface $notifier) {
        $this->notifier = $notifier;
    }

    public function sendNotifications(Post $post) {
        $notification = new PostNotification($post);
        $stamp = new RelatedEntityStamp($post);        // Solution
        $notification->addStamp($stamp);               // Solution
        
        $recipients = [];
        foreach($post->getNewsFeed()->getSubscribers() as $user) {
            $recipients[] = new Recipient($user->getEmail(), $user->getMobilePhone());
        }

        $this->notifier->send($notification, ...$recipients);
    }

}

消息发送后,转储结果显示我们确实在事件触发点注册了戳记。

^ Symfony\Component\Messenger\Event\WorkerMessageHandledEvent^ {#1078
  -envelope: Symfony\Component\Messenger\Envelope^ {#1103
    -stamps: array:8 [
      "App\Notification\Stamp\RelatedEntityStamp" => array:1 [
        0 => App\Notification\Stamp\RelatedEntityStamp^ {#1062
          -className: "App\Entity\Post"
          -classId: 207
        }
      ]
      "Symfony\Component\Messenger\Stamp\BusNameStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\BusNameStamp^ {#1063
          -busName: "messenger.bus.default"
        }
      ]
      "Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp" => array:1 [
        0 => Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineReceivedStamp^ {#1066
          -id: "2590"
        }
      ]
      "Symfony\Component\Messenger\Stamp\TransportMessageIdStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\TransportMessageIdStamp^ {#1067
          -id: "2590"
        }
      ]
      "Symfony\Component\Messenger\Stamp\ReceivedStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\ReceivedStamp^ {#1075
          -transportName: "async"
        }
      ]
      "Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp^ {#1076}
      ]
      "Symfony\Component\Messenger\Stamp\AckStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\AckStamp^ {#1077
          -ack: Closure(Envelope $envelope, Throwable $e = null)^ {#1074
            class: "Symfony\Component\Messenger\Worker"
            this: Symfony\Component\Messenger\Worker {#632 …}
            use: {
              $transportName: "async"
              $acked: & false
            }
          }
        }
      ]
      "Symfony\Component\Messenger\Stamp\HandledStamp" => array:1 [
        0 => Symfony\Component\Messenger\Stamp\HandledStamp^ {#1101
          -result: Symfony\Component\Notifier\Message\SentMessage^ {#1095
            -original: Symfony\Component\Notifier\Message\NullMessage^ {#1091
              -decoratedMessage: Symfony\Component\Notifier\Message\SmsMessage^ {#1060
                -transport: null
                -subject: ".................................................."
                -phone: "0412345678"
              }
            }
            -transport: "null"
            -messageId: null
          }
          -handlerName: "Symfony\Component\Notifier\Messenger\MessageHandler::__invoke"
        }
      ]
    ]
    -message: Symfony\Component\Notifier\Message\SmsMessage^ {#1060}
  }
  -receiverName: "async"
}