Skip to content

Commit 275da5d

Browse files
committed
Adjusted SiteAccessAware examples
1 parent 14a39ce commit 275da5d

File tree

3 files changed

+97
-78
lines changed

3 files changed

+97
-78
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Acme\ExampleBundle\DependencyInjection;
4+
5+
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ConfigurationProcessor;
6+
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Extension\Extension;
10+
use Symfony\Component\DependencyInjection\Loader;
11+
12+
final class AcmeExampleExtension extends Extension
13+
{
14+
public const ACME_CONFIG_DIR = __DIR__ . '/../../../config/acme';
15+
16+
/**
17+
* @throws \Exception
18+
*/
19+
public function load(array $configs, ContainerBuilder $container): void
20+
{
21+
$configuration = $this->getConfiguration($configs, $container);
22+
$config = $this->processConfiguration($configuration, $configs);
23+
24+
$loader = new Loader\YamlFileLoader($container, new FileLocator(self::ACME_CONFIG_DIR));
25+
$loader->load('default_settings.yaml');
26+
27+
$processor = new ConfigurationProcessor($container, 'acme_example');
28+
$processor->mapConfig(
29+
$config,
30+
// Any kind of callable can be used here.
31+
// It is called for each declared scope/SiteAccess.
32+
static function ($scopeSettings, $currentScope, ContextualizerInterface $contextualizer) {
33+
// Maps the "name" setting to "acme_example.<$currentScope>.name" container parameter
34+
// It is then possible to retrieve this parameter through ConfigResolver in the application code:
35+
// $helloSetting = $configResolver->getParameter( 'name', 'acme_example' );
36+
$contextualizer->setContextualParameter('name', $currentScope, $scopeSettings['name']);
37+
}
38+
);
39+
40+
// Now map "custom_setting" and ensure the key defined for "my_siteaccess" overrides the one for "my_siteaccess_group"
41+
// It is done outside the closure as it's needed only once.
42+
$processor->mapConfigArray('custom_setting', $config);
43+
44+
// Map setting example
45+
$processor = new ConfigurationProcessor($container, 'acme_example');
46+
$processor->mapSetting('name', $config);
47+
48+
// Map config array example
49+
$processor->mapConfigArray('custom_setting', $config);
50+
51+
// Merge from second level example
52+
$contextualizer = $processor->getContextualizer();
53+
$contextualizer->mapConfigArray('custom_setting', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL);
54+
}
55+
56+
/** @param array<mixed> $config */
57+
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
58+
{
59+
return new Configuration();
60+
}
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Acme\ExampleBundle\DependencyInjection;
4+
5+
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration;
6+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7+
8+
class Configuration extends SiteAccessConfiguration
9+
{
10+
public function getConfigTreeBuilder(): TreeBuilder
11+
{
12+
$treeBuilder = new TreeBuilder('acme_example');
13+
$rootNode = $treeBuilder->getRootNode();
14+
15+
// $systemNode is the root of SiteAccess-aware settings.
16+
$systemNode = $this->generateScopeBaseNode($rootNode);
17+
$systemNode
18+
->scalarNode('name')->isRequired()->end()
19+
->arrayNode('custom_setting')
20+
->children()
21+
->scalarNode('string')->end()
22+
->integerNode('number')->end()
23+
->booleanNode('enabled')->end()
24+
->end()
25+
->end();
26+
27+
return $treeBuilder;
28+
}
29+
}

docs/multisite/siteaccess/siteaccess_aware_configuration.md

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,8 @@ Acme\ExampleBundle\AcmeExampleBundle::class => ['all' => true],
3535

3636
To parse semantic configuration, create a `Configuration` class which extends `Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\Configuration` and then extend its `generateScopeBaseNode()` method:
3737

38-
``` php hl_lines="17"
39-
<?php
40-
41-
namespace Acme\ExampleBundle\DependencyInjection;
42-
43-
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration;
44-
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
45-
46-
class Configuration extends SiteAccessConfiguration
47-
{
48-
public function getConfigTreeBuilder(): TreeBuilder
49-
{
50-
$treeBuilder = new TreeBuilder('acme_example');
51-
$rootNode = $treeBuilder->getRootNode();
52-
53-
// $systemNode is the root of SiteAccess-aware settings.
54-
$systemNode = $this->generateScopeBaseNode($rootNode);
55-
$systemNode
56-
->scalarNode('name')->isRequired()->end()
57-
->arrayNode('custom_setting')
58-
->children()
59-
->scalarNode('string')->end()
60-
->integerNode('number')->end()
61-
->booleanNode('enabled')->end()
62-
->end()
63-
->end();
64-
65-
return $treeBuilder;
66-
}
67-
}
38+
``` php hl_lines="16"
39+
[[= include_file('code_samples/multisite/siteaccess/Configuration.php') =]]
6840
```
6941

7042
!!! note
@@ -96,57 +68,14 @@ Semantic configuration must always be mapped to internal key/value settings with
9668
You usually do it in the [service container](php_api.md#service-container) extension.
9769
9870
``` php
99-
<?php
100-
101-
namespace Acme\ExampleBundle\DependencyInjection;
102-
103-
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ConfigurationProcessor;
104-
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
105-
use Symfony\Component\Config\FileLocator;
106-
use Symfony\Component\DependencyInjection\ContainerBuilder;
107-
use Symfony\Component\DependencyInjection\Loader;
108-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
109-
110-
class AcmeExampleExtension extends Extension
111-
{
112-
public const ACME_CONFIG_DIR = __DIR__ . '/../../../config/acme';
113-
114-
/**
115-
* @throws \Exception
116-
*/
117-
public function load(array $configs, ContainerBuilder $container): void
118-
{
119-
$configuration = $this->getConfiguration($configs, $container);
120-
$config = $this->processConfiguration($configuration, $configs);
121-
122-
$loader = new Loader\YamlFileLoader($container, new FileLocator(self::ACME_CONFIG_DIR));
123-
$loader->load('default_settings.yaml');
124-
125-
$processor = new ConfigurationProcessor($container, 'acme_example');
126-
$processor->mapConfig(
127-
$config,
128-
// Any kind of callable can be used here.
129-
// It is called for each declared scope/SiteAccess.
130-
static function ($scopeSettings, $currentScope, ContextualizerInterface $contextualizer) {
131-
// Maps the "name" setting to "acme_example.<$currentScope>.name" container parameter
132-
// It is then possible to retrieve this parameter through ConfigResolver in the application code:
133-
// $helloSetting = $configResolver->getParameter( 'name', 'acme_example' );
134-
$contextualizer->setContextualParameter('name', $currentScope, $scopeSettings['name']);
135-
}
136-
);
137-
138-
// Now map "custom_setting" and ensure the key defined for "my_siteaccess" overrides the one for "my_siteaccess_group"
139-
// It is done outside the closure as it's needed only once.
140-
$processor->mapConfigArray('custom_setting', $config);
141-
}
142-
}
71+
[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 0, 41) =]]
72+
[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 53, 61) =]]
14373
```
14474

14575
You can also map simple settings by calling `$processor->mapSetting()`, without having to call `$processor->mapConfig()` with a callable.
14676

14777
``` php
148-
$processor = new ConfigurationProcessor($container, 'acme_example');
149-
$processor->mapSetting('name', $config);
78+
[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 44, 46) =]]
15079
```
15180

15281
!!! caution "Important"
@@ -171,7 +100,7 @@ but enrich them by appending new entries.
171100
This is possible by using `$processor->mapConfigArray()`, which you must call outside the closure (before or after), so that it's called only once.
172101

173102
``` php
174-
$processor->mapConfigArray('custom_setting', $config);
103+
[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 48, 49) =]]
175104
```
176105

177106
Consider the following default config in `default_settings.yaml`:
@@ -225,7 +154,7 @@ However, because you defined the `os_types` key for `siteaccess1`, it completely
225154
You can add another level by passing `ContextualizerInterface::MERGE_FROM_SECOND_LEVEL` as the third argument to `$contextualizer->mapConfigArray()`:
226155

227156
``` php
228-
$contextualizer->mapConfigArray('custom_setting', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL);
157+
[[= include_file('code_samples/multisite/siteaccess/AcmeExampleExtension.php', 51, 53) =]]
229158
```
230159

231160
When you use `ContextualizerInterface::MERGE_FROM_SECOND_LEVEL` with the configuration above, you get the following result:

0 commit comments

Comments
 (0)