1

Is it possible to get config parameters during compiler pass? I have this extention config:

my_extension:
    foo: 'bar'

I need to see if a config is set before adding a compiler pass:

<?php

namespace My\TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;

class MyTestBundle extends Bundle
{
    /**
     * @param ContainerBuilder $container
     */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // Here I need to check if 'foo' == 'bar' from the extension config and then add the following compiler pass
        $container->addCompilerPass(
            DoctrineOrmMappingsPass::createAnnotationMappingDriver(
                [__NAMESPACE__],
                [
                    __DIR__.'/Model',
                ]
            )
        );
    }
}

The problem is that at the time of compiler pass, the extension config is not yet processed: or am I wrong?

numediaweb
  • 16,362
  • 12
  • 74
  • 110
  • Very similar to https://stackoverflow.com/questions/58877892/symfony-how-can-i-get-hold-of-tagged-services-and-processed-configuration-at-th – Cerad Nov 27 '19 at 15:19

1 Answers1

0

Solved it by using a compiler pass on the bundle like this:

class MyTestBundle extends Bundle
{
    /**
     * @param ContainerBuilder $container
     */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new MyTestBundleCompilerPass());
    }
}

then using extension on the compiler pass like this:

class MyTestBundleCompilerPass implements CompilerPassInterface
{
    /**
     * @param ContainerBuilder $container
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasExtension(MyTestExtension::ALIAS)) {
            return;
        }

        /** @var MyTestExtension $extension */
        $extension = $container->getExtension(MyTestExtension::ALIAS);
        $config = $extension->getConfig();

        if (array_key_exists('foo', $config) && $config['foo']) {
            $container->addCompilerPass(
                DoctrineOrmMappingsPass::createAnnotationMappingDriver(
                    [__NAMESPACE__],
                    [
                        __DIR__.'/Model',
                    ]
                )
            );
        }
    }
}

then using extension to read the config:

class MyTestExtension extends Extension
{
    const ALIAS = 'my_extension';

    private $config = array();

    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $this->config = $this->processConfiguration($configuration, $configs);

        $container->setParameter(self::ALIAS.'.foo', $this->config['foo']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        // Extensions to override configs for
        $loader->load('config.yml');
    }

    public function getConfig()
    {
        try {
            return $this->config;
        } finally {
            // Erases the config after it is retrieved, for security and performance reasons
            $this->config = array();
        }
    }
}
numediaweb
  • 16,362
  • 12
  • 74
  • 110