Subscription enumeration, tweaks
This commit is contained in:
@ -5,14 +5,18 @@ namespace NoccyLabs\Mercureact\Broker;
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use React\Stream\WritableStreamInterface;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
class SseSubscriber implements SubscriberInterface
|
||||
{
|
||||
private string $id;
|
||||
|
||||
public function __construct(
|
||||
private WritableStreamInterface $stream,
|
||||
private ServerRequestInterface $request,
|
||||
)
|
||||
{
|
||||
$this->id = (string)Uuid::v7();
|
||||
}
|
||||
|
||||
public function deliver(Message $message): void
|
||||
@ -24,4 +28,9 @@ class SseSubscriber implements SubscriberInterface
|
||||
{
|
||||
return $this->request->getAttribute('authorization') instanceof JWTToken;
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return "urn:uuid:".$this->id;
|
||||
}
|
||||
}
|
@ -14,6 +14,8 @@ class Topic
|
||||
/** @var array<string,Message> */
|
||||
private array $messages = [];
|
||||
|
||||
private ?string $lastEventId = null;
|
||||
|
||||
/** @var int Creation unixtime */
|
||||
private int $created;
|
||||
|
||||
@ -69,6 +71,21 @@ class Topic
|
||||
$this->subscribers->detach($subscriber);
|
||||
}
|
||||
|
||||
public function getSubscribers(): array
|
||||
{
|
||||
return iterator_to_array($this->subscribers);
|
||||
}
|
||||
|
||||
public function getTopic(): string
|
||||
{
|
||||
return $this->topic;
|
||||
}
|
||||
|
||||
public function getLastEventId(): ?string
|
||||
{
|
||||
return $this->lastEventId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collect histry
|
||||
*
|
||||
|
@ -53,6 +53,27 @@ class TopicManager
|
||||
}
|
||||
}
|
||||
|
||||
public function getSubscriptions(): array
|
||||
{
|
||||
$all = [];
|
||||
|
||||
foreach ($this->topics as $topic) {
|
||||
$subs = $topic->getSubscribers();
|
||||
foreach ($subs as $sub) {
|
||||
$all[] = [
|
||||
'id' => './well-known/mercure/subsciptions/'.urlencode($topic->getTopic())."/".urlencode($sub->getId()),
|
||||
'type' => "Subscription",
|
||||
'topic' => $topic->getTopic(),
|
||||
'subscriber' => $sub->getId(),
|
||||
'active' => true,
|
||||
'payload' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
public function getTopicCount(): int
|
||||
{
|
||||
return count($this->topics);
|
||||
|
Reference in New Issue
Block a user