Skip to content

[Bundles] [Bundle] Fix code example about prepend config #16808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2022

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented May 18, 2022

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:

$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.:

$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.

@carsonbot carsonbot added this to the 4.4 milestone May 18, 2022
@carsonbot carsonbot changed the title [Bundle] Fix code example about prepend config [Bundles] [Bundle] Fix code example about prepend config May 19, 2022
@javiereguiluz
Copy link
Member

This is tricky to explain, but I think you did it nicely with the proposed changes. Thanks Yonel!

@javiereguiluz javiereguiluz merged commit bd0ad98 into symfony:4.4 May 19, 2022
@yceruto yceruto deleted the prepend_extension_config branch May 19, 2022 17:19
Jean-Beru pushed a commit to Jean-Beru/symfony-docs that referenced this pull request May 23, 2022
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants