pmishev
pmishev

Reputation: 976

get resolved parameters in a bundle extension class

I'm writing a Symfony bundle and I need to setup a service definition in my Extension class depending on the value of a configuration parameter.

An it works fine, as long as you don't try to use any expressions in the parameter value.

My extension class looks like this:

class MyBundleExtension extends Extension
{

    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);
        
        dd($config['custom_param']);
    }

    ...
}

In my application, I set the parameter like this:

.env.local:

MY_BUNDLE_CUSTOM_PARAM=blah

config.yaml:

parameters:
    my_bundle_custom_param: '%env(MY_BUNDLE_CUSTOM_PARAM)%'
my_bundle:
    custom_param: '%my_bundle_custom_param%'

The problem is, that in the Extension class, this parameter is not yet resolved, so dd() shows this:

"env_de85db6c4d65707d_MY_BUNDLE_CUSTOM_PARAM_71f5c271abfed252c958c82c1e3fb8dc"

instead of the expected blah.

Is there any way to get the resolved value of the parameter in the Extension class?

Upvotes: 1

Views: 1452

Answers (1)

jcarlosweb
jcarlosweb

Reputation: 974

You gave the correct answer in your comment:

/**
 * @throws \Exception
 */
public function load(array $configs, ContainerBuilder $container): void
{

    $configuration = new Configuration();
    $config        = $this->processConfiguration($configuration, $configs);
    $resolve       = $container->resolveEnvPlaceholders($config, true);
    $config['upload_path'] = \str_replace($_SERVER['DOCUMENT_ROOT'], '', $resolve['upload_directory']);
    $container->setParameter("mybundle", $config);

Upvotes: 0

Related Questions