ibanawx/prooph-event-store-rest-api-bundle

通过REST API使Prooph Event Store流可访问。

v1.0.0 2016-10-26 16:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:04:04 UTC


README

1. 下载

composer require ibanawx/prooph-event-store-rest-api-bundle

2. 包注册

在您的Symfony内核中注册以下包

  1. Prooph\Bundle\EventStore\ProophEventStoreBundle
  2. Ibanawx\Bundle\Prooph\EventStore\RestApiBundle\ProophEventStoreRestApiBundle

3. 导入路由

# app/config/routing.yml

prooph_event_store:
  resource: '@ProophEventStoreRestApiBundle/Resources/config/routing.yml'
  prefix: /event-store

上面的 prefix 选项是可选的,但如果您想将此包的路由与应用程序中的其他路由分开,则非常有用。

配置

# app/config/config.yml

prooph_event_store:
  stores:
    default:
      adapter: event_store_adapter_service_id

prooph_event_store_rest_api:
  event_store:
    name: default
  formatters:
    stream: ~
    event: ~

有关配置 prooph_event_store 包的更多信息,请点击这里

prooph_event_store_rest_api

用法

所有对REST API的HTTP请求都必须包含至少一个指定您接受的内容的 Accept 头。接受的内容类型直接与格式化程序的输出内容类型相关。

获取流

请求占位符

  • streamName:流的名称。
  • minVersion:将在流中出现的最小事件版本。

成功响应

默认情况下,将返回一个 application/json 内容类型的响应。这是因为默认的格式化程序将流格式化为JSON。

默认的JSON格式化程序紧密遵循Atom聚合格式,但并不完全匹配。

假设您有一个具有 0 个事件的 user 流。向 http://site.com/streams/user/0 发送GET请求将返回以下内容

{
  "id": "http://site.com/streams/user/0",
  "title": "user stream",
  "links": [],
  "entries": []
}

假设您有一个具有 2 个事件的 user 流。向 http://site.com/streams/user/0 发送GET请求将返回以下内容

{
  "id": "http://site.com/streams/user/0",
  "title": "user stream",
  "links": [
    {
      "uri": "http://site.com/streams/user/2",
      "relation": "next"
    }
  ],
  "entries": [
    {
      "id": "http://site.com/streams/user/events/0",
      "title": "0@user",
      "updated": "2016-10-25 21:41:03",
      "content": {
        "id": "44c552d3-0868-4e36-9b9b-160bad558d89",
        "name": "UserSignedUp",
        "version": 0,
        "metadata": {},
        "createdAt": "2016-10-25 21:41:03",
        "payload": {}
      }
    },
    {
      "id": "http://site.com/streams/user/events/1",
      "title": "1@user",
      "updated": "2016-10-25 21:43:56",
      "content": {
        "id": "e902f95f-60d0-4bc5-afcf-13eddc6eed23",
        "name": "UserSignedUp",
        "version": 1,
        "metadata": {},
        "createdAt": "2016-10-25 21:43:56",
        "payload": {}
      }
    }
  ]
}

流导航

默认的JSON表示形式允许您使用超媒体链接在流中导航。

如果 user 流包含42个事件(版本0-41),向 http://site.com/streams/user/0 发送GET请求将返回以下 next 链接

{
  "uri": "http://site.com/streams/user/42",
  "relation": "next"
}

要获取此流中的下一个事件,您将轮询 next URI。

如果没有您指定的最小版本的事件,流中将不存在 next 链接。

获取流事件

请求占位符

  • streamName:流的名称。
  • version:事件的版本。

自定义

格式化程序

格式化程序接受一个流或一个事件,并将其格式化为字符串,然后将其作为响应体发送回去。

流格式化程序

获取流时,将调用此格式化程序。

  1. 实现 Ibanawx\Bundle\Prooph\EventStore\RestApiBundle\Formatter\StreamFormatter
  2. 将自定义格式化程序定义为依赖注入容器中的服务。
  3. 在您的 app/config/config.yml 文件中将流格式化程序设置为服务ID。

需要实现的方法

  • getOutputContentType():返回一个字符串,指定格式化程序返回的数据的内容类型。
  • format():返回流的字符串表示形式。

流事件格式化程序

获取单个流事件时,将调用此格式化器。

  1. 实现Ibanawx\Bundle\Prooph\EventStore\RestApiBundle\Formatter\StreamEventFormatter
  2. 将自定义格式化程序定义为依赖注入容器中的服务。
  3. 将流事件格式化器设置到您的app/config/config.yml文件中的服务ID。

需要实现的方法

  • getOutputContentType():返回一个字符串,指定格式化程序返回的数据的内容类型。
  • format():返回流事件的字符串表示形式。