Skip to content

Commit ea5cb9b

Browse files
committed
Improve readme
1 parent 4989736 commit ea5cb9b

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,19 @@ public function initialize(): void
980980
| `addTopic(string $topic)` | `$this` | Add a topic for the view to subscribe to |
981981
| `addTopics(array $topics)` | `$this` | Add multiple topics for the view |
982982
| `getTopics()` | `array` | Get all topics added in the component |
983-
| `authorize(array $subscribe, array $additionalClaims)` | `$this` | Set authorization cookie |
983+
| `resetTopics()` | `$this` | Reset all accumulated topics |
984+
| `addSubscribe(string $topic, array $additionalClaims = [])` | `$this` | Add a topic to authorize with optional JWT claims |
985+
| `addSubscribes(array $topics, array $additionalClaims = [])` | `$this` | Add multiple topics to authorize with optional JWT claims |
986+
| `getSubscribe()` | `array` | Get accumulated subscribe topics |
987+
| `getAdditionalClaims()` | `array` | Get accumulated JWT claims |
988+
| `resetSubscribe()` | `$this` | Reset accumulated subscribe topics |
989+
| `resetAdditionalClaims()` | `$this` | Reset accumulated JWT claims |
990+
| `authorize(array $subscribe = [], array $additionalClaims = [])` | `$this` | Set authorization cookie (merges with accumulated state, then resets) |
984991
| `clearAuthorization()` | `$this` | Clear authorization cookie |
985992
| `discover()` | `$this` | Add Mercure discovery Link header |
986993
| `publish(Update $update)` | `bool` | Publish an update to the Mercure hub |
987994
| `publishJson(string\|array $topics, mixed $data, ...)` | `bool` | Publish JSON data (auto-encodes) |
995+
| `publishSimple(string\|array $topics, string $data, ...)` | `bool` | Publish simple string data (no encoding) |
988996
| `publishView(string\|array $topics, ?string $template, ?string $element, array $data, ...)` | `bool` | Publish rendered view/element |
989997
| `getCookieName()` | `string` | Get the cookie name |
990998
@@ -1012,15 +1020,39 @@ const url = '<?= $this->Mercure->url() ?>';
10121020
// Subscribes to: /books/123 and /user/456/updates (from component)
10131021
```
10141022
1015-
**Authorization methods** support fluent chaining:
1023+
**Authorization Builder Pattern:**
1024+
1025+
Build up authorization topics and claims fluently, then call `authorize()`:
10161026
10171027
```php
1028+
// Build up gradually with claims
1029+
$this->Mercure
1030+
->addSubscribe('/books/123', ['sub' => $userId])
1031+
->addSubscribe('/notifications/*', ['role' => 'admin'])
1032+
->authorize()
1033+
->discover();
1034+
1035+
// Add multiple at once
1036+
$this->Mercure->addSubscribes(
1037+
['/books/123', '/notifications/*'],
1038+
['sub' => $userId, 'role' => 'admin']
1039+
);
1040+
1041+
// Mix builder and direct parameters
1042+
$this->Mercure
1043+
->addSubscribe('/books/123')
1044+
->authorize(['/notifications/*'], ['sub' => $userId]);
1045+
1046+
// Chain with topic management
10181047
$this->Mercure
1019-
->addTopics(['/books/123', '/notifications'])
1020-
->authorize(['/feeds/123'])
1048+
->addTopic('/books/123') // For EventSource
1049+
->addSubscribe('/books/123', ['sub' => $userId]) // For authorization
1050+
->authorize()
10211051
->discover();
10221052
```
10231053
1054+
Claims accumulate across multiple `addSubscribe()` calls. The `authorize()` method automatically resets accumulated state after setting the cookie.
1055+
10241056
**Publishing convenience methods** make it easy to publish updates directly from controllers:
10251057
10261058
```php

0 commit comments

Comments
 (0)