7
7
use Dapr \exceptions \DaprException ;
8
8
use JetBrains \PhpStorm \Deprecated ;
9
9
use Psr \Log \LoggerInterface ;
10
+ use CloudEvents \V1 \CloudEventInterface ;
11
+ use CloudEvents \Serializers \JsonSerializer ;
10
12
11
13
/**
12
14
* Class Topic
@@ -25,7 +27,7 @@ public function __construct(
25
27
/**
26
28
* Publish an event to the topic
27
29
*
28
- * @param CloudEvent|mixed $event The event to publish
30
+ * @param CloudEventInterface| CloudEvent|mixed $event The event to publish
29
31
* @param array|null $metadata Additional metadata to pass to the component
30
32
* @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent
31
33
*
@@ -38,13 +40,17 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t
38
40
} elseif ($ this ->client instanceof NewClient) {
39
41
$ this ->client ->logger ->debug ('Sending {event} to {topic} ' , ['event ' => $ event , 'topic ' => $ this ->topic ]);
40
42
}
41
- if ($ event instanceof CloudEvent) {
43
+ if ($ event instanceof CloudEvent || $ event instanceof CloudEventInterface ) {
42
44
$ content_type = 'application/cloudevents+json ' ;
43
45
$ this ->client ->extra_headers = [
44
46
'Content-Type: application/cloudevents+json ' ,
45
47
];
46
48
47
- $ event = $ event ->to_array ();
49
+ if ($ event instanceof CloudEvent) {
50
+ $ event = $ event ->to_array ();
51
+ } elseif ($ event instanceof CloudEventInterface) {
52
+ $ event = json_decode (JsonSerializer::create ()->serializeStructured ($ event ), true );
53
+ }
48
54
}
49
55
50
56
if ($ this ->client instanceof DaprClient) {
0 commit comments