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?
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
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With