Skip to content

Commit bd0ad98

Browse files
committed
minor #16808 [Bundles] [Bundle] Fix code example about prepend config (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [Bundles] [Bundle] Fix code example about prepend config See issue symfony/symfony#40198 The prepend mechanism happens before resolving any parameters and env vars, so at this moment the raw configs are not yet ready to be normalized and merged. Note that you can prepend a raw config like this one without any problem: ```php $container->prependExtensionConfig('foo', [ 'enabled' => '%kernel.debug%', // or '%env(bool:DEBUG_ENABLED)%' ]); ``` Now, if you want to add an extension config based on another extension config, what you have to do is to prepend this config iterating over the other one without merging, e.g.: ```php $configs = $container->getExtensionConfig('foo'); // iterate in reverse to preserve the original order after prepending config foreach (array_reverse($configs) as $config) { $container->prependExtensionConfig('bar', [ 'enabled' => $config['enabled'], // raw value ]); } ``` Where `$config['enabled']` can be a boolean value, a parameter, or an env var. It doesn't matter because the real value will be determined by Symfony after resolving, normalizing, and merging all configs. Note also that you can't be 100% sure that `$container->getExtensionConfig('foo')` will return all configs because there could be another extension (being executed after) appending config to your own `foo` extension, and then the final config could differ. About conditional configuration, it's harder to handle here. Even if you can resolve the parameter or env var to know the real value you should never pass the resolved value to another config, otherwise, something like this symfony/symfony#40198 (comment) will happen, especially when env vars are used. Therefore, the current documentation is error-prone as is, so I suggest using the iteration approach instead to avoid any issue with parameters and env vars. Commits ------- 1c16c30 Fix code example about prepend config
2 parents dba5691 + 1c16c30 commit bd0ad98

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

bundles/prepend_extension.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,18 @@ in case a specific other bundle is not registered::
8080
}
8181
}
8282

83-
// process the configuration of AcmeHelloExtension
83+
// get the configuration of AcmeHelloExtension (it's a list of configuration)
8484
$configs = $container->getExtensionConfig($this->getAlias());
8585

86-
// resolve config parameters e.g. %kernel.debug% to its boolean value
87-
$resolvingBag = $container->getParameterBag();
88-
$configs = $resolvingBag->resolveValue($configs);
89-
90-
// use the Configuration class to generate a config array with
91-
// the settings "acme_hello"
92-
$config = $this->processConfiguration(new Configuration(), $configs);
93-
94-
// check if entity_manager_name is set in the "acme_hello" configuration
95-
if (isset($config['entity_manager_name'])) {
96-
// prepend the acme_something settings with the entity_manager_name
97-
$config = ['entity_manager_name' => $config['entity_manager_name']];
98-
$container->prependExtensionConfig('acme_something', $config);
86+
// iterate in reverse to preserve the original order after prepending the config
87+
foreach (array_reverse($configs) as $config) {
88+
// check if entity_manager_name is set in the "acme_hello" configuration
89+
if (isset($config['entity_manager_name'])) {
90+
// prepend the acme_something settings with the entity_manager_name
91+
$container->prependExtensionConfig('acme_something', [
92+
'entity_manager_name' => $config['entity_manager_name'],
93+
]);
94+
}
9995
}
10096
}
10197

0 commit comments

Comments
 (0)