-1

我想将Doctrine 捆绑包配置为具有DBAL连接。出于某种原因,配置需要一些逻辑来检索。我尝试使用容器扩展,然后在编译容器时使用编译器传递来执行逻辑,并将配置存储为容器参数

在我的尝试过程中,我在Kernel类中注册了扩展和编译器通道,如下所示:

protected function build(ContainerBuilder $container)
{
    // Those lines weren't there at the same time
    $container->registerExtension(new MyCustomExtension());
    $container->addCompilerPass(new MyCustomCompilerPass());
}

它似乎运行良好,因为我可以在控制台中看到我的参数:

 # ./bin/console debug:container --parameters

Symfony Container Parameters
============================

 ------------------------------------------------------------- ------------------------------------------------------------------------ 
  Parameter                                                     Value                                                                   
 ------------------------------------------------------------- ------------------------------------------------------------------------ 
 ...
 some.prefix.host                                              some-mariadb-host
 some.prefix.dbname                                            some-database-name
 ...

问题是,当我尝试在我的中使用这些参数时,config/packages/doctrine.yaml我的下一个控制台命令出现错误:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%some.prefix.host%'
        dbname: '%some.prefix.dbname%'
        # ...
# ./bin/console debug:container --parameters

In ParameterBag.php line 98:
                                                                                
  You have requested a non-existent parameter "some.prefix.host".  
                                                                                

我正在使用Symfony 5.3Doctrine bundle 2.4

  • 为什么我的参数对于 3rd 方捆绑配置似乎无法访问?
  • 我怎样才能使这项工作?
  • 有没有更好的方法来实现这一目标?
4

1 回答 1

0

我认为在我的编译器传递可以声明参数之前处理了 Doctrine 捆绑配置。它可能无法使用 DependencyInjection 组件解决。

通过在services.yaml中导入 PHP 配置文件来解决它:

imports:
    - { resource: my_custom_file.php }

具有以下内容:

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function(ContainerConfigurator $configurator) {
    // My specific logic

    // Saving the configuration as parameters
    $configurator->parameters()->set('some.prefix.host', $host);
    $configurator->parameters()->set('some.prefix.dbname', $dbname);
    // ...
};
于 2021-09-28T14:56:14.447 回答