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?

1

1 Answer 1

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.