diff --git a/code_samples/multisite/siteaccess/AcmeExampleExtension.php b/code_samples/multisite/siteaccess/AcmeExampleExtension.php new file mode 100644 index 0000000000..3c56d8023a --- /dev/null +++ b/code_samples/multisite/siteaccess/AcmeExampleExtension.php @@ -0,0 +1,61 @@ +getConfiguration($configs, $container); + $config = $this->processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(self::ACME_CONFIG_DIR)); + $loader->load('default_settings.yaml'); + + $processor = new ConfigurationProcessor($container, 'acme_example'); + $processor->mapConfig( + $config, + // Any kind of callable can be used here. + // It is called for each declared scope/SiteAccess. + static function ($scopeSettings, $currentScope, ContextualizerInterface $contextualizer) { + // Maps the "name" setting to "acme_example.<$currentScope>.name" container parameter + // It is then possible to retrieve this parameter through ConfigResolver in the application code: + // $helloSetting = $configResolver->getParameter( 'name', 'acme_example' ); + $contextualizer->setContextualParameter('name', $currentScope, $scopeSettings['name']); + } + ); + + // Now map "custom_setting" and ensure the key defined for "my_siteaccess" overrides the one for "my_siteaccess_group" + // It is done outside the closure as it's needed only once. + $processor->mapConfigArray('custom_setting', $config); + + // Map setting example + $processor = new ConfigurationProcessor($container, 'acme_example'); + $processor->mapSetting('name', $config); + + // Map config array example + $processor->mapConfigArray('custom_setting', $config); + + // Merge from second level example + $contextualizer = $processor->getContextualizer(); + $contextualizer->mapConfigArray('custom_setting', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); + } + + /** @param array $config */ + public function getConfiguration(array $config, ContainerBuilder $container): Configuration + { + return new Configuration(); + } +} diff --git a/code_samples/multisite/siteaccess/Configuration.php b/code_samples/multisite/siteaccess/Configuration.php new file mode 100644 index 0000000000..fd0020ef43 --- /dev/null +++ b/code_samples/multisite/siteaccess/Configuration.php @@ -0,0 +1,29 @@ +getRootNode(); + + // $systemNode is the root of SiteAccess-aware settings. + $systemNode = $this->generateScopeBaseNode($rootNode); + $systemNode + ->scalarNode('name')->isRequired()->end() + ->arrayNode('custom_setting') + ->children() + ->scalarNode('string')->end() + ->integerNode('number')->end() + ->booleanNode('enabled')->end() + ->end() + ->end(); + + return $treeBuilder; + } +} diff --git a/docs/multisite/siteaccess/siteaccess_aware_configuration.md b/docs/multisite/siteaccess/siteaccess_aware_configuration.md index b2e73ac072..bf77dc3974 100644 --- a/docs/multisite/siteaccess/siteaccess_aware_configuration.md +++ b/docs/multisite/siteaccess/siteaccess_aware_configuration.md @@ -35,36 +35,8 @@ Acme\ExampleBundle\AcmeExampleBundle::class => ['all' => true], To parse semantic configuration, create a `Configuration` class which extends `Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\Configuration` and then extend its `generateScopeBaseNode()` method: -``` php hl_lines="17" -getRootNode(); - - // $systemNode is the root of SiteAccess-aware settings. - $systemNode = $this->generateScopeBaseNode($rootNode); - $systemNode - ->scalarNode('name')->isRequired()->end() - ->arrayNode('custom_setting') - ->children() - ->scalarNode('string')->end() - ->integerNode('number')->end() - ->booleanNode('enabled')->end() - ->end() - ->end(); - - return $treeBuilder; - } -} +``` php hl_lines="16" +[[= include_file('code_samples/multisite/siteaccess/Configuration.php') =]] ``` !!! note @@ -96,57 +68,13 @@ Semantic configuration must always be mapped to internal key/value settings with You usually do it in the [service container](php_api.md#service-container) extension. ``` php -getConfiguration($configs, $container); - $config = $this->processConfiguration($configuration, $configs); - - $loader = new Loader\YamlFileLoader($container, new FileLocator(self::ACME_CONFIG_DIR)); - $loader->load('default_settings.yaml'); - - $processor = new ConfigurationProcessor($container, 'acme_example'); - $processor->mapConfig( - $config, - // Any kind of callable can be used here. - // It is called for each declared scope/SiteAccess. - static function ($scopeSettings, $currentScope, ContextualizerInterface $contextualizer) { - // Maps the "name" setting to "acme_example.<$currentScope>.name" container parameter - // It is then possible to retrieve this parameter through ConfigResolver in the application code: - // $helloSetting = $configResolver->getParameter( 'name', 'acme_example' ); - $contextualizer->setContextualParameter('name', $currentScope, $scopeSettings['name']); - } - ); - - // Now map "custom_setting" and ensure the key defined for "my_siteaccess" overrides the one for "my_siteaccess_group" - // It is done outside the closure as it's needed only once. - $processor->mapConfigArray('custom_setting', $config); - } -} +[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 0, 42) =]][[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 53, 61) =]] ``` You can also map simple settings by calling `$processor->mapSetting()`, without having to call `$processor->mapConfig()` with a callable. ``` php -$processor = new ConfigurationProcessor($container, 'acme_example'); -$processor->mapSetting('name', $config); +[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 44, 46) =]] ``` !!! caution "Important" @@ -171,7 +99,7 @@ but enrich them by appending new entries. This is possible by using `$processor->mapConfigArray()`, which you must call outside the closure (before or after), so that it's called only once. ``` php -$processor->mapConfigArray('custom_setting', $config); +[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 48, 49) =]] ``` Consider the following default config in `default_settings.yaml`: @@ -225,7 +153,7 @@ However, because you defined the `os_types` key for `siteaccess1`, it completely You can add another level by passing `ContextualizerInterface::MERGE_FROM_SECOND_LEVEL` as the third argument to `$contextualizer->mapConfigArray()`: ``` php -$contextualizer->mapConfigArray('custom_setting', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); +[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 51, 53) =]] ``` When you use `ContextualizerInterface::MERGE_FROM_SECOND_LEVEL` with the configuration above, you get the following result: