Skip to content

Commit af63179

Browse files
javiereguiluzJean-Beru
authored andcommitted
minor symfony#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 af63179

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-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

reference/configuration/framework.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ settings is configured.
337337

338338
.. _reference-framework-csrf-protection:
339339

340+
field_name
341+
..........
342+
343+
**type**: ``string`` **default**: ``_token``
344+
345+
This is the field name that you should give to the CSRF token field of your form.
346+
340347
csrf_protection
341348
~~~~~~~~~~~~~~~
342349

0 commit comments

Comments
 (0)