Skip to content

Commit 0ea3176

Browse files
committed
feat: add support for official cloudevent sdk
Signed-off-by: Hendrik Heil <[email protected]>
1 parent d17e37e commit 0ea3176

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/lib/PubSub/CloudEvent.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
use Exception;
99
use InvalidArgumentException;
1010
use JetBrains\PhpStorm\ArrayShape;
11+
use JetBrains\PhpStorm\Deprecated;
1112
use LogicException;
1213

1314
/**
1415
* Class CloudEvent
1516
* @package Dapr\PubSub
17+
* @deprecated 1.3.0 Replaced by cloudevents/sdk-php
1618
*/
19+
#[Deprecated(since: '1.3.0', replacement: \CloudEvents\V1\CloudEvent::class)]
1720
class CloudEvent implements IDeserialize
1821
{
1922
/**

src/lib/PubSub/Topic.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Dapr\exceptions\DaprException;
88
use JetBrains\PhpStorm\Deprecated;
99
use Psr\Log\LoggerInterface;
10+
use CloudEvents\V1\CloudEventInterface;
11+
use CloudEvents\Serializers\JsonSerializer;
1012

1113
/**
1214
* Class Topic
@@ -25,7 +27,7 @@ public function __construct(
2527
/**
2628
* Publish an event to the topic
2729
*
28-
* @param CloudEvent|mixed $event The event to publish
30+
* @param CloudEventInterface|CloudEvent|mixed $event The event to publish
2931
* @param array|null $metadata Additional metadata to pass to the component
3032
* @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent
3133
*
@@ -38,13 +40,17 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t
3840
} elseif ($this->client instanceof NewClient) {
3941
$this->client->logger->debug('Sending {event} to {topic}', ['event' => $event, 'topic' => $this->topic]);
4042
}
41-
if ($event instanceof CloudEvent) {
43+
if ($event instanceof CloudEvent || $event instanceof CloudEventInterface) {
4244
$content_type = 'application/cloudevents+json';
4345
$this->client->extra_headers = [
4446
'Content-Type: application/cloudevents+json',
4547
];
4648

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+
}
4854
}
4955

5056
if ($this->client instanceof DaprClient) {

0 commit comments

Comments
 (0)