I'd like my bundle to inject a twig global from its config.
class MyBundle extends AbstractBundle
{
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new TwigPass());
}
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$theme = $config['theme']; // how to make this a twig global?
// $twig = $builder->getDefinition('twig'); // NOPE! Twig isn't loaded yet.
}
The compiler pass gets twig, which isn't available when my bundle extension is loading.
class TwigPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {
return;
}
$theme = 'theme_from_config';
$def = $container->getDefinition('twig');
$def->addMethodCall('addGlobal', ['theme', $theme]);
I'm missing something, or something is out of order. How can the compiler pass get the config data from the extension? Or if that's not the right approach, how can I inject a twig global from my bundle config?