Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access semantic configuration in a compiler pass?

I have semantic configuration for a bundle that needs to be interpreted during a compiler pass for the same bundle.

Is it possible to access it without storing it in an intermediate container variable?

like image 329
Jens Avatar asked Mar 10 '13 17:03

Jens


3 Answers

Yes, kind of:

<?php

namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $configs = $container->getExtensionConfig('acme_demo');
    }
}

From what I can see $configs is an array of unmerged configurations and default values are not included (values defined by the configuration TreeBuilder).

See here and here

like image 77
Peter Avatar answered Nov 20 '22 22:11

Peter


Just for the sake of completeness to @Peter's answer: getExtensionConfig returns an array of arrays which should be processed with the corresponding Configuration to be able to access default values.

<?php

namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class CompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $configs = $container->getExtensionConfig('acme_demo');
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        /// You can safely work with $config now
    }

    private function processConfiguration(ConfigurationInterface $configuration, array $configs)
    {
        $processor = new Processor();

        return $processor->processConfiguration($configuration, $configs);
    }
}
like image 6
xPheRe Avatar answered Nov 20 '22 23:11

xPheRe


I realize that this is an old post, but I was looking for the same information and eventually found that this works for a single parameter:

$cfgVal = $container
  ->getParameterBag()
  ->resolveValue( $container->getParameter( 'param_name' ));

Of course it's possible that this functionality was added after the original post.

like image 4
David Patterson Avatar answered Nov 20 '22 22:11

David Patterson