You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
0 commit comments