Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection] Prepend extension config withContainerConfigurator#52636

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@yceruto
Copy link
Member

@ycerutoyceruto commentedNov 17, 2023
edited
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

I found this by fixing an issue in a bundle that was trying to prepend extension configs using$container->extension('namespace', [...]) inAbstractBundle::prependExtension(), which indeed was appending that config instead of prepending it. Most importantly, the append strategy requires the extension namespace to be loaded beforehand, which is not required when prepend is used.

This title DX improvement helps to avoid the confusion betweenContainerConfigurator $container andContainerBuilder $builder to prepend extension config by allowingContainerConfigurator to do the same now.

Example:

class AcmeFooBundleextends AbstractBundle{publicfunctionprependExtension(ContainerConfigurator$container,ContainerBuilder$builder):void    {// Before (only way)$builder->prependExtensionConfig('namespace', ['foo' =>'bar']);// After (also this way) passing "true" as the 3rd parameter$container->extension('namespace', ['foo' =>'bar'],true);    }}

Instead of adding a new$prepend argument to the existing method, I could create a new method, e.g.,ContainerConfigurator::prependExtension(). What do you prefer?

This also helps when you want to prepend several or large configs in your bundle or extension class. Actually, using just$container->import('...') doesn't work because internally it will always append the configs, unless you do the following hidden trick below.

// acme-bundle/config/packages/configs.phpuseSymfony\Component\DependencyInjection\ContainerBuilder;returnstaticfunction (ContainerBuilder$container) {$container->prependExtensionConfig('namespace', ['large' =>'config', ...]);};

If you typeContainerBuilder instead ofContainerConfigurator in the external PHP config file, the builder instance will be passed instead, allowing you to use theprependExtensionConfig() method.

But with this proposal, it's simpler as you can keep usingContainerConfigurator to prepend extension configs without doing any tactic.

vtsykun reacted with thumbs up emoji
@ycerutoyceruto added this to the7.1 milestoneNov 17, 2023
@carsonbotcarsonbot added Status: Needs Review DependencyInjection DXDX = Developer eXperience (anything that improves the experience of using Symfony) Feature labelsNov 17, 2023
@carsonbotcarsonbot changed the title[DI][DX] add argument to prepend extension config[DependencyInjection] add argument to prepend extension configNov 17, 2023
@ycerutoyceruto changed the title[DependencyInjection] add argument to prepend extension config[DependencyInjection] Prepend extension config withContainerConfiguratorNov 17, 2023
@yceruto
Copy link
MemberAuthor

Ideally, I would like to import configs from theAbstractBundle::prependExtension() method this way:

$container->import('../config/packages/framework.yaml', prepend:true);// or using a new method

But the impact is currently too high, as this prepend value should pass across multiple interfaces and processes until reaching the finalContainerBuilder::loadFromExtension() invocation.

@yceruto
Copy link
MemberAuthor

yceruto commentedNov 17, 2023
edited
Loading

I was also thinking about a new marker in the config file, similar towhen@env, but using it for the importing config strategy, e.g.:

import@prepend:framework:messenger:...

And then prepend that config instead of appending. Even with less impact on the code, I don't like it too much as the config itself shouldn't be aware of the importing strategy.

Similarly, in theimports section:

imports:    -{ resource: 'config.yaml', prepend: true }

But still, I'm not sure as you will need to create two config files to achieve that and again the prepend value needs to by pass some loading interfaces where this prepend value doesn't mean anything.

@nicolas-grekas
Copy link
Member

the append strategy requires the extension namespace to be loaded beforehand, which is not required when prepend is used.

Can you please refresh my mind about this: why don't we check the namespace when prepending?

@yceruto
Copy link
MemberAuthor

Can you please refresh my mind about this: why don't we check the namespace when prepending?

It's on purpose because the extension might not even exist. This feature was initially created to provide preset settings in bundles, so if the extension doesn't exist in your app, that prepended config is ignored.

@yceruto
Copy link
MemberAuthor

Also because the extension order according to thebundles.php.

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Got it, makes sense to me also.

@nicolas-grekas
Copy link
Member

Thank you@yceruto.

@nicolas-grekasnicolas-grekas merged commit0693c5a intosymfony:7.1Nov 20, 2023
@ycerutoyceruto deleted the feature/prepend_extension branchNovember 20, 2023 12:49
nicolas-grekas added a commit that referenced this pull requestApr 3, 2024
…h file loaders (yceruto)This PR was merged into the 7.1 branch.Discussion----------[DependencyInjection] Prepending extension configs with file loaders| Q             | A| ------------- | ---| Branch?       | 7.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        |Fix#52789| License       | MIT#52636 continuation.This is another try to simplify even more the prepending extension config strategy for Bundles and Extensions.Feature: "Import and prepend an extension configuration from an external YAML, XML or PHP file"> Useful when you need to pre-configure some functionalities in your app, such as mapping some DBAL types provided by your bundle, or simply pre-configuring some default values automatically when your bundle is installed, without losing the ability to override them if desired in userland.```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        $config = Yaml::parse(file_get_contents(__DIR__.'/../config/doctrine.yaml'));        $builder->prependExtensionConfig('doctrine', $config['doctrine']);        // After        $container->import('../config/doctrine.yaml');    }}```The "Before" section is limited to importing/parsing this kind of configuration by hand; it doesn't support features like `when@env`, recursive importing, or defining parameters/services in the same file. Further, you can't simply use `ContainerConfigurator::import()` or any `*FileLoader::load()` here, as they will append the extension config instead of prepending it, defeating the prepend method purpose and breaking your app's config strategy. Thus, you are forced to use `$builder->prependExtensionConfig()` as the only way.> This is because if you append any extension config using `$container->import()`, then you won't be able to change that config in your app as it's merged with priority. If anyone is doing that currently, it shouldn't be intentional.Therefore, the "After" proposal changes that behavior for `$container->import()`. *Now, all extension configurations encountered in your external file will be prepended instead. BUT, this will only happen here, at this moment, during the `prependExtension()` method.*Of course, this little behavior change is a "BC break". However, it is a behavior that makes more sense than the previous one, enabling the usage of `ContainerConfigurator::import()` here, which was previously ineffective.This capability is also available for `YamlFileLoader`, `XmlFileLoader` and `PhpFileLoader` through a new boolean constructor argument:```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        // - It can't be used for prepending config purposes        // After        $loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/'), prepend: true);        $loader->load('doctrine.yaml'); // now it prepends extension configs as default behavior        // or        $loader->import('prepend/*.yaml');    }}```These changes only affect the loading strategy for extension configs; everything else keeps working as before.Cheers!Commits-------6ffd706 prepend extension configs with file loaders
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull requestApr 3, 2024
…h file loaders (yceruto)This PR was merged into the 7.1 branch.Discussion----------[DependencyInjection] Prepending extension configs with file loaders| Q             | A| ------------- | ---| Branch?       | 7.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | Fix #52789| License       | MITsymfony/symfony#52636 continuation.This is another try to simplify even more the prepending extension config strategy for Bundles and Extensions.Feature: "Import and prepend an extension configuration from an external YAML, XML or PHP file"> Useful when you need to pre-configure some functionalities in your app, such as mapping some DBAL types provided by your bundle, or simply pre-configuring some default values automatically when your bundle is installed, without losing the ability to override them if desired in userland.```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        $config = Yaml::parse(file_get_contents(__DIR__.'/../config/doctrine.yaml'));        $builder->prependExtensionConfig('doctrine', $config['doctrine']);        // After        $container->import('../config/doctrine.yaml');    }}```The "Before" section is limited to importing/parsing this kind of configuration by hand; it doesn't support features like `when@env`, recursive importing, or defining parameters/services in the same file. Further, you can't simply use `ContainerConfigurator::import()` or any `*FileLoader::load()` here, as they will append the extension config instead of prepending it, defeating the prepend method purpose and breaking your app's config strategy. Thus, you are forced to use `$builder->prependExtensionConfig()` as the only way.> This is because if you append any extension config using `$container->import()`, then you won't be able to change that config in your app as it's merged with priority. If anyone is doing that currently, it shouldn't be intentional.Therefore, the "After" proposal changes that behavior for `$container->import()`. *Now, all extension configurations encountered in your external file will be prepended instead. BUT, this will only happen here, at this moment, during the `prependExtension()` method.*Of course, this little behavior change is a "BC break". However, it is a behavior that makes more sense than the previous one, enabling the usage of `ContainerConfigurator::import()` here, which was previously ineffective.This capability is also available for `YamlFileLoader`, `XmlFileLoader` and `PhpFileLoader` through a new boolean constructor argument:```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        // - It can't be used for prepending config purposes        // After        $loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/'), prepend: true);        $loader->load('doctrine.yaml'); // now it prepends extension configs as default behavior        // or        $loader->import('prepend/*.yaml');    }}```These changes only affect the loading strategy for extension configs; everything else keeps working as before.Cheers!Commits-------6ffd70662e prepend extension configs with file loaders
symfony-splitter pushed a commit to symfony/http-kernel that referenced this pull requestApr 3, 2024
…h file loaders (yceruto)This PR was merged into the 7.1 branch.Discussion----------[DependencyInjection] Prepending extension configs with file loaders| Q             | A| ------------- | ---| Branch?       | 7.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | Fix #52789| License       | MITsymfony/symfony#52636 continuation.This is another try to simplify even more the prepending extension config strategy for Bundles and Extensions.Feature: "Import and prepend an extension configuration from an external YAML, XML or PHP file"> Useful when you need to pre-configure some functionalities in your app, such as mapping some DBAL types provided by your bundle, or simply pre-configuring some default values automatically when your bundle is installed, without losing the ability to override them if desired in userland.```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        $config = Yaml::parse(file_get_contents(__DIR__.'/../config/doctrine.yaml'));        $builder->prependExtensionConfig('doctrine', $config['doctrine']);        // After        $container->import('../config/doctrine.yaml');    }}```The "Before" section is limited to importing/parsing this kind of configuration by hand; it doesn't support features like `when@env`, recursive importing, or defining parameters/services in the same file. Further, you can't simply use `ContainerConfigurator::import()` or any `*FileLoader::load()` here, as they will append the extension config instead of prepending it, defeating the prepend method purpose and breaking your app's config strategy. Thus, you are forced to use `$builder->prependExtensionConfig()` as the only way.> This is because if you append any extension config using `$container->import()`, then you won't be able to change that config in your app as it's merged with priority. If anyone is doing that currently, it shouldn't be intentional.Therefore, the "After" proposal changes that behavior for `$container->import()`. *Now, all extension configurations encountered in your external file will be prepended instead. BUT, this will only happen here, at this moment, during the `prependExtension()` method.*Of course, this little behavior change is a "BC break". However, it is a behavior that makes more sense than the previous one, enabling the usage of `ContainerConfigurator::import()` here, which was previously ineffective.This capability is also available for `YamlFileLoader`, `XmlFileLoader` and `PhpFileLoader` through a new boolean constructor argument:```phpclass AcmeFooBundle extends AbstractBundle{    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void    {        // Before        // - It can't be used for prepending config purposes        // After        $loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/'), prepend: true);        $loader->load('doctrine.yaml'); // now it prepends extension configs as default behavior        // or        $loader->import('prepend/*.yaml');    }}```These changes only affect the loading strategy for extension configs; everything else keeps working as before.Cheers!Commits-------6ffd70662e prepend extension configs with file loaders
@fabpotfabpot mentioned this pull requestMay 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Labels

DependencyInjectionDXDX = Developer eXperience (anything that improves the experience of using Symfony)FeatureStatus: Reviewed

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

3 participants

@yceruto@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp