@@ -159,20 +159,26 @@ service, including controllers::
159159 namespace App\Controller;
160160
161161 use Symfony\Component\HttpFoundation\Response;
162- use Symfony\Component\Mercure\PublisherInterface ;
162+ use Symfony\Component\Mercure\HubInterface ;
163163 use Symfony\Component\Mercure\Update;
164164
165165 class PublishController
166166 {
167- public function __invoke(PublisherInterface $publisher): Response
167+ private $hub;
168+
169+ public function __construct(HubInterface $hub)
170+ {
171+ $this->hub = $hub;
172+ }
173+
174+ public function __invoke(): Response
168175 {
169176 $update = new Update(
170177 'http://example.com/books/1',
171178 json_encode(['status' => 'OutOfStock'])
172179 );
173180
174- // The Publisher service is an invokable object
175- $publisher($update);
181+ $this->hub->publish($update);
176182
177183 return new Response('published!');
178184 }
@@ -294,22 +300,25 @@ by using the ``AbstractController::addLink`` helper method::
294300 // src/Controller/DiscoverController.php
295301 namespace App\Controller;
296302
297- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
298303 use Symfony\Component\HttpFoundation\JsonResponse;
299304 use Symfony\Component\HttpFoundation\Request;
300- use Symfony\Component\WebLink\Link ;
305+ use Symfony\Component\Mercure\Discovery ;
301306
302- class DiscoverController extends AbstractController
307+ class DiscoverController
303308 {
304- public function __invoke(Request $request): JsonResponse
309+ private $discovery;
310+
311+ public function __construct(Discovery $discovery)
305312 {
306- // This parameter is automatically created by the MercureBundle
307- $hubUrl = $this->getParameter('mercure.default_hub');
313+ $this->discovery = $discovery;
314+ }
308315
316+ public function __invoke(Request $request): JsonResponse
317+ {
309318 // Link: <http://localhost:3000/.well-known/mercure>; rel="mercure"
310- $this->addLink($request, new Link('mercure', $hubUrl) );
319+ $this->discovery-> addLink($request);
311320
312- return $this->json ([
321+ return new JsonResponse ([
313322 '@id' => '/books/1',
314323 'availability' => 'https://schema.org/InStock',
315324 ]);
@@ -347,12 +356,18 @@ of the ``Update`` constructor to ``true``::
347356 namespace App\Controller;
348357
349358 use Symfony\Component\HttpFoundation\Response;
350- use Symfony\Component\Mercure\PublisherInterface;
351359 use Symfony\Component\Mercure\Update;
352360
353361 class PublishController
354362 {
355- public function __invoke(PublisherInterface $publisher): Response
363+ private $hub;
364+
365+ public function __construct(HubInterface $hub)
366+ {
367+ $this->hub = $hub;
368+ }
369+
370+ public function __invoke(): Response
356371 {
357372 $update = new Update(
358373 'http://example.com/books/1',
@@ -362,7 +377,7 @@ of the ``Update`` constructor to ``true``::
362377
363378 // Publisher's JWT must contain this topic, a URI template it matches or * in mercure.publish or you'll get a 401
364379 // Subscriber's JWT must contain this topic, a URI template it matches or * in mercure.subscribe to receive the update
365- $publisher ($update);
380+ $this->hub->publish ($update);
366381
367382 return new Response('private update published!');
368383 }
@@ -417,39 +432,34 @@ And here is the controller::
417432 // src/Controller/DiscoverController.php
418433 namespace App\Controller;
419434
420- use Lcobucci\JWT\Configuration;
421- use Lcobucci\JWT\Signer\Hmac\Sha256;
422- use Lcobucci\JWT\Signer\Key;
423- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
424- use Symfony\Component\HttpFoundation\Cookie;
425435 use Symfony\Component\HttpFoundation\Request;
426436 use Symfony\Component\HttpFoundation\Response;
427- use Symfony\Component\WebLink\Link;
437+ use Symfony\Component\Mercure\Authorization;
438+ use Symfony\Component\Mercure\Discovery;
428439
429- class DiscoverController extends AbstractController
440+ class DiscoverController
430441 {
442+ private $discovery;
443+ private $authorization;
444+
445+ public function __construct(Discovery $discovery, Authorization $authorization)
446+ {
447+ $this->discovery = $discovery;
448+ $this->authorization = $authorization;
449+ }
450+
431451 public function __invoke(Request $request): Response
432452 {
433- $hubUrl = $this->getParameter('mercure.default_hub');
434- $this->addLink($request, new Link('mercure', $hubUrl));
435-
436- $key = Key\InMemory::plainText('mercure_secret_key'); // don't forget to set this parameter! Test value: !ChangeMe!
437- $configuration = Configuration::forSymmetricSigner(new Sha256(), $key);
438-
439- $token = $configuration->builder()
440- ->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
441- ->getToken($configuration->signer(), $configuration->signingKey())
442- ->toString();
443-
444- $response = $this->json(['@id' => '/demo/books/1', 'availability' => 'https://schema.org/InStock']);
445- $cookie = Cookie::create('mercureAuthorization')
446- ->withValue($token)
447- ->withPath('/.well-known/mercure')
448- ->withSecure(true)
449- ->withHttpOnly(true)
450- ->withSameSite('strict')
451- ;
452- $response->headers->setCookie($cookie);
453+ $this->discovery->addLink($request);
454+
455+ $response = new JsonResponse([
456+ '@id' => '/demo/books/1',
457+ 'availability' => 'https://schema.org/InStock'
458+ ]);
459+
460+ $response->headers->setCookie(
461+ $this->authorization->createCookie($request, ["http://example.com/books/1"])
462+ );
453463
454464 return $response;
455465 }
@@ -464,15 +474,17 @@ Programmatically Generating The JWT Used to Publish
464474---------------------------------------------------
465475
466476Instead of directly storing a JWT in the configuration,
467- you can create a service that will return the token used by
468- the ``Publisher `` object::
477+ you can create a token provider that will return the token used by
478+ the ``HubInterface `` object::
469479
470- // src/Mercure/MyJwtProvider .php
480+ // src/Mercure/MyTokenProvider .php
471481 namespace App\Mercure;
472482
473- final class MyJwtProvider
483+ use Symfony\Component\Mercure\JWT\TokenProviderInterface;
484+
485+ final class MyTokenProvider implements TokenProviderInterface
474486 {
475- public function __invoke (): string
487+ public function getToken (): string
476488 {
477489 return 'the-JWT';
478490 }
@@ -489,7 +501,8 @@ Then, reference this service in the bundle configuration:
489501 hubs :
490502 default :
491503 url : https://mercure-hub.example.com/.well-known/mercure
492- jwt_provider : App\Mercure\MyJwtProvider
504+ jwt :
505+ provider : App\Mercure\MyTokenProvider
493506
494507 .. code-block :: xml
495508
@@ -499,8 +512,9 @@ Then, reference this service in the bundle configuration:
499512 <hub
500513 name =" default"
501514 url =" https://mercure-hub.example.com/.well-known/mercure"
502- jwt-provider =" App\Mercure\MyJwtProvider"
503- />
515+ >
516+ <jwt provider =" App\Mercure\MyTokenProvider" />
517+ </hub >
504518 </config >
505519
506520 .. code-block :: php
@@ -512,7 +526,9 @@ Then, reference this service in the bundle configuration:
512526 'hubs' => [
513527 'default' => [
514528 'url' => 'https://mercure-hub.example.com/.well-known/mercure',
515- 'jwt_provider' => MyJwtProvider::class,
529+ 'jwt' => [
530+ 'provider' => MyJwtProvider::class,
531+ ]
516532 ],
517533 ],
518534 ]);
0 commit comments