Symfony DependencyInjection:导入多个资源(yaml)

Symfony DependencyInjection : import multiple resources (yaml)

我正在尝试在非 Symfony 项目中实施 Symfony 依赖注入组件(https://symfony.com/doc/current/components/dependency_injection.html

如果我将所有服务和参数放入一个“services.yaml”文件中,效果会很好。 例如:

# services.yaml

parameters:
    mysql.host: "127.0.0.1"
    mysql.database: "database"
    mysql.username: "root"
    mysql.password: ""

    oracle.database: ""
    oracle.username: ""
    oracle.password: ""

    cookie.domain: ".site.local"
    cookie.lifetime: 0
    cookie.useHttps: true

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    OtherNamespace\:
        resource: '../someHomemadeLibrary/src/*'
    
    OtherNamespace\Database\Drivers\MySqlDriver:
        class: OtherNamespace\Database\Drivers\MySqlDriver
        arguments:
            $host: "%mysql.host%"
            $database: "%mysql.database%"
            $username: "%mysql.username%"
            $password: "%mysql.password%"

    # ...    

由于文件越来越大,我想将它分成不同的文件,如下所述:https://symfony.com/doc/current/service_container/import.html#importing-configuration-with-imports

所以我试过了:

# index.php

$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator('config/'));
$loader->load('services.yaml');
# services.yaml

imports:
    - { resource: "databases.yaml" }
    - { resource: "sessions.yaml" }

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'


    OtherNamespace\:
        resource: '../someHomemadeLibrary/src/*'

# databases.yaml

parameters:
    mysql.host: "127.0.0.1"
    mysql.database: "database"
    mysql.username: "root"
    mysql.password: ""

services:
    OtherNamespace\Database\Drivers\MySqlDriver:
        class: OtherNamespace\Database\Drivers\MySqlDriver
        arguments:
            $host: "%mysql.host%"
            $database: "%mysql.database%"
            $username: "%mysql.username%"
            $password: "%mysql.password%"

    # ...  

但是在这个配置中,我得到以下错误:

Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: Cannot autowire service "OtherNamespace\Database\Drivers\MySqlDriver": argument "$host" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

如果我切换导入顺序,那么就是 CookieSession 有问题。就像导入覆盖参数和服务只使用最后一个?如何将我的服务及其参数拆分到多个文件中?

谢谢!

问题是我的服务已从我的 databases.yaml 和其他 yaml 正确导入到我的 services.yaml 文件中,但在此文件中,我还自动 类 转换为服务.

所以我定义的服务是 reloaded/converted,但没有参数和所有。我添加了排除项并且有效!

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'


    OtherNamespace\:
        resource: '../someHomemadeLibrary/src/*'
        exclude: 
        [
            '.../theClassFileDeclaredInAnotherYaml.php',
            '../or/a/whole/folder/not/to/be/loaded/again/*.php'
        ]

感谢@NicoHaase、@Cerad 和@WillB。感谢他们的帮助!