Skip to content

Commit fc015df

Browse files
authored
Add support for the builtin Hub provided by FrankenPHP (#106)
* Add support for the buitin Hub provided by FrankenPHP * cs * test version * remove local repo config * add test * cs * cs
1 parent fc04b2f commit fc015df

4 files changed

Lines changed: 175 additions & 118 deletions

File tree

composer.json

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

src/DependencyInjection/Configuration.php

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1717
use Symfony\Component\Config\Definition\ConfigurationInterface;
18+
use Symfony\Component\Mercure\FrankenPhpHub;
1819

1920
/**
2021
* MercureExtension configuration structure.
@@ -25,68 +26,83 @@ final class Configuration implements ConfigurationInterface
2526
{
2627
public function getConfigTreeBuilder(): TreeBuilder
2728
{
29+
$builtinPublish = class_exists(FrankenPhpHub::class) && \function_exists('mercure_publish');
30+
2831
$treeBuilder = new TreeBuilder('mercure');
2932
$rootNode = $treeBuilder->getRootNode();
30-
$rootNode
33+
34+
$urlNode = $rootNode
3135
->fixXmlConfig('hub')
3236
->children()
3337
->arrayNode('hubs')
3438
->useAttributeAsKey('name')
3539
->normalizeKeys(false)
3640
->arrayPrototype()
3741
->children()
38-
->scalarNode('url')->info('URL of the hub\'s publish endpoint')->example('https://demo.mercure.rocks/.well-known/mercure')->end()
39-
->scalarNode('public_url')->info('URL of the hub\'s public endpoint')->example('https://demo.mercure.rocks/.well-known/mercure')->defaultNull()->end()
40-
->arrayNode('jwt')
41-
->beforeNormalization()
42-
->ifString()
43-
->then(static function (string $token): array {
44-
return [
45-
'value' => $token,
46-
];
47-
})
48-
->end()
49-
->info('JSON Web Token configuration.')
50-
->children()
51-
->scalarNode('value')->info('JSON Web Token to use to publish to this hub.')->end()
52-
->scalarNode('provider')->info('The ID of a service to call to provide the JSON Web Token.')->end()
53-
->scalarNode('factory')->info('The ID of a service to call to create the JSON Web Token.')->end()
54-
->arrayNode('publish')
55-
->beforeNormalization()->castToArray()->end()
56-
->scalarPrototype()->end()
57-
->info('A list of topics to allow publishing to when using the given factory to generate the JWT.')
58-
->end()
59-
->arrayNode('subscribe')
60-
->beforeNormalization()->castToArray()->end()
61-
->scalarPrototype()->end()
62-
->info('A list of topics to allow subscribing to when using the given factory to generate the JWT.')
63-
->end()
64-
->scalarNode('secret')->info('The JWT Secret to use.')->example('!ChangeMe!')->end()
65-
->scalarNode('passphrase')->info('The JWT secret passphrase.')->defaultValue('')->end()
66-
->scalarNode('algorithm')->info('The algorithm to use to sign the JWT')->defaultValue('hmac.sha256')->end()
67-
->end()
68-
->end()
69-
->scalarNode('jwt_provider')
70-
->info('The ID of a service to call to generate the JSON Web Token.')
71-
->setDeprecated('symfony/mercure-bundle', '0.3', 'The child node "%node%" at path "%path%" is deprecated, use "jwt.provider" instead.')
72-
->end()
73-
->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()
42+
->scalarNode('url')->info('URL of the hub\'s publish endpoint')->example('https://demo.mercure.rocks/.well-known/mercure');
43+
44+
if ($builtinPublish) {
45+
$urlNode->defaultNull();
46+
}
47+
48+
$publicUrlNode = $urlNode->end()
49+
->scalarNode('public_url')->info('URL of the hub\'s public endpoint')->example('https://demo.mercure.rocks/.well-known/mercure');
50+
51+
if (!$builtinPublish) {
52+
$publicUrlNode->defaultNull();
53+
}
54+
55+
$publicUrlNode->end()
56+
->arrayNode('jwt')
57+
->beforeNormalization()
58+
->ifString()
59+
->then(static function (string $token): array {
60+
return [
61+
'value' => $token,
62+
];
63+
})
64+
->end()
65+
->info('JSON Web Token configuration.')
66+
->children()
67+
->scalarNode('value')->info('JSON Web Token to use to publish to this hub.')->end()
68+
->scalarNode('provider')->info('The ID of a service to call to provide the JSON Web Token.')->end()
69+
->scalarNode('factory')->info('The ID of a service to call to create the JSON Web Token.')->end()
70+
->arrayNode('publish')
71+
->beforeNormalization()->castToArray()->end()
72+
->scalarPrototype()->end()
73+
->info('A list of topics to allow publishing to when using the given factory to generate the JWT.')
74+
->end()
75+
->arrayNode('subscribe')
76+
->beforeNormalization()->castToArray()->end()
77+
->scalarPrototype()->end()
78+
->info('A list of topics to allow subscribing to when using the given factory to generate the JWT.')
79+
->end()
80+
->scalarNode('secret')->info('The JWT Secret to use.')->example('!ChangeMe!')->end()
81+
->scalarNode('passphrase')->info('The JWT secret passphrase.')->defaultValue('')->end()
82+
->scalarNode('algorithm')->info('The algorithm to use to sign the JWT')->defaultValue('hmac.sha256')->end()
83+
->end()
84+
->end()
85+
->scalarNode('jwt_provider')
86+
->info('The ID of a service to call to generate the JSON Web Token.')
87+
->setDeprecated('symfony/mercure-bundle', '0.3', 'The child node "%node%" at path "%path%" is deprecated, use "jwt.provider" instead.')
88+
->end()
89+
->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()
7490
->end()
7591
->validate()
76-
->ifTrue(function ($v) { return isset($v['jwt'], $v['jwt_provider']); })
77-
->thenInvalid('"jwt" and "jwt_provider" cannot be used together.')
92+
->ifTrue(function ($v) { return isset($v['jwt'], $v['jwt_provider']); })
93+
->thenInvalid('"jwt" and "jwt_provider" cannot be used together.')
7894
->end()
7995
->validate()
80-
->ifTrue(function ($v) { return !isset($v['jwt']) && !isset($v['jwt_provider']); })
81-
->thenInvalid('You must specify at least one of "jwt", and "jwt_provider".')
96+
->ifTrue(function ($v) { return isset($v['url']) && !isset($v['jwt']) && !isset($v['jwt_provider']); })
97+
->thenInvalid('You must specify at least one of "jwt", and "jwt_provider".')
8298
->end()
8399
->validate()
84-
->ifTrue(function ($v) { return isset($v['jwt']['value'], $v['jwt']['provider']); })
85-
->thenInvalid('"jwt.value" and "jwt.provider" cannot be used together.')
100+
->ifTrue(function ($v) { return isset($v['jwt']['value'], $v['jwt']['provider']); })
101+
->thenInvalid('"jwt.value" and "jwt.provider" cannot be used together.')
86102
->end()
87103
->validate()
88-
->ifTrue(function ($v) { return isset($v['jwt']) && !isset($v['jwt']['value']) && !isset($v['jwt']['provider']) && !isset($v['jwt']['factory']) && !isset($v['jwt']['secret']); })
89-
->thenInvalid('You must specify at least one of "jwt.value", "jwt.provider", "jwt.factory", and "jwt.secret".')
104+
->ifTrue(function ($v) { return isset($v['jwt']) && !isset($v['jwt']['value']) && !isset($v['jwt']['provider']) && !isset($v['jwt']['factory']) && !isset($v['jwt']['secret']); })
105+
->thenInvalid('You must specify at least one of "jwt.value", "jwt.provider", "jwt.factory", and "jwt.secret".')
90106
->end()
91107
->end()
92108
->end()

0 commit comments

Comments
 (0)