Symfony DI:在捆绑包构建期间,通过编译器时访问配置参数

问题描述 投票:0回答:1

是否可以在编译器传递过程中获取配置参数?我有此扩展配置:

my_extension:
    foo: 'bar'

我需要在添加编译器通道之前查看是否已设置配置:

<?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',
                ]
            )
        );
    }
}

问题是,在通过编译器时,扩展配置尚未处理:还是我错了?

php symfony dependency-injection doctrine doctrine-extensions
1个回答
0
投票

通过使用编译器通过如下方式解决了这个问题:

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

然后在编译器上使用扩展名,如下所示:

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',
                    ]
                )
            );
        }
    }
}

然后使用扩展名读取配置:

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.