@@ -244,29 +244,26 @@ public function addDiscoveryHeader(
244244
245245 // Add rel="self" link if provided
246246 if ($ selfUrl !== null ) {
247- $ selfLinkHeader = $ this ->buildLinkHeader ($ selfUrl , 'self ' );
248- $ response = $ response ->withAddedHeader ('Link ' , $ selfLinkHeader );
247+ $ response = $ response ->withAddedLink ($ selfUrl , ['rel ' => 'self ' ]);
249248 }
250249
251250 // Build rel="mercure" link with optional attributes
252- $ hubUrl = $ this -> getPublicUrl ();
253- $ attributes = [];
251+ $ hubUrl = ConfigurationHelper:: getPublicUrl ();
252+ $ options = [' rel ' => ' mercure ' ];
254253
255254 if ($ lastEventId !== null ) {
256- $ attributes ['last-event-id ' ] = $ lastEventId ;
255+ $ options ['last-event-id ' ] = $ lastEventId ;
257256 }
258257
259258 if ($ contentType !== null ) {
260- $ attributes ['content-type ' ] = $ contentType ;
259+ $ options ['content-type ' ] = $ contentType ;
261260 }
262261
263262 if ($ keySet !== null ) {
264- $ attributes ['key-set ' ] = $ keySet ;
263+ $ options ['key-set ' ] = $ keySet ;
265264 }
266265
267- $ mercureLinkHeader = $ this ->buildLinkHeader ($ hubUrl , 'mercure ' , $ attributes );
268-
269- return $ response ->withAddedHeader ('Link ' , $ mercureLinkHeader );
266+ return $ response ->withAddedLink ($ hubUrl , $ options );
270267 }
271268
272269 /**
@@ -284,39 +281,4 @@ private function isPreflightRequest(ServerRequest $request): bool
284281 return $ request ->is ('options ' )
285282 && $ request ->hasHeader ('Access-Control-Request-Method ' );
286283 }
287-
288- /**
289- * Get the Mercure public URL from configuration
290- *
291- * This is the client-facing URL for EventSource connections.
292- * Falls back to url if not configured.
293- *
294- * @throws \Mercure\Exception\MercureException
295- */
296- private function getPublicUrl (): string
297- {
298- return ConfigurationHelper::getPublicUrl ();
299- }
300-
301- /**
302- * Build a Link header value according to RFC5988
303- *
304- * Constructs a Link header with the given URL, relation type, and optional attributes.
305- *
306- * @param string $url The URL for the link
307- * @param string $rel The relation type (e.g., "mercure", "self")
308- * @param array<string, string> $attributes Optional link attributes
309- * @return string Formatted Link header value
310- */
311- private function buildLinkHeader (string $ url , string $ rel , array $ attributes = []): string
312- {
313- $ parts = [sprintf ('<%s> ' , $ url )];
314- $ parts [] = sprintf ('rel="%s" ' , $ rel );
315-
316- foreach ($ attributes as $ name => $ value ) {
317- $ parts [] = sprintf ('%s="%s" ' , $ name , $ value );
318- }
319-
320- return implode ('; ' , $ parts );
321- }
322284}
0 commit comments