G
G
grabbee2020-04-18 14:33:05
symfony
grabbee, 2020-04-18 14:33:05

How to properly configure a third-party bundle in your own?

For example , LiipImagineBundle - it has many parameters that need to be set in its own config.

I want to make a separate bundle in which almost everything should be already configured.
How to do it right?

1. Import the config file from your bundle into the application.
2. $container->prependExtensionConfig($name, $config);

Or how is it in general?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Flying, 2020-04-18
@grabbee

Redefining configuration in Symfony outside of the main process of loading configuration files is not the easiest thing to do. This process is closed from direct customization. the main process of loading and processing the bundles configuration is completely at the mercy of them. The reason for these restrictions is that loading the configuration is one of the earliest stages of work and, as with all such parts of the code, there is quite a bit of room for maneuver. In this regard, the way (and the very possibility) of redefining the configuration will very much depend on what exactly you want to redefine.
In order to better understand how this happens, you should study in more detail the question of how the loading and processing of the configuration in Symfony is organized.
In short:

  1. The container configuration process collects configuration data from configuration files. This data is stored in the container via ContainerBuilder::loadFromExtension() .
  2. Next, the collected data is passed as a basis to the bundle extension (via the load() method ). How exactly the bundle extension will work with the collected data depends entirely on the bundle code, so in general anything can happen to them.

As you, I hope, understand - due to paragraph 2, there is no general approach to redefining data in Symfony. Therefore, as I wrote above, you need to decide what exactly you want to override, and then look at the loader code of the corresponding bundle ( here it is for LiipImagineBundle).
As you can see, LiipImagineBundlein the whole case, nothing terrible happens, the resulting configuration is immediately sent to processing and then used to register services and parameters.
Since the container build process starts with a merge pass , you can't interfere with this process with a compiler pass, as is usually done, but you can use thispiece of logic in order to achieve your goal: you just need to define your own extension in your bundle and note that it implements PrependExtensionInterface . This will allow your extension to get an instance ContainerBuilder before other extensions are loaded, which is what will allow you to use ContainerBuilder::loadFromExtension() to add configuration options to the bundle you need.
Somewhat non-trivial, but should work :)

G
grabbee, 2020-04-19
@grabbee

Yes. It seems to work.

# implements CompilerPassInterface

    public function process(ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/packages'));
        $loader->load('liip_imagine.yaml');
    }

It loaded everything at once into the container.
PrependExtensionInterface prepend - didn't work. He needed parameters from the services.yml of the bundle itself, and its loading comes after prepend, as I understand it. process - works on an already prepared container, and all the parameters are there. Now I will understand if I am doing everything right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question