iquety/pubsub

实现发布/订阅架构模式的库

v1.0.1 2024-09-16 14:25 UTC

This package is auto-updated.

Last update: 2024-09-16 14:27:28 UTC


README

PHP Version License Codacy Grade Codacy Coverage

摘要

此存储库包含运行发布/订阅系统的必要功能,使您可以订阅和发布事件。

composer require iquety/pubsub

有关详细信息,请参阅文档摘要

工作原理

1. 运行事件服务器

在此存储库的根目录中,有一个终端命令来演示此过程。可以使用以下方式使用

# running in local PHP
./example pubsub:broker -c 'tests/Example/config-file.php' -v

ou

# running in Dockerized PHP
./composer pubsub-server
# Server terminal running

➜ Using JsonEventSerializer serializer
➜ Verbose mode enabled
✔ The publish/subscriber server has been started in tcp://:7703

准备好了!事件服务器正在端口7703上运行,将可用于接收事件。

注意:在服务器运行期间,当前终端将处于“监视”模式,等待接收事件。"-v"选项激活了“详细”模式,因此每当接收到事件时,服务器都会通知正在运行的终端。

3. 向服务器发送事件

服务器运行时,只需打开另一个终端,并使用以下命令发送一些测试事件

# running in local PHP
./example pubsub:client-test -d localhost -p 7703 -v

ou

# running in Dockerized PHP
./composer pubsub-client

两个终端将对事件的发送做出反应,显示相应的信息

# Terminal where events were sent

✔ Publish event of type 'EventOne' to channel 'channel-vormir' in tcp://:7703
✔ Publish event of type 'EventTwo' to channel 'channel-vormir' in tcp://:7703
✔ Publish event of type 'EventTwo' to channel 'channel-mordor' in tcp://:7703
✔ Publish event of type 'EventTwo' to channel 'channel-greenville' in tcp://:7703
➜ Published Events
➜ Used memory: 1921928
# Server terminal that received the events

➜ [2022-06-03 17:06:09]: Message of type 'EventOne' received on channel 'channel-vormir'
Message dispatched to SubscriberOne
Message dispatched to SubscriberTwo

➜ [2022-06-03 17:06:09]: Message of type 'EventTwo' received on channel 'channel-vormir'
Message dispatched to SubscriberTwo

...

特性

  • 适用于PHP 8.3或更高版本;
  • 使用最佳实践和最高质量编写;
  • 文档齐全且IDE友好;
  • 使用TDD(测试驱动开发);
  • 使用PHPUnit进行单元测试实现;
  • 用❤️ & ☕制作。

致谢

Ricardo Pereira Dias