Description
Description
I am facing an issue that I can't seem to resolve nicely.
The code I am working on uses a series of "modules".
Each module has a ModuleConfigurator class that is responsible for configuring the module and its dependencies.
The way these dependencies are configured might depend on complex logic, which is better handled by using PHP.
The way they are loaded looks as follows:
// Kernel.php
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
CoreModuleConfigurator::configure($container, $loader);
OtherModuleConfigurator::configure($container, $loader);
}
The issues I am facing are:
- Given I have a ContainerBuilder, how can I load multiple services of a namespace at once?
The container builder does not provide something like this. Also, it does not provide a way to specify defaults. Meaning I would be better off using a ContainerConfigurator. Fine with me, but as per the documentation the only way is by using a PhpFileLoader that needs a script that returns a function, but I already have a class for this. So, I guess a workaround would be by providing a callback to my static method? But this leads me to my second issue:
- Given I managed to pass a ContainerConfigurator to my ModuleConfigurator, how can I add compiler passes?
The ContainerConfigurator does not provide a way to add a Compiler Pass to the underlying container. It does not give access to the container builder at all.
I could wire things up so I can pass both the ContainerConfigurator and the ContainerBuilder, but that seems a little repetitive given the ContainerConfigurator has access to the container.
Suggestion
I have seen that in the latest version on master 5.1.0 not released yet, the KernelTraits now allows to type hint the configureContainer
method with either a ContainerBuilder
or a ContainerConfigurator
. Which is pretty nice by the way, but would mean from what I understand so far, that:
- If I want to load multiple services at once in PHP, I need the ContainerConfigurator
- If I want to add compiler passes, I need the ContainerBuilder
- If I want to do both -> Do I need both? Or is there another simpler way?
What I could see, would be to expose the ContainerBuilder using a getContainerBuilder method on the ContainerConfigurator.
Maybe, I am completely off track, but I haven't seen anything in the documentation, that would address my use case.