12
12
namespace Symfony \UX \LiveComponent \DependencyInjection ;
13
13
14
14
use Symfony \Component \AssetMapper \AssetMapperInterface ;
15
+ use Symfony \Component \Config \Definition \Builder \ArrayNodeDefinition ;
16
+ use Symfony \Component \Config \Definition \Builder \TreeBuilder ;
17
+ use Symfony \Component \Config \Definition \ConfigurationInterface ;
18
+ use Symfony \Component \DependencyInjection \Argument \TaggedIteratorArgument ;
15
19
use Symfony \Component \DependencyInjection \ChildDefinition ;
16
20
use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
21
use Symfony \Component \DependencyInjection \ContainerInterface ;
50
54
use Symfony \UX \TwigComponent \ComponentFactory ;
51
55
use Symfony \UX \TwigComponent \ComponentRenderer ;
52
56
53
- use function Symfony \Component \DependencyInjection \Loader \Configurator \tagged_iterator ;
54
-
55
57
/**
56
58
* @author Kevin Bond <[email protected] >
57
59
*
58
60
* @internal
59
61
*/
60
- final class LiveComponentExtension extends Extension implements PrependExtensionInterface
62
+ final class LiveComponentExtension extends Extension implements PrependExtensionInterface, ConfigurationInterface
61
63
{
62
64
public const TEMPLATES_MAP_FILENAME = 'live_components_twig_templates.map ' ;
63
65
@@ -93,16 +95,19 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
93
95
}
94
96
);
95
97
98
+ $ configuration = $ this ->getConfiguration ($ configs , $ container );
99
+ $ config = $ this ->processConfiguration ($ configuration , $ configs );
100
+
96
101
$ container ->registerForAutoconfiguration (HydrationExtensionInterface::class)
97
102
->addTag (LiveComponentBundle::HYDRATION_EXTENSION_TAG );
98
103
99
104
$ container ->register ('ux.live_component.component_hydrator ' , LiveComponentHydrator::class)
100
105
->setArguments ([
101
- tagged_iterator (LiveComponentBundle::HYDRATION_EXTENSION_TAG ),
106
+ new TaggedIteratorArgument (LiveComponentBundle::HYDRATION_EXTENSION_TAG ),
102
107
new Reference ('property_accessor ' ),
103
108
new Reference ('ux.live_component.metadata_factory ' ),
104
109
new Reference ('serializer ' , ContainerInterface::NULL_ON_INVALID_REFERENCE ),
105
- ' %kernel.secret% ',
110
+ $ config [ ' secret ' ], // defaults to ' %kernel.secret%'
106
111
])
107
112
;
108
113
@@ -236,7 +241,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
236
241
237
242
$ container ->register ('ux.live_component.deterministic_id_calculator ' , DeterministicTwigIdCalculator::class);
238
243
$ container ->register ('ux.live_component.fingerprint_calculator ' , FingerprintCalculator::class)
239
- ->setArguments ([' %kernel. secret% ' ]);
244
+ ->setArguments ([$ config [ ' secret ' ]]); // default to %kernel.secret%
240
245
241
246
$ container ->setAlias (ComponentValidatorInterface::class, ComponentValidator::class);
242
247
@@ -258,6 +263,35 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
258
263
->addTag ('kernel.cache_warmer ' );
259
264
}
260
265
266
+ public function getConfigTreeBuilder (): TreeBuilder
267
+ {
268
+ $ treeBuilder = new TreeBuilder ('live_component ' );
269
+ $ rootNode = $ treeBuilder ->getRootNode ();
270
+ \assert ($ rootNode instanceof ArrayNodeDefinition);
271
+
272
+ $ rootNode
273
+ ->addDefaultsIfNotSet ()
274
+ ->children ()
275
+ ->scalarNode ('secret ' )
276
+ ->info ('The secret used to compute fingerprints and checksums ' )
277
+ ->beforeNormalization ()
278
+ ->ifString ()
279
+ ->then (trim (...))
280
+ ->end ()
281
+ ->cannotBeEmpty ()
282
+ ->defaultValue ('%kernel.secret% ' )
283
+ ->end ()
284
+ ->end ()
285
+ ;
286
+
287
+ return $ treeBuilder ;
288
+ }
289
+
290
+ public function getConfiguration (array $ config , ContainerBuilder $ container ): ConfigurationInterface
291
+ {
292
+ return $ this ;
293
+ }
294
+
261
295
private function isAssetMapperAvailable (ContainerBuilder $ container ): bool
262
296
{
263
297
if (!interface_exists (AssetMapperInterface::class)) {
0 commit comments