Skip to content

[DependencyInjection] Accessing processed extension configs #48408

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

Closed
wants to merge 1 commit into from

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Nov 30, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR TODO

This proposal adds a new container parameter .extension.processed_configs that holds all processed extension configs from all registered extensions in order to improve the DX during the DI compilation phase (load extension, compiler pass).

Before

Currently, it's not possible to access processed configs, so this new parameter will avoid unsafely and hacking workarounds just to query a config value that belongs to another (already processed) extension and derives some DI definitions from it.

Some workarounds involve:

  • Collect extension configs during the "prepend extension" phase (even if other extensions can prepend more config after)
  • Duplicated configs with similar meanings in two or more extensions
  • Call/override the processConfiguration() method from another extension is forbidden as it's protected and final, meaning you'll have to process twice this config outside the extension owner.
  • Useless container parameters have to be defined to pass config information from "prepend" to "load" or/and from "load" to "compiler passes".

After

With this proposal, you'll access these configs through the .extension.processed_configs parameter. It's an array of extensions configs, where the array key matches the extension alias:

class AppExtension extends AbstractExtension
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $processedConfigs = $builder->getParameter('.extension.processed_configs');

        // $processedConfigs['framework']
    }
}

Note that in load or loadExtension you have access only to already loaded extensions depending on the extension/bundle definition order. If you need access to other extension configs that are not loaded yet then move your logic to a compiler pass or change the bundle/extension order in the bundle.php file of your project.

Also available in any compiler pass with the capability to resolve env placeholders:

class AppCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        $processedConfigs = $container->getParameter('.extension.processed_configs');
        $processedConfigs = $container->resolveEnvPlaceholders($processedConfigs, true);

        // $processedConfigs['framework']
    }
}

After merging #47680 this .extension.processed_configs parameter is considered a "build parameter" finally, so it will be used during compiler time only and will be removed at the end of the process.

@carsonbot carsonbot added Status: Needs Review DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature labels Nov 30, 2022
@carsonbot carsonbot added this to the 6.3 milestone Nov 30, 2022
@carsonbot carsonbot changed the title [DX][DependencyInjection] Accessing to processed extension configs [DependencyInjection] Accessing to processed extension configs Nov 30, 2022
@yceruto yceruto force-pushed the extension_processed_config branch from 9a47590 to 6be13c9 Compare November 30, 2022 14:20
@OskarStark
Copy link
Contributor

Regarding the PR headline

- [DependencyInjection] Accessing to processed extension configs
+ [DependencyInjection] Accessing processed extension configs

@stof
Copy link
Member

stof commented Nov 30, 2022

Big -1 for this, as it means that the structure of the processed config becomes a public API covered by BC, making it impossible to refactor the config anymore, while the Configuration class is currently the perfect tool to provide a BC layer for the config structure (converting old structure into the new structure expected by the extension).

@stof
Copy link
Member

stof commented Nov 30, 2022

Another -1 is that you expose that parameter with a partial value to DI extensions, making them very sensible to the bundle loading order. Currently, each DI extension receives a dedicated ContainerBuilder that has none of the services defined by other bundles precisely to reduce the risk of implementing something in the DI extension that should be done in a compiler pass instead to allow collecting services from any other bundle.

Note that the early preview versions of Symfony 2.0 did not have this isolation layer, and it caused lots of issues for early adopters when doing stuff in DI extensions instead of implementing a compiler pass. So we have implementation experience proving that it is a bad architecture (making it easier to write broken code than correct code).

@yceruto yceruto changed the title [DependencyInjection] Accessing to processed extension configs [DependencyInjection] Accessing processed extension configs Nov 30, 2022
@yceruto
Copy link
Member Author

yceruto commented Dec 4, 2022

Fair enough, thanks for the feedback.

@yceruto yceruto closed this Dec 4, 2022
@yceruto yceruto deleted the extension_processed_config branch December 4, 2022 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Status: Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants