Skip to content

Commit 0dcaf1b

Browse files
authored
feat: add Twig helper generating hub URLs and setting authorization cookies (#57)
1 parent 976062f commit 0dcaf1b

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/.php_cs.cache
2-
/.php_cs
1+
/.php-cs-fixer.cache
2+
/.php-cs-fixer.php
33
/.phpunit.result.cache
44
/composer.phar
55
/composer.lock

.php_cs.dist renamed to .php-cs-fixer.dist.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$finder = PhpCsFixer\Finder::create()->in(__DIR__);
1313

14-
return PhpCsFixer\Config::create()
14+
return (new PhpCsFixer\Config)
1515
->setRiskyAllowed(true)
1616
->setRules([
1717
'@Symfony' => true,
@@ -29,25 +29,13 @@
2929
],
3030
'modernize_types_casting' => true,
3131
'native_function_invocation' => true,
32-
'no_extra_consecutive_blank_lines' => [
33-
'break',
34-
'continue',
35-
'curly_brace_block',
36-
'extra',
37-
'parenthesis_brace_block',
38-
'return',
39-
'square_brace_block',
40-
'throw',
41-
'use',
42-
],
4332
'no_useless_else' => true,
4433
'no_useless_return' => true,
4534
'ordered_imports' => true,
4635
'phpdoc_add_missing_param_annotation' => [
4736
'only_untyped' => true,
4837
],
4938
'phpdoc_order' => true,
50-
'psr4' => true,
5139
'semicolon_after_instruction' => true,
5240
'strict_comparison' => true,
5341
'strict_param' => true,

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cache:
1515
- $HOME/.composer/cache
1616

1717
before_install:
18-
- if [[ $lint = '1' ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.18.4/php-cs-fixer.phar; fi
18+
- if [[ $lint = '1' ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.0.0/php-cs-fixer.phar; fi
1919
- if [[ $lint = '1' ]]; then wget https://github.com/phpstan/phpstan/releases/download/0.12.82/phpstan.phar; fi
2020

2121
before_script:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/config": "^4.4|^5.0|^6.0",
2222
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
2323
"symfony/http-kernel": "^4.4|^5.0|^6.0",
24-
"symfony/mercure": "^0.5.3",
24+
"symfony/mercure": "^0.5.3|^0.6",
2525
"symfony/web-link": "^4.4|^5.0|^6.0"
2626
},
2727
"autoload": {

src/DependencyInjection/MercureExtension.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Mercure\Debug\TraceableHub;
2828
use Symfony\Component\Mercure\Debug\TraceablePublisher;
2929
use Symfony\Component\Mercure\Discovery;
30+
use Symfony\Component\Mercure\EventSubscriber\SetCookieSubscriber;
3031
use Symfony\Component\Mercure\Hub;
3132
use Symfony\Component\Mercure\HubInterface;
3233
use Symfony\Component\Mercure\HubRegistry;
@@ -40,9 +41,11 @@
4041
use Symfony\Component\Mercure\Messenger\UpdateHandler;
4142
use Symfony\Component\Mercure\Publisher;
4243
use Symfony\Component\Mercure\PublisherInterface;
44+
use Symfony\Component\Mercure\Twig\MercureExtension as TwigMercureExtension;
4345
use Symfony\Component\Stopwatch\Stopwatch;
4446
use Symfony\UX\Turbo\Bridge\Mercure\Broadcaster;
4547
use Symfony\UX\Turbo\Bridge\Mercure\TurboStreamListenRenderer;
48+
use Twig\Environment;
4649

4750
/**
4851
* @author Kévin Dunglas <[email protected]>
@@ -255,10 +258,21 @@ public function load(array $configs, ContainerBuilder $container)
255258
->addArgument($config['default_cookie_lifetime'])
256259
;
257260

258-
$container->register(Discovery::class, Discovery::class)
261+
$container->register(Discovery::class)
259262
->addArgument(new Reference(HubRegistry::class))
260263
;
261264

265+
if (class_exists(SetCookieSubscriber::class)) {
266+
$container->register(SetCookieSubscriber::class)
267+
->addTag('kernel.event_subscriber', ['priority' => -10]);
268+
}
269+
270+
if (class_exists(TwigMercureExtension::class) && class_exists(Environment::class)) {
271+
$container->register(TwigMercureExtension::class)
272+
->setArguments([new Reference(HubRegistry::class), new Reference(Authorization::class), new Reference('request_stack')])
273+
->addTag('twig.extension');
274+
}
275+
262276
// TODO: remove these parameters in the next release.
263277
$container->setParameter('mercure.hubs', $hubUrls);
264278
$container->setParameter('mercure.default_hub', $defaultHubUrl);

0 commit comments

Comments
 (0)