-
-
Notifications
You must be signed in to change notification settings - Fork 44
feat: add Twig helper generating hub URLs and setting authorization cookies #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8696e6
feat: add Twig helper generating hub URLs and setting authorization c…
dunglas b5d97a3
fix: support for multiple hubs
dunglas af9df06
clean request attributes
dunglas 5cd7372
fix @chalasr's comments
dunglas 1e361dd
add tests
dunglas c77729c
compat with PHP 7.1
dunglas ab2b33e
fix tests
dunglas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Mercure Component project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Mercure\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Sets the cookies created by the Authorization helper class. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class SetCookieSubscriber implements EventSubscriberInterface | ||
{ | ||
public function onKernelResponse(ResponseEvent $event): void | ||
{ | ||
$mainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest(); | ||
if ( | ||
!($mainRequest) || | ||
null === $cookies = ($request = $event->getRequest())->attributes->get('_mercure_authorization_cookies')) { | ||
return; | ||
} | ||
|
||
$request->attributes->remove('_mercure_authorization_cookies'); | ||
|
||
$response = $event->getResponse(); | ||
foreach ($cookies as $cookie) { | ||
$response->headers->setCookie($cookie); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::RESPONSE => 'onKernelResponse']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Mercure Component project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Mercure\Twig; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Mercure\Authorization; | ||
use Symfony\Component\Mercure\HubRegistry; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* Registers the Twig helper function. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class MercureExtension extends AbstractExtension | ||
{ | ||
private $hubRegistry; | ||
private $authorization; | ||
private $requestStack; | ||
|
||
public function __construct(HubRegistry $hubRegistry, ?Authorization $authorization = null, ?RequestStack $requestStack = null) | ||
{ | ||
$this->hubRegistry = $hubRegistry; | ||
$this->authorization = $authorization; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [new TwigFunction('mercure', [$this, 'mercure'])]; | ||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @param string|string[]|null $topics A topic or an array of topics to subscribe for. If this parameter is omitted or `null` is passed, the URL of the hub will be returned (useful for publishing in JavaScript). | ||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @return string The URL of the hub with the appropriate "topic" query parameters (if any) | ||
*/ | ||
public function mercure($topics = null, array $options = []): string | ||
{ | ||
$hub = $options['hub'] ?? null; | ||
$url = $this->hubRegistry->getHub($hub)->getPublicUrl(); | ||
if (null !== $topics) { | ||
// We cannot use http_build_query() because this method doesn't support generating multiple query parameters with the same name without the [] suffix | ||
$separator = '?'; | ||
foreach ((array) $topics as $topic) { | ||
$url .= $separator.'topic='.rawurlencode($topic); | ||
if ('?' === $separator) { | ||
$separator = '&'; | ||
} | ||
} | ||
} | ||
|
||
if ( | ||
null === $this->authorization || | ||
null === $this->requestStack || | ||
(!isset($options['subscribe']) && !isset($options['publish']) && !isset($options['additionalClaims'])) || | ||
/* @phpstan-ignore-next-line */ | ||
null === $request = method_exists($this->requestStack, 'getMainRequest') ? $this->requestStack->getMainRequest() : $this->requestStack->getMasterRequest() | ||
) { | ||
return $url; | ||
} | ||
|
||
$this->authorization->setCookie($request, $options['subscribe'] ?? [], $options['publish'] ?? [], $options['additionalClaims'] ?? [], $hub); | ||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return $url; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.