Skip to content

Commit eb14abc

Browse files
authored
upgrade to symfony/mercure 0.5 (#42)
* upgrade to symfony/mercure 0.5 * rename 'token' configuration to 'jwt' * fix coding standards * test link header utils service * flip subscribe and publish arguments * add support for public url * add authorization utils * add test for creating authorization cookie for external public url. * move hub to the last argument * update branch alias * add mercure class * update with the latest changes from mercure component
1 parent 7a50672 commit eb14abc

File tree

7 files changed

+363
-52
lines changed

7 files changed

+363
-52
lines changed

.travis.yml

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

1717
before_install:
18+
- if [[ $lint = '1' ]]; then composer require --no-update --dev lcobucci/jwt:^4.0; fi
1819
- if [[ $lint = '1' ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.3/php-cs-fixer.phar; fi
1920
- if [[ $lint = '1' ]]; then wget https://github.com/phpstan/phpstan/releases/download/0.12.25/phpstan.phar; fi
2021

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"symfony/config": "^4.4|^5.0",
2121
"symfony/dependency-injection": "^4.4|^5.0",
2222
"symfony/http-kernel": "^4.4|^5.0",
23-
"symfony/mercure": "^0.3|^0.4"
23+
"symfony/mercure": "^0.5",
24+
"symfony/web-link": "^4.4|^5.0"
2425
},
2526
"autoload": {
2627
"psr-4": { "Symfony\\Bundle\\MercureBundle\\": "src/" }
@@ -30,7 +31,7 @@
3031
},
3132
"extra": {
3233
"branch-alias": {
33-
"dev-main": "0.2.x-dev"
34+
"dev-main": "0.3.x-dev"
3435
}
3536
},
3637
"config": {

src/DataCollector/MercureDataCollector.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
19+
use Symfony\Component\Mercure\Debug\TraceableHub;
1920
use Symfony\Component\Mercure\Debug\TraceablePublisher;
2021

2122
final class MercureDataCollector extends DataCollector
2223
{
23-
private $publishers;
24+
private $hubs;
2425

2526
/**
26-
* @var TraceablePublisher[]
27+
* @var TraceablePublisher[]|TraceableHub[]
2728
*/
28-
public function __construct(iterable $publishers)
29+
public function __construct(iterable $hubs)
2930
{
30-
$this->publishers = $publishers;
31+
$this->hubs = $hubs;
3132
}
3233

3334
public function collect(Request $request, Response $response, \Throwable $exception = null): void
@@ -39,17 +40,17 @@ public function collect(Request $request, Response $response, \Throwable $except
3940
'publishers' => [],
4041
];
4142

42-
foreach ($this->publishers as $name => $publisher) {
43-
$this->data['publishers'][$name] = [
44-
'count' => $publisher->count(),
45-
'duration' => $publisher->getDuration(),
46-
'memory' => $publisher->getMemory(),
47-
'messages' => $publisher->getMessages(),
43+
foreach ($this->hubs as $name => $hub) {
44+
$this->data['hubs'][$name] = [
45+
'count' => $hub->count(),
46+
'duration' => $hub->getDuration(),
47+
'memory' => $hub->getMemory(),
48+
'messages' => $hub->getMessages(),
4849
];
4950

50-
$this->data['duration'] += $publisher->getDuration();
51-
$this->data['memory'] += $publisher->getMemory();
52-
$this->data['count'] += \count($publisher->getMessages());
51+
$this->data['duration'] += $hub->getDuration();
52+
$this->data['memory'] += $hub->getMemory();
53+
$this->data['count'] += \count($hub->getMessages());
5354
}
5455
}
5556

@@ -78,8 +79,18 @@ public function getMemory(): int
7879
return $this->data['memory'];
7980
}
8081

82+
public function getHubs(): iterable
83+
{
84+
return $this->data['hubs'];
85+
}
86+
87+
/**
88+
* @deprecated use {@see MercureDataCollector::getHubs()} instead
89+
*/
8190
public function getPublishers(): iterable
8291
{
83-
return $this->data['publishers'];
92+
trigger_deprecation('symfony/mercure-bundle', '0.3', 'Method "%s::getPublishers()" is deprecated, use "%s::getHubs()" instead.', __CLASS__, __CLASS__);
93+
94+
return $this->getHubs();
8495
}
8596
}

src/DependencyInjection/Configuration.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,49 @@ public function getConfigTreeBuilder(): TreeBuilder
3939
->arrayPrototype()
4040
->children()
4141
->scalarNode('url')->info('URL of the hub\'s publish endpoint')->example('https://demo.mercure.rocks/.well-known/mercure')->end()
42-
->scalarNode('jwt')->info('JSON Web Token to use to publish to this hub.')->end()
43-
->scalarNode('jwt_provider')->info('The ID of a service to call to generate the JSON Web Token.')->end()
42+
->scalarNode('public_url')->info('URL of the hub\'s public endpoint')->example('https://demo.mercure.rocks/.well-known/mercure')->defaultNull()->end()
43+
->arrayNode('jwt')
44+
->beforeNormalization()
45+
->ifString()
46+
->then(static function (string $token): array {
47+
return [
48+
'value' => $token,
49+
];
50+
})
51+
->end()
52+
->info('JSON Web Token configuration.')
53+
->children()
54+
->scalarNode('value')->info('JSON Web Token to use to publish to this hub.')->end()
55+
->scalarNode('provider')->info('The ID of a service to call to provide the JSON Web Token.')->end()
56+
->scalarNode('factory')->info('The ID of a service to call to create the JSON Web Token.')->end()
57+
->arrayNode('publish')
58+
->beforeNormalization()->castToArray()->end()
59+
->scalarPrototype()->end()
60+
->info('A list of topics to allow publishing to when using the given factory to generate the JWT.')
61+
->end()
62+
->arrayNode('subscribe')
63+
->beforeNormalization()->castToArray()->end()
64+
->scalarPrototype()->end()
65+
->info('A list of topics to allow subscribing to when using the given factory to generate the JWT.')
66+
->end()
67+
->scalarNode('secret')->info('The JWT Secret to use.')->example('!ChangeMe!')->end()
68+
->end()
69+
->end()
70+
->scalarNode('jwt_provider')
71+
->info('The ID of a service to call to generate the JSON Web Token.')
72+
->setDeprecated('symfony/mercure-bundle', '0.3', 'The child node "%node%" at path "%path%" is deprecated, use "jwt.provider" instead.')
73+
->end()
4474
->scalarNode('bus')->info('Name of the Messenger bus where the handler for this hub must be registered. Default to the default bus if Messenger is enabled.')->end()
4575
->end()
4676
->validate()
47-
->ifTrue(function ($v) { return isset($v['jwt']) && isset($v['jwt_provider']); })
77+
->ifTrue(function ($v) { return isset($v['jwt'], $v['jwt_provider']); })
4878
->thenInvalid('"jwt" and "jwt_provider" cannot be used together.')
79+
->ifTrue(function ($v) { return !isset($v['jwt']) && !isset($v['jwt_provider']); })
80+
->thenInvalid('You must specify at least one of "jwt", and "jwt_provider".')
81+
->ifTrue(function ($v) { return isset($v['jwt']['value'], $v['jwt']['provider']); })
82+
->thenInvalid('"jwt.value" and "jwt.provider" cannot be used together.')
83+
->ifTrue(function ($v) { return isset($v['jwt']) && !isset($v['jwt']['value']) && !isset($v['jwt']['provider']) && !isset($v['jwt']['factory']) && !isset($v['jwt']['secret']); })
84+
->thenInvalid('You must specify at least one of "jwt.value", "jwt.provider", "jwt.factory", and "jwt.secret".')
4985
->end()
5086
->end()
5187
->end()

0 commit comments

Comments
 (0)